Description

给定N个点以及每个点的权值,要你处理接下来的M个操作。
操作有4种。操作从0到3编号。点从1到N编号。
0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。
保证x到y是联通的。
1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。
2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。
3:后接两个整数(x,y),代表将点X上的权值变成Y。

Input

第1行两个整数,分别为N和M,代表点数和操作数。
第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。
第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。
1<=N,M<=300000

Output

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input

3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output

3
1

解题思路:

这是LCT很好的模板。
主要难点是处理一下路径权值,和判断两个点是否联通(以防重边)
路径权值:单独将x到y的路径提取(split)Splay维护链的时候在将节点处权值上传,最后查值就好
判断有无x到y的边:将x到y的路径提取,若直接连接无中间值就是有边。
代码:
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define lll tr[spc].ch[0]
#define rrr tr[spc].ch[1]
#define ls ch[0]
#define rs ch[1]
const int N=;
struct trnt{
int ch[];
int lzt;
int fa;
int val;
int sum;
bool anc;
}tr[N];
int n,m;
int cnt;
bool whc(int spc)
{
return tr[tr[spc].fa].rs==spc;
}
void pushup(int spc)
{
tr[spc].sum=tr[lll].sum^tr[rrr].sum^tr[spc].val;
return ;
}
void trr(int spc)
{
if(!spc)
return ;
std::swap(lll,rrr);
tr[spc].lzt^=;
return ;
}
void pushdown(int spc)
{
if(tr[spc].lzt)
{
trr(lll);
trr(rrr);
tr[spc].lzt=;
}
return ;
}
void recal(int spc)
{
if(!tr[spc].anc)
recal(tr[spc].fa);
pushdown(spc);
}
void rotate(int spc)
{
int f=tr[spc].fa;
bool k=whc(spc);
tr[f].ch[k]=tr[spc].ch[!k];
tr[spc].ch[!k]=f;
if(tr[f].anc)
{
tr[f].anc=false;
tr[spc].anc=true;
}else
tr[tr[f].fa].ch[whc(f)]=spc;
tr[spc].fa=tr[f].fa;
tr[f].fa=spc;
tr[tr[f].ch[k]].fa=f;
pushup(f);
pushup(spc);
}
void splay(int spc)
{
recal(spc);
while(!tr[spc].anc)
{
int ft=tr[spc].fa;
if(tr[ft].anc)
{
rotate(spc);
return ;
}
if(whc(spc)^whc(ft))
rotate(spc);
else
rotate(ft);
rotate(spc);
}
return ;
}
void access(int spc)
{
int lsts=;
while(spc)
{
splay(spc);
tr[rrr].anc=true;
tr[lsts].anc=false;
rrr=lsts;
pushup(spc);
lsts=spc;
spc=tr[spc].fa;
}
return ;
}
void Mtr(int spc)
{ access(spc);
splay(spc);
trr(spc);
return ;
}
int spmrt(int spc)
{
access(spc);
splay(spc);
while(lll)
{
pushdown(spc);
spc=lll;
}
return spc;
}
void split(int x,int y)
{
Mtr(x);
access(y);
splay(y);
}
void Link(int x,int y)
{
Mtr(x);
if(spmrt(y)!=x)
tr[x].fa=y;
return ;
}
void Cut(int x,int y)
{
Mtr(x);
if(spmrt(y)==x&&tr[x].fa==y&&!tr[y].rs)
{
tr[x].anc=;
tr[y].ls=;
tr[x].fa=;
pushup(y);
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&tr[i].val);
tr[i].anc=true;
}
while(m--)
{
int cmd,x,y;
scanf("%d%d%d",&cmd,&x,&y);
if(cmd==)
{
split(x,y);
printf("%d\n",tr[y].sum);
}
if(cmd==)
Link(x,y);
if(cmd==)
Cut(x,y);
if(cmd==)
{
splay(x);
tr[x].val=y;
pushup(x);
}
}
return ;
}

最新文章

  1. 【Discuz】云平台服务:出了点小错,由于站点ID/通信KEY等关键信息丢失导致Discuz!云平台服务出现异常
  2. Python DBUtils
  3. AngulaJS实战总结, 带你进入AngularJS世界(待续)
  4. MapReduce的执行过程.
  5. Ajax-数据格式-html
  6. DB天气app冲刺第八天
  7. 【转】Android中设置TextView的颜色setTextColor
  8. Gson 禁止特殊字符转码
  9. JAVA并发实现五(生产者和消费者模式Condition方式实现)
  10. JSP自定义标签——传统标签
  11. DeviceIoControl 直接从磁盘扇区读文件
  12. JVM 几个重要的参数
  13. Tensorflow激活函数
  14. 分布式系统一致性协议--2PC,3PC
  15. 201621123012《Java程序设计》第10次学习总结
  16. position:fixed部分版本的浏览器不支持
  17. Bad escape character ‘ygen’ 错误原因!
  18. codeforces 637A A. Voting for Photos(水题)
  19. jquery_final
  20. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

热门文章

  1. (2) 我的结果- spec2006中精确的simulation points运行点
  2. Ext4.0 经常使用代码整理(一)
  3. vue8 生命周期
  4. zzuli--1812--sort(模拟水题)
  5. 学习 shell —— 编写基本脚本
  6. NET Remoting原理及应用
  7. Sqoop 数据导入导出实践
  8. 2017.9.17校内noip模拟赛解题报告
  9. jquery基本Dom操作
  10. AtCoder Beginner Contest 067 D - Fennec VS. Snuke