Summary: Given an array $$$A$$$ with $$$N$$$ elements. Find the largest continous subbarray $$$(l,\ r)$$$ of $$$A$$$ that: $$$max(A_l,\ A_{l + 1},\ ...,\ A_r)\ \vdots\ min(A_l,\ A_{l + 1},\ ...,\ A_r)$$$. Print the value $$$r - l + 1$$$. constraints: $$$n\leq 6\times 10^5,\ a_i\leq 10^5$$$.
Subtasks:
$$$n\leq 500$$$ and $$$n\leq 5000$$$: for $$$O(n^2)$$$.
$$$a_1\leq a_2\leq ...\leq a_n$$$: for dp $$$O(n\ log\ n)$$$.
$$$a_i\leq 1000$$$: dont know how to do.
$$$n\leq 10^5$$$: dont know how to do.
No additional constraints: dont know how to do.
Please help me with this problem, thanks for your help and attention. Have a good day !








Auto comment: topic has been updated by nhphuc (previous revision, new revision, compare).
What is ⋮
$$$x\ \vdots\ y$$$ mean $$$x$$$ can be divided by $$$y$$$ sir
Cartesian tree and sieve should solve in nlog^2n if I'm not mistaken.
For $$$n\leq 6\times 10^5$$$ I don't think it will pass.
If N = 6 * 10^5 and C = 10^5, then the exact complexity is something like C + NlogNlogC, which I think is fine.
I haven't thought it out completely but it might be worth a try.
Sorry but do your solution need to visit all divisors of a_i ?
We can sieve from the minimums instead of checking the factors of the maximum. 1 visits n numbers 2 visits n/2 numbers 3 visits n/3 numbers and so on
H_n = n/1 + n/2 + n/3 + n/4 + ... + n/n = O(nlogn)
If we keep track of the min/max ranges for each number (O(n) using a cartesian tree, for example) we can speed things up greatly.
Using running stack, precompute the $$$\text{pr1}[]$$$ and $$$\text{nx1}[]$$$ to store the previous/next $$$j$$$ such that $$$A_j \lt A_i$$$.
Also precompute $$$\text{pr2}[]$$$ and $$$\text{nx2}[]$$$ for $$$A_j \gt A_i$$$
Bruteforce the index of the $$$\max A_i$$$.
For each max $$$A_i$$$, there are $$$\mathcal{O}(\sqrt[3]{A_i})$$$ divisors, so we can bruteforce the divisors that will be $$$\min A_j$$$.
For current $$$A_i$$$, and current divisor $$$X$$$, find the largest $$$j$$$ such that $$$j\leq i$$$ and $$$A_j = X$$$.
Also find the smallest $$$k$$$ such that $$$i\leq k$$$ and $$$A_k = X$$$.
If $$$j$$$ doesn't exist, or $$$j \leq \text{pr2}_i$$$, set $$$j$$$ to $$$i$$$
If $$$k$$$ doesn't exist, or $$$\text{nx2}_i \leq k$$$ set $$$k$$$ to $$$i$$$
$$$j = \max(\text{pr2}_i+1, \text{pr1}_j+1), k = \min(\text{nx2}_i-1, \text{nx1}_k-1)$$$
$$$\text{ans} = \max(\text{ans}, k-j+1)$$$
Let $$$C$$$ = $$$\max_{i=1,\dots,n} A_i$$$
Time complexity: $$$O(N\sqrt[3]{C}+C\log{C})$$$
UPD: Made some minor corrections + Code in comments below
Is there any case where $$$j$$$ or $$$k$$$ does not have to be the closest point to i such that $$$a_j = X$$$ or $$$a_k = X$$$? If it happens, I wonder how to solve it.
The above accounts for that, since it may be possible to extend without changing both max and min
i found a counter case for your solution here, please let me know if i'm wrong.
n = 8 a = [10, 4, 20, 19, 29, 16, 30, 17]
answer is 4, yours would output 7
it seems that there was indeed something wrong with my implementation, i corrected it and it now returns 4.
I've just coded it and there's one more case to consider. That the actual smallest from j....i (and i....k) is at j/k
And also a corner case when extending the range
Try this Code
I just used your code and submit it into problem on judge and it get full WA/TLE. Maybe some bug appear here.
I didn't initialize the $$$\text{lst}[]$$$ properly, but this definitely works: Code
This code got an accepted, orz.
hello, is there a submission link available for this problem?
No, there's not, this is a problem in my province contest and just users who have access into contest can virtual and make a submit.
i have coded something up, can you please help me try and submit it?
You can send me your source code in DM, I'll try it for you.
My idea is:
Not sure how to properly evaluate complexity, but seems like $$$O(n \log A)$$$.Tested again random input this works in
The judge of this problem just has cpp, python, pypy and pascal so I can test this for you but an $$$O(n\ log\ A)$$$ solution can passed this problem (if get no WA :P).
Ask AI to translate code without test section to cpp, or try implementing idea by yourself, a little long, but simple.
In the test section there are small tests against slow bruteforce solution, so I don't believe it could get WA. TLE maybe because 4 nested loops look really scary, maybe it would be necessary to iterate over smaller of maxints[mx] and minints[mn] first.