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

#SRD0000080. XOR trên các đoạn giá trị (Xors on Segments)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

XOR trên các đoạn giá trị (Xors on Segments)

Xors on Segments

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Define f(u,v)=u⊕(u+1)⊕⋯⊕vf(u,v)=u\oplus(u+1)\oplus\cdots\oplus v for u≤vu\le v. For each index range [l,r][l,r], choose indices x,yx,y (possibly the same) with ax≤aya_x\le a_y and maximize f(ax,ay)f(a_x,a_y). The XOR spans all consecutive integers from the smaller to the larger value.

Input

Read n,qn,q, the array, and qq inclusive ranges.

Output

Print the maximum XOR value for each query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤301\le n\le 30, 1≤q≤401\le q\le 40. 1≤ai≤1061\le a_i\le10^6; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤10001\le n\le 1000, 1≤q≤2001\le q\le 200. 1≤ai≤1061\le a_i\le10^6; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (25 points): 1≤n≤80001\le n\le 8000, 1≤q≤15001\le q\le 1500. 1≤ai≤1061\le a_i\le10^6; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (45 points): 1≤n≤500001\le n\le 50000, 1≤q≤50001\le q\le 5000. 1≤ai≤1061\le a_i\le10^6; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

7
7
7

Explanation

Range [3,4][3,4] contains values 3 and 4, so f(3,4)=3⊕4=7f(3,4)=3\oplus4=7. All other choices in this small range give at most 7; the two larger ranges also contain this pair and have maximum 7.