×

# FRMQ - Editorial

Author: Vasia Antoniuk
Tester: Sergey Kulik

Easy-Medium

# PREREQUISITES:

Range Maximum Queries

# PROBLEM:

You are given an array of integers, and are asked a lot of queries (at most 10^8) like give two indices find the maximum of all the integers in the array whose index lies between those two.

# EXPLANATION:

The generation of the queries as given in the question is
x = x + 7 (mod N - 1)
y = y + 11 (mod N)
l = min(x, y)
r = max(x, y)
You need to find maximum of a[i] such that l <= i <= r.

The generation of (l, r) pairs is like a pseudo random procedure, so its very very hard task to find a pattern in that. Now as we got (l, r) for each query the finding maximum is quite a standard task of range queries.

There are a lot of ways to find range queries. Some of them are
1. O(1) per Update and O(N) per query : Using an array, we can update each element in O(1) and when queried iterate through all elements from l to r.
2. O(log N) per Update and O(log N) per query : There are a lot of ways to do this. We can use Segment trees, Binary index trees, etc.
3. O(N) per Update and O(1) per query : We can use Sparse Table to solve this case.

The methods used for 2 and 3 are very standard and their explanation can be found at a lot of places.

Some useful links for solving Range Queries using Method 2 are
1. Utkarsh Lath's Blog using Segment trees
2. Geeks for Geeks using Segment trees
3. Using BIT

You can find very useful explanation of all methods including the sparse table method here in this Topcoder Tutorial.

# AUTHOR'S AND TESTER'S SOLUTIONS:

This question is marked "community wiki".

92852428
accept rate: 11%

19.8k350498541

 14 I expect to see a lot of comments like "I wrote a correct solution but got TL, why?!" below :D What was the reason for giving such constraints? There are no idea at all in given task, only problem is because of constraints. If you want to prevent logN solutions from passing - much lower constraints are enough; if you want to make task challenging and teach people to write well-optimized code - you may set TL even stricter (0.75 is still not hard to reach)... This problem is already awful even with 1 second, so why to stop at it? answered 14 Apr '15, 17:04 7★lebron 3.3k●3●17 accept rate: 24%
 7 The reason of arr[logN][N] getting accepted is that since x and y are increased only by a small amount, the row number tends to remain constant, and column number increases only by a small amount. So subsequent accesses tend to happen within the same memory block. This increases the number of cache hits. answered 14 Apr '15, 18:11 5★a7i7 151●2●5 accept rate: 0%
 5 And all the time i was thinking that the intended solution will be sleeker and awesome...what a disgrace Such problems must not be allowed to appear in Long Contests.. worst problem ever on CC.. waste of time!! answered 15 Apr '15, 20:03 231●1●3●12 accept rate: 0% 1 just by changing a[n][logn] to a[logn][n] the solution gets passed.. It doesn't make any sense for us to spend hours on end to think of such optimizations.. a constraint of m<=10^7 would have easily sufficed in this problem -_- (16 Apr '15, 05:03)
 3 To me Codechef long challenge is very important and seeing such a poor "code optimization" problem makes me sad. Codechef, I have high hopes from you, please take care before serving the problems. The efforts being made are great but this is one scar on the repute. answered 15 Apr '15, 20:20 5★saurv4u 31●2 accept rate: 0%
 2 I think M<=10⁷ or N,M<=10⁶ would have perfectly make the M*logN solutions time out, there was no need at all for M<=10⁸ (which is supposed to be TLE on other tasks btw) Why is nowadays every problem so much about crazy optimization? I mean, why do we have to spend hours trying to find out whether if we (don't) inline a function/use post-increment operator outside of while()/swap the dimensions of an array/take modulo one time less/other useless optimizations, then the running time will decrease by 0.01sec and will be inside the limit? It just doesn't make sense in my opinion, since you can't know, what will decrease and what will actually increase the running time. E.g. I changed N times post-increment on X to X+=N; and the program actually ran 0.10sec slower on one single test case. answered 15 Apr '15, 13:57 6★emarci15 21●1 accept rate: 0%
 2 I have no idea about sparse table someone please give me some tutorial answered 15 Apr '15, 22:27 3★nil96 180●7●18●45 accept rate: 5%
 1 I did just the same thing and was getting 70 points. I used and array for the sparse matrix as arr[N][logN] ,but as soon as I changed my array to arr[logN][N] ,it gave 100 Points.. What's the reason for this? Why did the running time decrease? answered 14 Apr '15, 18:01 4★amanmj 69●2 accept rate: 18% 1 Read about cache (14 Apr '15, 18:07)
 1 https://www.youtube.com/watch?v=0rCFkuQS968 Found an MIT lecture on this. Complexity: { O(N) (update),O(1) (query) } answered 18 Apr '15, 11:39 320●1●2●15 accept rate: 0% that's sweet, thanks (07 May '15, 15:36)
 0 will 10^8 run in one second? i used to assume around 10^7 calculations per second. what should is assume as upper limit for computations in one sec. answered 14 Apr '15, 17:58 163●5 accept rate: 10% 1 order of 10^8 operations per second is a good approximation on an upper bound. (15 Apr '15, 11:07) thanks, upper bound for codechef increased, but i feel its risky in competitions where you can make one submission or you get penalty for wrong one. (15 Apr '15, 17:15)
 0 Can someone explain "Method 3"? How to use splay trees with O(N) update and O(1) query? answered 14 Apr '15, 18:00 559●1●3●12 accept rate: 7% It have been fixed now; when edotorial was published, there was "We can use Splay Tree to solve this case." in case 3. (14 Apr '15, 20:06) lebron7★ Ok, thank you. (14 Apr '15, 20:07)
 0 Can any one give me a useful link for splay tree ? answered 14 Apr '15, 18:02 11●2 accept rate: 0% http://digital.cs.usu.edu/~allan/DS/Notes/Ch22.pdf (14 Apr '15, 18:09)
 0 This problem was kind of a disaster . Its very sad that all those people who didnt use a kind of worthless optimisation atleast from the respect of goals of codechef didnt get the points and hence the ranking they deserved. Very sad . answered 15 Apr '15, 00:35 148●5 accept rate: 0% 2 On the other hand, such "worthless optimization" may give you one more solved problem at regional contest or even world finals. For me it happened 10-15 times during trainings already. I don't think that this problem is good, for me picking such constants looks like easy way to make problem "harder" and simply get more money for it :D But people who can't solve a problem - don't deserve higher rankings. (15 Apr '15, 00:55) lebron7★
 0 I wrote the solution with sparse table but it took only 70 points. I managed to get 100 with these 2 further optimizations: When you have something like (A + b) % C and b is very small compared to C, using C++ the code(a + b) % c is far slower than a += b; if(a > c) a -= c; Don't ask me why because I don't know, but I noticed that only this was enough to take 100 point (from more than 1s to 0.8). N is O(10^5) so x can have at most 10^5 -1 different values, and y 10^5 different values. We can find the length of the two cycles ( length of x cycle = N / 7, length of y cycle = N/11, because 7 and 11 are prime numbers). Then you can find L = lcm of these 2 numbers and if L < M you can just make L queries and if sum[ i ] is the sum of answers at i-th iteration answer will be sum[ L ]* (M/L) + sum[ M % L]` answered 15 Apr '15, 03:11 9 accept rate: 0%
 0 Hi i used method 1 to solve this problem. this is my code with this i got only 20pts. Can any please tell me why i got only 20pts? answered 15 Apr '15, 08:30 12●1 accept rate: 0%
 0 @prasadram126 the O(M*N) solution is really supposed to get 20 points, you should implement the third one to get 100p answered 15 Apr '15, 13:58 6★emarci15 21●1 accept rate: 0%
 0 can anyone post a link to a resource where i can learn this approach of using sparse tables. answered 15 Apr '15, 17:50 163●5 accept rate: 10% 1 https://comeoncodeon.wordpress.com/2009/04/18/range-minimum-query/ (16 Apr '15, 14:15)
 0 http://www.codechef.com/viewsolution/6745851 i have implemented my solution based on topcoder tutorial of sparse table algorithm during the contest! But still i got 70 points! Please have a look at it ! answered 15 Apr '15, 21:49 3★grab 31●1 accept rate: 0% 1 Can you give the link to tutorial of sparse table (15 Apr '15, 22:34) nil963★ https://comeoncodeon.wordpress.com/2009/04/18/range-minimum-query/ (16 Apr '15, 14:15)
 0 arr[N][logN] worked for me (after lots of optimizations of course) answered 16 Apr '15, 11:56 291●1●4 accept rate: 21%
 0 segment tree is the best to be understood if someone knows DIVIDE AND CONQUER METHOD.I finally gt it answered 16 Apr '15, 22:26 35●2●13 accept rate: 0%
 0 can i get some better explanation for sparse table ,,,it's difficult to understand,,plz explain or give some link by which it can understand more easily answered 17 Apr '15, 20:24 1●1 accept rate: 0%
 0 i used segment tree to solve this question but in last two test cases i am getting runtime error can anyone explain me why i am getting runtime error link text i think so my solution is well optimized and i don't know for which test cases i am getting runtime error if i increased the size of array around 10^7 then i am getting tle please anyone explain me why i am getting runtime error and tle if i increase the size of array? answered 24 Apr '15, 22:28 4★viperx 31●1●5 accept rate: 0%
 0 Anybody solved this problem with BIT ? answered 04 May '15, 19:57 1 accept rate: 0%
 0 I solved the problem using segment tree which takes O(logn) time.I got only 70 points,for the last sub-task I am getting a TLE.Can someone please help me? solution- https://www.codechef.com/viewsolution/7497214 Any help is appreciated. answered 18 Jul '15, 17:00 37●1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,676
×1,672
×186
×56
×47
×33

question asked: 14 Apr '15, 15:05

question was seen: 9,268 times

last updated: 01 Jun '16, 20:21