You are not logged in. Please login at www.codechef.com to post your questions!

×

FRMQ - Editorial

-11
3

PROBLEM LINK:

Practice
Contest

Author: Vasia Antoniuk
Tester: Sergey Kulik
Editorialist: Adury Surya Kiran

DIFFICULTY:

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:

Author's solution
Tester's solution

This question is marked "community wiki".

asked 14 Apr '15, 15:05

adurysk's gravatar image

4★adurysk ♦
92842428
accept rate: 11%

edited 01 Jun '16, 20:21

admin's gravatar image

0★admin ♦♦
19.0k348495533


123next »

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?

link

answered 14 Apr '15, 17:04

lebron's gravatar image

7★lebron
2.8k316
accept rate: 26%

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.

link

answered 14 Apr '15, 18:11

a7i7's gravatar image

5★a7i7
15125
accept rate: 0%

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!!

link

answered 15 Apr '15, 20:03

usaxena95's gravatar image

7★usaxena95
2211212
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) gvaibhav217★

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.

link

answered 15 Apr '15, 20:20

saurv4u's gravatar image

5★saurv4u
312
accept rate: 0%

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.

link

answered 15 Apr '15, 13:57

emarci15's gravatar image

6★emarci15
211
accept rate: 0%

I have no idea about sparse table someone please give me some tutorial

link

answered 15 Apr '15, 22:27

nil96's gravatar image

2★nil96
18071844
accept rate: 5%

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?

link

answered 14 Apr '15, 18:01

amanmj's gravatar image

4★amanmj
692
accept rate: 18%

1

Read about cache

(14 Apr '15, 18:07) alexvaleanu4★

https://www.youtube.com/watch?v=0rCFkuQS968
Found an MIT lecture on this. Complexity: { O(N) (update),O(1) (query) }

link

answered 18 Apr '15, 11:39

codedecode0111's gravatar image

5★codedecode0111
3201214
accept rate: 0%

that's sweet, thanks

(07 May '15, 15:36) ashish17293★

Can someone explain "Method 3"? How to use splay trees with O(N) update and O(1) query?

link

answered 14 Apr '15, 18:00

alexvaleanu's gravatar image

4★alexvaleanu
5591312
accept rate: 7%

edited 14 Apr '15, 18:01

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) alexvaleanu4★

Can any one give me a useful link for splay tree ?

link

answered 14 Apr '15, 18:02

shakil_ruet's gravatar image

5★shakil_ruet
112
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×14,464
×1,409
×185
×46
×44
×27

question asked: 14 Apr '15, 15:05

question was seen: 8,850 times

last updated: 01 Jun '16, 20:21