Min difference queries
Practice
4.7 (6 votes)
Advanced data structures
Data structures
Medium
Query
Segment trees
普通 難しい
Problem
52% Success 1396 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

You have a sequence a of length n and you want to answer q queries determined by 2 integers l and r. You take all numbers present in subsequence \((a_l, a_{l + 1}, \dots , a_r)\), let them be \(b_1, b_2, \dots , b_t\) and number of occurrenses of each number in this subsequence be \(c_1, c_2, \dots, c_t\) respectively. The answer for per query is equal to \(\min |c_i - c_j|\) for \(1 \le i < j \le t\) or 1 if \(t = 1\).

\(\textbf{Input}\)

The first line contains 2 integers - n and q \((1 \le n , q \le 100 \; 000)\).

The second line contains n numbers - \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^9)\).

Each of the next q lines contains 2 integers - \(u_i,v_i\) \((1 \le u_i, v_i \le n)\).

You will need to answer the queries online so the information is encoded, if the answer for \((i - 1) ^ {th}\) query is \(last\) (initially \(last = 0\)) then the \(i^{th}\) query is :

\( l_i = ((u_i + last) \; mod \; n) + 1\)

\( r_i = ((v_i + last) \; mod \; n) + 1\)

Note that "last" can be -1 in some cases.

\(\textbf{Output}\)

Output q lines, the \(i^{th}\) line contains the answer for the \(i^{th}\) query.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
MediumSegment Trees
Points:50
5 votes
Tags:
Advanced Data StructuresData StructuresMediumSegment Trees
Points:50
8 votes
Tags:
Advanced Data StructuresData StructuresSegment Trees