# LOJ#510. 「LibreOJ NOI Round #1」北校门外的回忆（线段树）

## 题解

//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int K=-1,Z=0;
inline void Ot(){fwrite(sr,1,K+1,stdout),K=-1;}
void print(R int x){
if(K>1<<20)Ot();if(x<0)sr[++K]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++K]=z[Z],--Z);sr[++K]='\n';
}
const int N=2e5+5,M=7e6+5;
struct node;typedef node* ptr;
int f[31][N],n,q,k;map<int,int>tmp;map<pair<int,int>,ptr>rt;
struct node{ptr lc,rc;int s;}e[M],*pp=e;
int find(int x){
int res=x%k;x/=k;while(res&1^1)res>>=1;
for(R int i=0;(1<<i)<=x;++i)if(x>>i&1)res=f[i][res];
return res;
}
void ins(ptr &p,int l,int r,int x,int v){
if(p==NULL)p=++pp;p->s^=v;if(l==r)return;
int mid=(l+r)>>1;
x<=mid?ins(p->lc,l,mid,x,v):ins(p->rc,mid+1,r,x,v);
}
int query(ptr p,int l,int r,int x){
if(p==NULL)return 0;if(l==r)return p->s;
int mid=(l+r)>>1;
if(x<=mid)return query(p->lc,l,mid,x);
return (p->lc==NULL?0:p->lc->s)^query(p->rc,mid+1,r,x);
}
int main(){
//	freopen("testdata.in","r",stdin);
int t=k,bin=k&-k;
while(t&1^1)t>>=1;
for(R int i=1,tt;i<t;i+=2){
tt=i+t;while(tt&1^1)tt>>=1;
f[0][i]=tt;
}
for(R int i=1;(1<<i)<=n;++i)for(R int j=1;j<t;j+=2)f[i][j]=f[i-1][f[i-1][j]];
for(int op,x,v,t;q;--q){
while(x%k==0)x/=k,t*=k;
if(op&1){
while(x*t<=n&&(x&bin-1)){
tmp[x*t]^=v,x+=x%k;
while(x%k==0)x/=k,t*=k;
}
if(x*t<=n)ins(rt[make_pair(t,find(x))],1,n,x*t,v);
}else{
int res=0;
while(x){
res^=(x&bin-1?tmp[x*t]:query(rt[make_pair(t,find(x))],1,n,x*t));
x-=x%k;while(x&&x%k==0)x/=k,t*=k;
}
print(res);
}
}
return Ot(),0;
}

posted @ 2019-05-19 22:28  bztMinamoto  阅读(598)  评论(1编辑  收藏  举报
Live2D