# [BZOJ4103][Thu Summer Camp 2015]异或运算

[BZOJ4103][Thu Summer Camp 2015]异或运算

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

6
5
1

1<=u<=d<=n<=1000,

1<=l<=r<=m<=300000,

1<=k<=(d-u+1)*(r-l+1),

1<=p<=500

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

#define maxn 1010
#define maxm 300010
#define maxnode 9600010
#define bin 30
#define oo 2147483647

int n, m, X[maxn];

int ToT, rt[maxm], ch[maxnode][2], siz[maxnode];
void update(int& y, int x, int p, int val) {
siz[y = ++ToT] = siz[x] + 1;
if(p < 0) return ;
memcpy(ch[y], ch[x], sizeof(ch[x]));
update(ch[y][val>>p&1], ch[x][val>>p&1], p - 1, val);
return ;
}
int lrt[maxn], rrt[maxn];
int qkth(int xl, int xr, int yl, int yr, int K) {
for(int i = xl; i <= xr; i++) lrt[i] = rt[yl-1], rrt[i] = rt[yr];
int ans = 0;
for(int i = bin; i >= 0; i--) {
int tmp = 0;
for(int j = xl; j <= xr; j++)
tmp += siz[ch[rrt[j]][X[j]>>i&1^1]] - siz[ch[lrt[j]][X[j]>>i&1^1]];
if(K <= tmp) {
for(int j = xl; j <= xr; j++)
lrt[j] = ch[lrt[j]][X[j]>>i&1^1],
rrt[j] = ch[rrt[j]][X[j]>>i&1^1];
ans = ans << 1 | 1;
}
else {
K -= tmp;
for(int j = xl; j <= xr; j++)
lrt[j] = ch[lrt[j]][X[j]>>i&1],
rrt[j] = ch[rrt[j]][X[j]>>i&1];
ans <<= 1;
}
}
return ans;
}

int main() {
for(int i = 1; i <= n; i++) X[i] = read();
for(int i = 1; i <= m; i++) update(rt[i], rt[i-1], bin, read());