Cyclic Shift and Minimal Inversions
Time limit2sMemory limit512 MB
Maintain a permutation under subarray cyclic-shift updates, and after each update report the smallest shift k whose cyclic rotation has the fewest inversions.
- Level
Hard8 of 10
- Topics
- Array, Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Problem
A cyclic shift of a sequence by () positions is the following sequence . Note that 0-indexing is used. For example, cyclic shift by 0 positions is equal to the original sequence.
There is a permutation which is initially equal to the identity permutation ().
Process a number of the following queries. Replace a subarray of the permutation by its cyclic shift by some number of positions. Formally you are given , such that , and replace the subarray between indices and (a half-interval, -th element is included, -th is not) by its cyclic shift by positions. For example, applying this operation to the permutation with parameters results in a permutation .
After each query find the cyclic shift of the permutation which contains the minimal number of inversions.
The changes of the permutation persist and are not reverted between queries.
Input
The first line contains two integers and (), length of the permutation and the number of queries, respectively.
lines follow. -th of them contains three integers (), parameters of the -th query.
Output
Print lines. -th of them should contain an integer (), such that after the first queries cyclic shift of the permutation by positions contains the minimal number of inversions among cyclic shifts. If there are multiple possible print the smallest one.
Hint
In the first example after the modification the permutation is . Its cyclic shift with the minimal number of inversions is .