昨天看了一天的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. 使用Libmicrohttpd搭建内嵌(本地)服务器

    Libmicrohttpd简介 GNU Libmicrohttpd是一个用来在项目中内嵌http服务器的C语言库,它具有以下几个非常鲜明的特点: C语言库,小而快. API非常简单,且都是可重入的. ...

  2. 深谈auto变量

    1.c++中有一个关键字auto,c语言也有这么一个关键字,但是两者的意义大不相同. 2.c++中用auto定义的变量自动匹配赋值号右边的值的类型,具有自动匹配类型的作用,而c语言中auto只是声明一 ...

  3. k-近邻算法实例

    1. 简单例子 步骤 1.1 计算已知点和被求点的距离 1.2 按距离递增排序 1.3 求出距离最近的前k个点的类别最大值作为目标分类 from numpy import * import opera ...

  4. ArcGIS API for JavaScript 4.2学习笔记[22] 使用【QueryTask类】进行空间查询 / 弹窗样式

    上一篇写道,使用Query类进行查询featureLayer图层的要素,也简单介绍了QueryTask类的使用. 这一篇博文继续推进,使用Query类和QueryTask类进行空间查询,查询USA的著 ...

  5. ArcGIS API for JavaScript 4.2学习笔记[28] 可视域分析【使用Geoprocessor类】

    想知道可视域分析是什么,就得知道可视域是什么 我们站在某个地方,原地不动转一圈能看到的所有事物就叫可视域.当然平地就没什么所谓的可视域. 如果在山区呢?可视范围就会被山体挡住了.这个分析对军事上有十分 ...

  6. 2018第一发:记一次【Advanced Installer】打包之旅

    一.前言 2017年最后几天,你们都高高兴兴的跨年,博主还在加班制作.net安装包.因为年前要出来第一版的安装包,所以博主是加班加点啊.本来想用VS自带的制作工具,不过用过的人都知道,真是非常好(to ...

  7. iOS音频采集过程中的音效实现

    1.背景 在移动直播中, 声音是主播和观众互动的重要途径之一, 为了丰富直播的内容,大家都会想要在声音上做一些文章, 在采集录音的基础上玩一些花样. 比如演唱类的直播间中, 主播伴随着背景音乐演唱. ...

  8. 我的第一个python web开发框架(19)——产品发布相关事项

    好不容易小白将系统开发完成,对于发布到服务器端并没有什么经验,于是在下班后又找到老菜. 小白:老大,不好意思又要麻烦你了,项目已经弄完,但要发布上线我还一头雾水,有空帮我讲解一下吗? 老菜:嗯,系统上 ...

  9. Dubbo(二) 认识Zookeeper

    前言 在昨天,我们给大家基本介绍了Dubbo,文中反复提到了Zookeeper,那么它到底是什么呢,这篇文章我们将从Dubbo层面去了解Zookeeper,不做全面讲解,毕竟这是Dubbo教程啊~ Z ...

  10. 操作Frame和IFrame中页面元素

    HTML <iframe> 标签 定义:iframe 元素会创建包含另外一个文档的内联框架(即行内框架). frame标签有frameset.frame.iframe三种,frameset ...