习题:Deduction Queries(冰茶姬)
题目
思路
考虑到异或的特殊性
将区间转换成两个前缀异或和的异或即可
代码
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
namespace ufs
{
int fa[400005];
int w[400005];
void makeset(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int findset(int x)
{
if(fa[x]==x)
return x;
int t=findset(fa[x]);
w[x]^=w[fa[x]];
return fa[x]=t;
}
void merge(int u,int v,int ty)
{
swap(u,v);
int a=findset(u);
int b=findset(v);
fa[a]=b;
w[a]=w[u]^ty^w[v];
w[b]=0;
}
}
using namespace ufs;
int n,cnt;
int lans;
int opt,l,r,x;
map<int,int> hashh;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
makeset(2*n);
for(int i=1;i<=n;i++)
{
cin>>opt>>l>>r;
l^=lans;
r^=lans;
if(l>r)
swap(l,r);
l--;
if(hashh.count(l)==0)
hashh[l]=++cnt;
if(hashh.count(r)==0)
hashh[r]=++cnt;
l=hashh[l];
r=hashh[r];
if(opt==1)
{
cin>>x;
x^=lans;
if(findset(l)!=findset(r))
merge(l,r,x);
}
else
{
if(findset(l)!=findset(r))
{
lans=1;
cout<<"-1\n";
}
else
{
lans=w[l]^w[r];
cout<<lans<<'\n';
}
}
}
return 0;
}

浙公网安备 33010602011771号