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

#BS0000171. Sheikh – phiên bản khó (Sheikh (Hard Version))

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Sheikh – phiên bản khó (Sheikh (Hard Version))

Sheikh (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of nonnegative integers. For a contiguous subarray [l,r][l,r], let

S(l,r)=al+al+1+⋯+ar,S(l,r)=a_l+a_{l+1}+\cdots+a_r, X(l,r)=al⊕al+1⊕⋯⊕ar,X(l,r)=a_l\oplus a_{l+1}\oplus\cdots\oplus a_r,

where ⊕\oplus is bitwise XOR. The value of this subarray is f(l,r)=S(l,r)−X(l,r)f(l,r)=S(l,r)-X(l,r).

There are q=nq=n queries. Query ii specifies Li,RiL_i,R_i. Find a subarray [l,r][l,r] completely inside [Li,Ri][L_i,R_i] that maximizes f(l,r)f(l,r). Among all maximizing subarrays, choose one of minimum length r−l+1r-l+1. If several answers remain, output any of them.

The first query is always [1,n][1,n].

Input

  • The first line contains n,qn,q, where q=nq=n.
  • The second line contains exactly nn nonnegative integers a1,a2,…,ana_1,a_2,\ldots,a_n.
  • Each of the next qq lines contains Li,RiL_i,R_i with the query boundaries. It is guaranteed that L1=1,R1=nL_1=1,R_1=n.

Output

Print exactly qq lines. Line ii must contain indices l,rl,r of a maximizing segment within [Li,Ri][L_i,R_i], of minimum length among all maximizing segments.

Subtasks

  • Subtask 1 (20 points): 1≤n=q≤301\le n=q\le30; 0≤ai≤10000\le a_i\le1000 for all 1≤i≤n1\le i\le n; 1≤Li≤Ri≤n1\le L_i\le R_i\le n for all 1≤i≤q1\le i\le q; L1=1,R1=nL_1=1,R_1=n.
  • Subtask 2 (30 points): 1≤n=q≤30001\le n=q\le3000; 0≤ai≤1060\le a_i\le10^6 for all 1≤i≤n1\le i\le n; 1≤Li≤Ri≤n1\le L_i\le R_i\le n for all 1≤i≤q1\le i\le q; L1=1,R1=nL_1=1,R_1=n.
  • Subtask 3 (50 points): 1≤n=q≤1051\le n=q\le10^5; 0≤ai≤1090\le a_i\le10^9 for all 1≤i≤n1\le i\le n; 1≤Li≤Ri≤n1\le L_i\le R_i\le n for all 1≤i≤q1\le i\le q; L1=1,R1=nL_1=1,R_1=n.

Examples

Input

4 4
0 12 8 3
1 4
1 3
2 4
2 3

Output

2 3
2 3
2 3
2 3

Explanation

Segment [2,3][2,3] contains 12,812,8: the sum is 2020, the XOR is 12⊕8=412\oplus8=4, and f(2,3)=16f(2,3)=16.

All four query intervals contain [2,3][2,3]. The entire interval [1,4][1,4] has sum 2323 and XOR 0⊕12⊕8⊕3=70\oplus12\oplus8\oplus3=7, also producing 1616; [2,4][2,4] has value 1616 too. Thus the maximum query value is 1616, but [2,3][2,3] is only two elements long and no one-element segment can have positive value. Consequently, each output line is 2 3.