BZOJ4546: codechef XRQRS

Description:

$1<=x<=500000$
$1<=N<=520000$

Solution:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 520000+9;
int tot_len;
struct T{
int ch[2];
int size;
int &operator [] (int x){
return ch[x];
}
}tree[MAXN*20];
int rt[MAXN];
int cnt;

int insert(int pre,int val,int bit_length){
int now=++cnt;
tree[now]=tree[pre];
tree[now].size++;
if(bit_length==-1)
return now;
bool tmp=val&(1<<bit_length);
if(tmp)tree[now][1]=insert(tree[pre][1],val,bit_length-1);
else tree[now][0]=insert(tree[pre][0],val,bit_length-1);
return now;
}

int del(int x){
tot_len-=x;
}

int max_xor(int pre,int now,int val){
int re=0;
for(int bit_length=19;bit_length>-1;bit_length--){
bool tmp=val&(1<<bit_length);
bool jud=tree[tree[now][tmp^1]].size-tree[tree[pre][tmp^1]].size;
if(jud){
re|=(tmp^1)*(1<<bit_length);
pre=tree[pre][tmp^1];now=tree[now][tmp^1];
}
else{
re|=(tmp)*(1<<bit_length);
pre=tree[pre][tmp];now=tree[now][tmp];
}
}
return re;
}

int smallers(int pre,int now,int val){
int re=0;
for(int bit_length=19;bit_length>-1;bit_length--){
bool tmp=val&(1<<bit_length);
if(tmp){
re+=tree[tree[now][0]].size-tree[tree[pre][0]].size;
}
pre=tree[pre][tmp];now=tree[now][tmp];
}
//叶子节点
re+=tree[now].size-tree[pre].size;
return re;
}

int k_th(int pre,int now,int val){
int re=0;
for(int bit_length=19;bit_length>-1;bit_length--){
int tmp=tree[tree[now][0]].size-tree[tree[pre][0]].size;
if(val<=tmp){
pre=tree[pre][0];now=tree[now][0];
}
else{
val-=tmp;
re|=(1<<bit_length);
pre=tree[pre][1];now=tree[now][1];
}
}
return re;
}

int main(){
int kase;
scanf("%d",&kase);
while(kase--){
int opt,x,l,r;
scanf("%d",&opt);
switch(opt){
case 1:scanf("%d",&x);tot_len++;rt[tot_len]=insert(rt[tot_len-1],x,19);break;
case 2:scanf("%d%d%d",&l,&r,&x);printf("%d\n",max_xor(rt[l-1],rt[r],x));break;
case 3:scanf("%d",&x);del(x);break;
case 4:scanf("%d%d%d",&l,&r,&x);printf("%d\n",smallers(rt[l-1],rt[r],x));break;
default:scanf("%d%d%d",&l,&r,&x);printf("%d\n",k_th(rt[l-1],rt[r],x));break;
}
}
return 0;
}

posted @ 2018-01-21 13:02  zzzc18  阅读(84)  评论(0编辑  收藏  举报