CF1732C2.Sheikh (Hard Version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference is that in this version q=n .
You are given an array of integers a1,a2,…,an .
The cost of a subsegment of the array [l,r] , 1≤l≤r≤n , is the value f(l,r)=sum(l,r)−xor(l,r) , where sum(l,r)=al+al+1+…+ar , and xor(l,r)=al⊕al+1⊕…⊕ar ( ⊕ stands for bitwise XOR).
You will have q queries. Each query is given by a pair of numbers Li , Ri , where 1≤Li≤Ri≤n . You need to find the subsegment [l,r] , Li≤l≤r≤Ri , with maximum value f(l,r) . If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of r−l+1 .
输入格式
Each test consists of multiple test cases. The first line contains an integer t ( 1≤t≤104 ) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n and q ( 1≤n≤105 , q=n ) — the length of the array and the number of queries.
The second line of each test case contains n integers a1,a2,…,an ( 0≤ai≤109 ) — array elements.
i -th of the next q lines of each test case contains two integers Li and Ri ( 1≤Li≤Ri≤n ) — the boundaries in which we need to find the segment.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
It is guaranteed that L1=1 and R1=n .
输出格式
For each test case print q pairs of numbers Li≤l≤r≤Ri such that the value f(l,r) is maximum and among such the length r−l+1 is minimum. If there are several correct answers, print any of them.
输入输出样例
输入#1
6 1 1 0 1 1 2 2 5 10 1 2 2 2 3 3 0 2 4 1 3 1 2 2 3 4 4 0 12 8 3 1 4 1 3 2 4 2 3 5 5 21 32 32 32 10 1 5 1 4 1 3 2 5 3 5 7 7 0 1 0 1 0 1 0 1 7 3 6 2 5 1 4 4 7 2 6 2 7
输出#1
1 1 1 1 2 2 1 1 1 1 2 2 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 3 4 2 4 4 6 2 4 2 4 4 6 2 4 2 4
说明/提示
In all test cases, the first query is considered.
In the first test case, f(1,1)=0−0=0 .
In the second test case, f(1,1)=5−5=0 , f(2,2)=10−10=0 . Note that f(1,2)=(10+5)−(10⊕5)=0 , but we need to find a subsegment with the minimum length among the maximum values of f(l,r) . So, only segments [1,1] and [2,2] are the correct answers.
In the fourth test case, f(2,3)=(12+8)−(12⊕8)=16 .
There are two correct answers in the fifth test case, since f(2,3)=f(3,4) and their lengths are equal.