昨天看了一天的lct。。当然幸好最后看懂了(也许吧。。)

论善良学长的重要性T_T,老司机带带我!

这题主要是删边的时候还要判断一下。。蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa。。。

事实是只要两个点之间联通就能断开了,管它有没有边。。。。整个就一模板题。。

交上去后跑得很慢(记录类型的锅)。。但还是很短?(请自行无视过长变量名)

 #include<cstdio>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct zs{
int c[],fa,val,sum;
bool rev;
}tree[maxn];
int i,j,n,m,id,x,y;
int stack[maxn];
inline void update(int x){tree[x].sum=tree[tree[x].c[]].sum^tree[x].val^tree[tree[x].c[]].sum;
}
inline bool isroot(int x){return tree[tree[x].fa].c[]!=x&&tree[tree[x].fa].c[]!=x;
}
void rotate(int x){
int fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))tree[gfa].c[tree[gfa].c[]==fa]=x;
int l=tree[fa].c[]==x,r=l^;
tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
update(fa);update(x);
}
void pushdown(int x){
if(!tree[x].rev)return;
int l=tree[x].c[],r=tree[x].c[];
if(l)tree[l].rev^=;if(r)tree[r].rev^=;
swap(tree[x].c[],tree[x].c[]);tree[x].rev^=;
}
void splay(int x){
int top=,tmp=x;stack[++top]=x;
while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
while(top)pushdown(stack[top]),top--;
int fa,gfa;
while(!isroot(x)){
fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))
if((tree[gfa].c[]==fa)^(tree[fa].c[]==x))rotate(x);
else rotate(fa);
rotate(x);
}
}
void access(int x){
int son=;
while(x){
splay(x);tree[x].c[]=son;
update(x);
son=x;x=tree[x].fa;
}
}
void makeroot(int x){
access(x);splay(x);tree[x].rev^=;
}
void link(int x,int y){
makeroot(x);tree[x].fa=y;splay(x);
}
void cut(int x,int y){
makeroot(x);access(y);splay(y);tree[y].c[]=tree[x].fa=;
}
int query(int x,int y){
makeroot(x);access(y);splay(y);return tree[y].sum;
}
int getfa(int x){
access(x);splay(x);while(tree[x].c[])pushdown(x),x=tree[x].c[];splay(x);
return x;
}
void change(int x,int y){
makeroot(x);tree[x].val=y;update(x);
}
int main(){
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val;
while(m--){
scanf("%d%d%d",&id,&x,&y);
if(id==)printf("%d\n",query(x,y));
else if(id==){if(getfa(x)!=getfa(y))link(x,y);}
else if(id==){if(getfa(x)==getfa(y))cut(x,y);}
else if(id==)change(x,y);
}
return ;
}

b站上的lct怎么都是300大洋的世界。。

16.1.13:重写了一发

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=;
int ch[maxn][],fa[maxn],sum[maxn],num[maxn],st[maxn];
bool rev[maxn];
int i,j,n,m,x,y; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
}
char s[];
inline void outx(int x){
if(!x)putchar('');
register int len=;
while(x)s[++len]=x%,x/=;
while(len)putchar(s[len--]+);putchar('\n');
}
inline bool isrt(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline void upd(int x){sum[x]=sum[ch[x][]]^num[x]^sum[ch[x][]];}
inline void pushdown(int x){
if(!rev[x])return;
rev[x]=,swap(ch[x][],ch[x][]),rev[ch[x][]]^=,rev[ch[x][]]^=;
}
inline void rotate(int x){
int f=fa[x],gfa=fa[f],l=ch[f][]==x,r=l^;
if(!isrt(f))ch[gfa][ch[gfa][]==f]=x;
fa[x]=gfa,fa[f]=x,ch[f][l]=ch[x][r],ch[x][r]=f,fa[ch[f][l]]=f;
sum[x]=sum[f],upd(f);
}
void splay(int x){
int f,gfa;
for(st[st[]=]=f=x;!isrt(f);)st[++st[]]=(f=fa[f]);
while(st[])pushdown(st[st[]--]);
for(f=fa[x],gfa=fa[f];!isrt(x);rotate(x),f=fa[x],gfa=fa[f])
if(!isrt(f))rotate(((ch[f][]==x)^(ch[gfa][]==f))?x:f);
}
inline void access(int x){
for(int t=;x;t=x,x=fa[x])splay(x),ch[x][]=t,upd(x);
}
inline void makert(int x){
access(x),splay(x),rev[x]^=;
}
void cut(int x,int y){
makert(x),access(y),splay(y),ch[y][]=fa[x]=,upd(y);
}
void link(int x,int y){
makert(x),fa[x]=y;if(!(x&))splay(x);
}
inline int query(int x,int y){
makert(x),access(y),splay(y);
return sum[y];
}
inline int getfa(int x){
for(access(x),splay(x),pushdown(x);ch[x][];x=ch[x][]);
return x;
}
inline void change(int x,int v){
splay(x),num[x]=v,upd(x);
}
int main(){
n=read(),m=read();
for(i=;i<=n;i++)num[i]=read();int id;
while(m--){
id=read(),x=read(),y=read();
if(id==)outx(query(x,y));
if(id==)if(getfa(x)!=getfa(y))link(x,y);
if(id==)if(getfa(x)==getfa(y))cut(x,y);
if(id==)change(x,y);
}
return ;
}

3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB

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

[bzoj3282]Tree (lct)的更多相关文章

  1. BZOJ3282: Tree (LCT模板)

    Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...

  2. 【BZOJ3282】Tree LCT

    1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...

  3. BZOJ3282: Tree

    传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...

  4. BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)

    蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...

  5. HDU 5002 Tree LCT 区间更新

    Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  6. hdu5398 GCD Tree(lct)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud GCD Tree Time Limit: 5000/2500 MS (Java/O ...

  7. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  8. BZOJ 2631: tree( LCT )

    LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...

  9. Link-Cut Tree(LCT)&TopTree讲解

    前言: Link-Cut Tree简称LCT是解决动态树问题的一种数据结构,可以说是我见过功能最强大的一种树上数据结构了.在此与大家分享一下LCT的学习笔记.提示:前置知识点需要树链剖分和splay. ...

随机推荐

  1. JAVA 解析、编辑nginx.conf

    最近工程开发遇到一个需求:用Java去解析并编辑nginx.conf 在github上找到nginx-java-parser工具,项目地址:https://github.com/odiszapc/ng ...

  2. android 串口开发第一篇:搭建ndk开发环境以及第一个jni调用程序

    一:ndk环境搭建 1:开发环境 我使用的是android studio 2.3.3版本,搭建ndk开发环境比较简单,打开File----Settings----Appearance&Beha ...

  3. 如何检测mvc性能和sql语句

    mvc中使用linq如何检测sql语句 .net中使用mvc开发已经是一种趋势,不仅仅是.net ,java 等越来越多的开发者更倾向于mvc这种开发模式,在.net mvc 使用linq非常方便,各 ...

  4. iView的使用【CDN向】

    直接粗暴地上html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  5. [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]

    Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + su ...

  6. mysql TIMESTAMP与DATATIME的区别---转载加自己的看法

    from:http://lhdeyx.blog.163.com/blog/static/318196972011230113645715/ from:http://blog.csdn.NET/zht6 ...

  7. java 修饰符之修饰范围

    不同修饰符有不同修饰范围,为了对修饰符有更明确的认识,使用表格总结. 抽象\关键字 public protected private static final abstract default 类 √ ...

  8. ab返回结果参数分析

    Server Software    返回的第一次成功的服务器响应的HTTP头.Server Hostname    命令行中给出的域名或IP地址Server Port    命令行中给出端口.如果没 ...

  9. .bashrc:16: command not found: shopt配置环境变量时出错

    source .bashrc ------------------------------------------------------- .bashrc:: command not found: ...

  10. js选中文字兼容性解决

    function selectText(){ if(document.selection){ //ie return document.selection.createRange().text; } ...