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

#SRD0000147. Dãy con của dãy con (Sub-subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy con của dãy con (Sub-subsequence)

Sub-subsequence

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a nonnegative integer array AA of length nn and qq online queries. Each query supplies two integers x,yx,y. Let lastans denote the preceding query's answer, initially 00. Decode the interval as

$$l=(x+\text{lastans})\bmod n+1,\qquad r=(y+\text{lastans})\bmod n+1.$$

Swap l,rl,r if l>rl>r. Find the maximum bitwise XOR of any nonempty contiguous subarray contained entirely in Al,…,ArA_l,\ldots,A_r. Output that maximum, then update lastans before reading the next query.

The queries must be processed in input order; their effective intervals depend on previous answers.

Input

  • The first line contains n,qn,q.
  • The second line has nn nonnegative integers A1,…,AnA_1,\ldots,A_n.
  • Each of the next qq lines contains the two integers x,yx,y that encode a query.

Output

Print qq lines. Line tt contains the maximum XOR of a nonempty contiguous subarray in the decoded interval of query tt.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤401\le q\le 40; 0≤Ai,x,y≤1090\le A_i,x,y\le10^9; interval indices decoded using lastans.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 0≤Ai,x,y≤1090\le A_i,x,y\le10^9; interval indices decoded using lastans.
  • Subtask 3 (50 points): 1≤n≤200001\le n\le 20000; 1≤q≤200001\le q\le 20000; 0≤Ai,x,y≤1090\le A_i,x,y\le10^9; interval indices decoded using lastans.

Examples

Example 1

Input

5 3
8 6 2 4 5
0 4
0 2
2 4

Output

14
6
14

Explanation

Initially lastans=0; query (0,4)(0,4) decodes to [1,5][1,5] and the subarray [8,6][8,6] has maximum XOR 1414. The next (0,2)(0,2) decodes using 1414 to (5,2)(5,2), hence [2,5][2,5]; the single-element subarray [6][6] yields maximum 66. The final (2,4)(2,4) decodes using 66 to (4,1)(4,1), hence [1,4][1,4], whose maximum is 1414.