Hii I am trying to solve this problem http://www.spoj.com/problems/ZQUERY/ after thinking so many days i am not able to come up with efficient solution ,would any one like to suggest some idea, how to solve this ?
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3857 |
| 2 | jiangly | 3810 |
| 3 | maroonrk | 3534 |
| 4 | tourist | 3528 |
| 5 | Kevin114514 | 3510 |
| 6 | turmax | 3411 |
| 7 | Um_nik | 3387 |
| 8 | Radewoosh | 3367 |
| 9 | heuristica | 3322 |
| 10 | strapple | 3317 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | maspy | 150 |
| 3 | Um_nik | 145 |
| 4 | Errichto | 139 |
| 5 | adamant | 136 |
| 6 | maroonrk | 134 |
| 7 | nik_exists | 133 |
| 7 | DNR | 133 |
| 9 | AmShZ | 130 |
| 10 | Dominater069 | 129 |
Hii I am trying to solve this problem http://www.spoj.com/problems/ZQUERY/ after thinking so many days i am not able to come up with efficient solution ,would any one like to suggest some idea, how to solve this ?
| Name |
|---|



Obviously, you can sort the queries to solve the problem offline and use prefix sums to covert the question to "longest subarray with S[l] = S[r], L ≤ l ≤ r ≤ R.
Then, you can increase R and keep the answers for all L; it's just a matter of updating these answers when R is incremented. It can be done in
.
@Xellos Thanks , can you explain more how to solve efficiently to find the longest subarray with S[l]=S[r] ,L<=l<=r<=R,
can you please explain the part keep answer for all L?
First we use this Mo's algorithm to sort Query.
http://codeforces.me/blog/entry/7383
Let's call each group is a "bucket"
For each Query, we have to find i j which s[i] = s[j] and L<=i<=j<=R
Each i j has 3 cases :
i and j in bucket, so we can for, just O(sqrt(n))
i in bucket, j out of bucket. Let's call id1[s[j]]=j is the max position of s[j]. So answer is max(id1[s[i]]-i)
i and j out of bucket. Similar, call id2[s[j]]=j is the min position of s[j], so answer is max(id1[s[j]]-id2[s[j]])
I_love_PMK can you explain bit more after query sorting step ?
Let's p = sqrt(n)
Every query has L <= p is in bucket 1
p < L <= 2*p is in bucket 2....
With every query in the same bucket, sort query with increasing of R.
I have solved it with MO's algorithm + segment tree for keeping track of the maximum value . Complexity is O( (N + M) * ROOT(N) * LOG (N) ) .
Here is the implementation http://ideone.com/YayNX7.
It would be good is someone can tell how to solve without the segment tree in O( (N + M) * ROOT(N) ).
Could you please explain how did you solve it??
Have a look at codechef march challenge problem: qchef , it has a nice and detailed editorial.
Thanks Everyone now it is solved :)
Here is a editorial to a different problem with a similar solution.