PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000071. Tần suất lớn nhất trực tuyến (Mode)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tần suất lớn nhất trực tuyến (Mode)

Mode

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Queries are online. Each input pair x,yx,y is decoded using the previous answer lastanslastans: l=(x+lastans) mod n+1l=(x+lastans)\bmod n+1, r=(y+lastans) mod n+1r=(y+lastans)\bmod n+1, swapping if l>rl>r. Output the maximum frequency of any value in Al,…,ArA_l,\ldots,A_r and set lastanslastans to this answer; initially lastans=0lastans=0.

Input

Read n,qn,q, the array and qq encoded pairs x,yx,y; these are not direct interval endpoints.

Output

Print the mode frequency of each decoded interval.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60, 1≤q≤801\le q\le 80. 1≤Ai≤2⋅1051\le A_i\le2\cdot10^5; 1≤x,y≤n1\le x,y\le n.
  • Subtask 2 (20 points): 1≤n≤30001\le n\le 3000, 1≤q≤30001\le q\le 3000. 1≤Ai≤2⋅1051\le A_i\le2\cdot10^5; 1≤x,y≤n1\le x,y\le n.
  • Subtask 3 (30 points): 1≤n≤300001\le n\le 30000, 1≤q≤300001\le q\le 30000. 1≤Ai≤2⋅1051\le A_i\le2\cdot10^5; 1≤x,y≤n1\le x,y\le n.
  • Subtask 4 (40 points): 1≤n≤2000001\le n\le 200000, 1≤q≤2000001\le q\le 200000. 1≤Ai≤2⋅1051\le A_i\le2\cdot10^5; 1≤x,y≤n1\le x,y\le n.

Examples

Input

5 3
1 2 2 3 2
1 3
1 5
4 5

Output

2
1
1

Explanation

Initially lastans=0lastans=0; input (1,3)(1,3) decodes to [2,4][2,4] and values [2,2,3][2,2,3], so the answer is 2. Next (1,5)(1,5) with lastans=2lastans=2 decodes to [3,4][3,4] after swapping, with maximum frequency 1. The last query also returns 1.