C1. Sheikh (Easy version)

C1. Sheikh (Easy version)

This is the easy version of the problem. The only difference is that in this version q=1.

You are given an array of integers a1,a2,,an.

The cost of a subsegment of the array [l,r], 1lrn, is the value f(l,r)=sum(l,r)xor(l,r), where sum(l,r)=al+al+1++ar, and xor(l,r)=alal+1ar ( stands for bitwise XOR).

You will have q=1 query. Each query is given by a pair of numbers Li, Ri, where 1LiRin. You need to find the subsegment [l,r], LilrRi, 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 rl+1.

Input

Each test consists of multiple test cases. The first line contains an integer t (1t104) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers n and q (1n105,q=1) — the length of the array and the number of queries.

The second line of each test case contains n integers a1,a2,,an (0ai109) — array elements.

i-th of the next q lines of each test case contains two integers Li and Ri (1LiRin) — 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 2105.

It is guaranteed that L1=1 and R1=n.

Output

For each test case print q pairs of numbers LilrRi such that the value f(l,r) is maximum and among such the length rl+1 is minimum. If there are several correct answers, print any of them.

Example

input

复制代码
6
1 1
0
1 1
2 1
5 10
1 2
3 1
0 2 4
1 3
4 1
0 12 8 3
1 4
5 1
21 32 32 32 10
1 5
7 1
0 1 0 1 0 1 0
1 7
复制代码

output

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

Note

In the first test case, f(1,1)=00=0.

In the second test case, f(1,1)=55=0, f(2,2)=1010=0. Note that f(1,2)=(10+5)(105)=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)(128)=16.

There are two correct answers in the fifth test case, since f(2,3)=f(3,4) and their lengths are equal.

 

解题思路

  当l固定后,f(l,r)是一个随着r增加的递增函数,即f(l,r)f(l,r+1)。当加入一个新的元素x,此时sum增加了x,而xor的增量不会超过x。要知道xor本质是不进位加法,因此axa+x。所以假设f(l,r)=ssx,那么f(l,r+1)=(s+x)(sxx)(s+x)(sx+x)=ssx,因此f(l,r+1)f(l,r)(ssx)(ssx)=0,即当l固定后,f(l,r)是一个递增函数。如果没想到这一步的话那这道题就做不出来了。

  因此,我们知道当l固定后,最大值就是f(l,n),又因为我们要使得区间长度尽可能小,且f(l,r)是一个递增函数,因此我们可以二分出值为f(l,n)最靠左的一个右端点。

  因此做法就是枚举左端点l,对于固定的左端点二分出最靠左且值为f(l,n)的右端点。

  AC代码如下,时间复杂度为O(nlogn)

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 LL s[N], sx[N];
 9 
10 void solve() {
11     int n, m;
12     scanf("%d %d", &n, &m);
13     for (int i = 1; i <= n; i++) {
14         int x;
15         scanf("%d", &x);
16         s[i] = s[i - 1] + x;    // 预处理前缀和
17         sx[i] = sx[i - 1] ^ x;    // 预处理前缀异或和
18     }
19 
20     while (m--) {
21         int x, y;
22         scanf("%d %d", &x, &y);
23         LL maxv = -1, a, b;    // maxv是最大f值,a、b是答案的左右端点
24         for (int i = x; i <= y; i++) {    // 枚举左端点i
25             int l = i, r = y;    // 在区间[i, y]中找到最靠左的右端点r,使得f(i,r) = f(i,y)
26             LL t = s[y] - s[i - 1] - (sx[y] ^ sx[i - 1]);    // f(i,y)
27             while (l < r) {    // 二分右端点
28                 int mid = l + r >> 1;
29                 if (s[mid] - s[i - 1] - (sx[mid] ^ sx[i - 1]) >= t) r = mid;
30                 else l = mid + 1;
31             }
32             if (t > maxv) {
33                 maxv = s[l] - s[i - 1] - (sx[l] ^ sx[i - 1]);
34                 a = i, b = l;
35             }
36             else if (t == maxv && b - a > l - i) {
37                 a = i, b = l;
38             }
39         }
40         printf("%d %d\n", a, b);
41     }
42 }
43 
44 int main() {
45     int t;
46     scanf("%d", &t);
47     while (t--) {
48         solve();
49     }
50 
51     return 0;
52 }
复制代码

 

参考资料

  Codeforces Round #830 (Div. 2) Editorial:https://codeforces.com/blog/entry/108327

posted @   onlyblues  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示