3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 2249  Solved: 1042
[Submit][Status][Discuss]

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行,每行三个整数,分别代表操作类型和操作所需的量。

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

HINT

1<=N,M<=300000

code

 #include<cstdio>
#include<algorithm> using namespace std; const int N = ; int val[N],fa[N],ch[N][],rev[N],sum[N],st[N],top; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF :*p1++;
}
inline int read() {
int x = ,f = ;char ch=nc();
for (; ch<''||ch>''; ch = nc()) if (ch == '-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x*+ch-'';
return x * f;
}
void pushup(int x) {
sum[x] = sum[ch[x][]] ^ sum[ch[x][]] ^ val[x];
}
void pushdown(int x) {
int l = ch[x][],r = ch[x][];
if (rev[x]) {
rev[l] ^= ;rev[r] ^= ;
swap(ch[x][],ch[x][]);
rev[x] ^= ;
}
}
bool isroot(int x) {
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
int son(int x) {
return ch[fa[x]][]==x;
}
void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
void splay(int x) {
top = ;st[++top] = x;
for (int i=x; !isroot(i); i=fa[i]) st[++top] = fa[i];
while (top) pushdown(st[top--]);
while (!isroot(x)) {
int y = fa[x];
if (!isroot(y)) {
if (son(x)==son(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
}
void access(int x) {
for (int t=; x; t=x,x=fa[x]) {
splay(x);ch[x][] = t;pushup(x);
}
}
void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= ;
}
void link(int x,int y) {
makeroot(x);
fa[x] = y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
ch[y][] = fa[ch[y][]] = ;
}
void update(int x,int y) {
makeroot(x);val[x] = y;pushup(x);
}
int query(int x,int y) {
makeroot(x);access(y);splay(y);
return sum[y];
}
int find(int x) {
access(x);splay(x);
while (ch[x][]) x = ch[x][];
return x;
}
int main() {
int n = read(),m = read(),opt,x,y;
for (int i=; i<=n; ++i) sum[i] = val[i] = read();
while (m--) {
opt = read(),x = read(),y = read();
if (opt==) printf("%d\n",query(x,y));
else if (opt==) {
if (find(x)!=find(y)) link(x,y);
}
else if (opt==) {
if (find(x)==find(y)) cut(x,y);
}
else update(x,y);
}
return ;
}

最新文章

  1. ReWriteDateControll
  2. eclipse的几个快捷键
  3. C语言复习(1)
  4. 【BZOJ2190】【SDOI2008】仪仗队
  5. MongoDB的安装与CRUD(JAVA)
  6. 四种java代码静态检查工具
  7. iOS之Xcode8 Auto Layout新特性
  8. Keil V4.72升级到V5.1X之后
  9. jni使用
  10. [Elasticsearch] 邻近匹配 (三) - 性能,关联单词查询以及Shingles
  11. Docker 核心技术之容器与镜像
  12. css居中flex
  13. android系统添加预置APP(so库自动释放)
  14. [nodemon] clean exit - waiting for changes before restart
  15. sqlserver 表操作 SQL篇
  16. Win10和Ubuntu双系统搭建详
  17. 文本tfidf
  18. DOS下读取spd信息的汇编程序(通过SMBus)
  19. 2017-2018 Exp7 网络欺诈技术防范 20155214
  20. Linux基础四(服务管理)

热门文章

  1. cairo-dock天气位置代码
  2. [LeetCode]7. Reverse Integer整数反转
  3. vue awaresome swiper的使用
  4. Java设计模式—装饰模式
  5. iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
  6. CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1
  7. 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑
  8. python 之开发工具 sublimetext 3
  9. Coursera 算法二 week2 Seam Carving
  10. 机器学习_线性回归和逻辑回归_案例实战:Python实现逻辑回归与梯度下降策略_项目实战:使用逻辑回归判断信用卡欺诈检测