#SGM0000073. Xây dựng quân đội (Army Creation)
Xây dựng quân đội (Army Creation)
Army Creation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem
Warrior has type . From a decoded interval , choose as many warriors as possible while taking at most of each type. Decode l=((x+last)mod n)+1 and r=((y+last)mod n)+1, then swap if needed.
Input
The first line contains , the second line the types, the third line , followed by encoded pairs .
Output
Print each maximum army size and use it as last for the next query.
Subtasks
Subtask 1 (20%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 2 (30%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 3 (50%)
Example
Input
6 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6
Output
2
4
1
3
2
Explanation
With , at most two warriors of every type can be chosen. Intervals are decoded sequentially using the previous answer.