BZOJ_3282_Tree_LCT

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维护一下点权的xor和,单点修改直接暴力。
修改的时候应该是不用access,但我加上access操作会快一些
 
代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5. #define N 300050
  6. #define ls ch[p][0]
  7. #define rs ch[p][1]
  8. #define get(x) (ch[f[x]][1]==x)
  9. int ch[N][2],f[N],sum[N],n,m,val[N],rev[N];
  10. inline bool isrt(int p) {
  11. return ch[f[p]][1]!=p&&ch[f[p]][0]!=p;
  12. }
  13. inline void pushdown(int p) {
  14. if(rev[p]) {
  15. swap(ch[ls][0],ch[ls][1]);
  16. swap(ch[rs][0],ch[rs][1]);
  17. rev[ls]^=1; rev[rs]^=1;
  18. rev[p]=0;
  19. }
  20. }
  21. inline void pushup(int p) {
  22. sum[p]=sum[ls]^sum[rs]^val[p];
  23. }
  24. inline void update(int p) {
  25. if(!isrt(p)) update(f[p]);
  26. pushdown(p);
  27. }
  28. void rotate(int x) {
  29. int y=f[x],z=f[y],k=get(x);
  30. if(!isrt(y)) ch[z][ch[z][1]==y]=x;
  31. ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
  32. ch[x][!k]=y; f[y]=x; f[x]=z;
  33. pushup(y); pushup(x);
  34. }
  35. void splay(int x) {
  36. update(x);
  37. for(int fa;fa=f[x],!isrt(x);rotate(x))
  38. if(!isrt(fa))
  39. rotate(get(fa)==get(x)?fa:x);
  40. }
  41. void access(int p) {
  42. int t=0;
  43. while(p) splay(p),rs=t,pushup(p),t=p,p=f[p];
  44. }
  45. void makeroot(int p) {
  46. access(p); splay(p);
  47. swap(ls,rs); rev[p]^=1;
  48. }
  49. void link(int x,int p) {
  50. makeroot(x); f[x]=p;
  51. }
  52. void cut(int x,int p) {
  53. makeroot(x); access(p); splay(p); ls=f[x]=0;
  54. }
  55. int find(int p) {
  56. access(p); splay(p);
  57. while(ls) pushdown(p),p=ls;
  58. return p;
  59. }
  60. void fix(int x,int v) {
  61. /*access(x);*/ splay(x); sum[x]^=val[x]; val[x]=v; sum[x]^=val[x];
  62. }
  63. int main() {
  64. scanf("%d%d",&n,&m);
  65. int i,x,y,opt;
  66. for(i=1;i<=n;i++) scanf("%d",&val[i]);
  67. for(i=1;i<=m;i++) {
  68. scanf("%d%d%d",&opt,&x,&y);
  69. if(opt==0) {
  70. makeroot(x); access(y); splay(y);
  71. printf("%d\n",sum[y]);
  72. }else if(opt==1) {
  73. int t1=find(x),t2=find(y);
  74. if(t1!=t2) link(x,y);
  75. }else if(opt==2) {
  76. int t1=find(x),t2=find(y);
  77. if(t1==t2) cut(x,y);
  78. }else {
  79. fix(x,y);
  80. }
  81. }
  82. }

BZOJ_3282_Tree_LCT的更多相关文章

随机推荐

  1. Java内存模型_重排序

    重排序:是指编译器和处理器为了优化程序性能而对指令序列进行重新排序的一种手段 1..编译器优化的重排序.编译器在不改变单线程程序语义的前提下,可以重新安排语句的执行顺序. 2..指令级并行的重排序.现 ...

  2. 安装centOS后要解决的问题

    1,检查是否联网 ping www.baidu.com 未显示: 则表示网络未连接 首先关闭防火墙 sudo systemctl stop firewalld.service #停止firewall ...

  3. django-debug-toolbar的配置以及使用

    django-debug-toolbar django,web开中,用django-debug-toolbar来调试请求的接口,无疑是完美至极.   可能本人,见识博浅,才说完美至极, 大神,表喷,抱 ...

  4. ssh优缺点

    面试归来 技术面试官叫我谈谈 ssh优缺点 平时用起来倒是挺顺手..但是从来没有系统的总结过..导致很多点会都没有说出来.. 这次我认真总结了一下... 常说的好处 开源 常说的坏处 配置文件过大我就 ...

  5. Aop实现SqlSugar自动事务

    http://www.cnblogs.com/jaycewu/p/7733114.html

  6. dp,px,pt,sp 的区别 以及dp 和 px 互转

    dp = dip : device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...

  7. java基本数据类型及其包装类

    1.String类 String s1 = "hello world"; String s2 = "hello world"; String s3 = s1 + ...

  8. ASP.NET MVC中的路由IRouteConstraint方法应用实例

    在如下代码的写法中: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { ro ...

  9. SQL Server性能优化(8)堆表结构介绍

    一.表结构综述 下图是SQL Server中表的组织形式(其中分区1.分区2是为了便于管理,把表进行分区,放到不同的硬盘数据文件里.默认情况下,表只有一个分区.).表在硬盘上的存放形式,有堆和B树两种 ...

  10. 对C#热更新方案ILRuntime的探究

    转载请标明出处:http://www.cnblogs.com/zblade/ 对于游戏中的热更,目前主流的解决方案,分为Lua(ulua/slua/xlua/tolua)系和ILRuntime代表的c ...