给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R)。求A[L] 至 A[R] 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值是多少?
Input
第1行:2个数N, Q中间用空格分隔,分别表示数组的长度及查询的数量(1 <= N <= 50000, 1 <= Q <= 50000)。 第2 - N+1行:每行1个数,对应数组A的元素(0 <= A[i] <= 10^9)。 第N+2 - N+Q+1行:每行3个数X, L, R,中间用空格分隔。(0 <= X <= 10^9,0 <= L <= R < N)
Output
输出共Q行,对应数组A的区间[L,R]中的数与X进行异或运算,所能得到的最大值。
Input示例
15 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 10 5 9 1023 6 6 33 4 7 182 4 9 181 0 12 5 9 14 99 7 8 33 9 13
Output示例
13 1016 41 191 191 15 107 47
本来以为会超时,没想到竟然水过去了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
const int N = 50000+10;
typedef long long LL;
typedef pair<int,int> pi;
vector<int>p[N*30];
int rt[N*30][2], cnt;
LL ans;
void insert1(LL x,int id)
{
int root=0;
for(int i=30;i>=0;i--)
{
int v=((x&(1<<i))?1:0);
if(rt[root][v]==-1)
{
rt[root][v]=cnt;
rt[cnt][0]=rt[cnt][1]=-1;
cnt++;
}
root=rt[root][v];
p[root].push_back(id);
}
return ;
}
void get(LL x,int l,int r)
{
int root=0;
for(int i=30;i>=0;i--)
{
int v=((x&(1<<i))?1:0);
if(rt[root][1^v]!=-1)
{
int y=rt[root][1^v];
int pos=lower_bound(p[y].begin(),p[y].end(),l)-p[y].begin();
if(pos!=p[y].size()&&p[y][pos]>=l&&p[y][pos]<=r)
{
ans|=(1<<i);
root=y;
continue;
}
}
if(rt[root][v]!=-1)
{
int y=rt[root][v];
if(v==1) ans^=(1<<i);
root=y;
}
}
return ;
}
int main()
{
int n, m;
LL x;
scanf("%d %d", &n, &m);
rt[0][0]=rt[0][1]=-1;
cnt=1;
for(int i=0;i<n;i++)
{
scanf("%lld", &x);
insert1(x,i);
}
while(m--)
{
int l, r;
scanf("%lld %d %d", &x, &l, &r);
ans=x;
get(x, l, r);
printf("%lld\n",ans);
}
return 0;
}