BZOJ3282: Tree (LCT模板)
Description
Input
Output
对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。
Sample Input
1
2
3
1 1 2
0 1 2
0 1 1
Sample Output
1
解题思路:
- #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 ;
- }
BZOJ3282: Tree (LCT模板)的更多相关文章
- [bzoj3282]Tree (lct)
昨天看了一天的lct..当然幸好最后看懂了(也许吧..) 论善良学长的重要性T_T,老司机带带我! 这题主要是删边的时候还要判断一下..蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa... 事实是 ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
- LCT模板
之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...
- HDU 5002 Tree LCT 区间更新
Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- Link-Cut Tree(LCT)&TopTree讲解
前言: Link-Cut Tree简称LCT是解决动态树问题的一种数据结构,可以说是我见过功能最强大的一种树上数据结构了.在此与大家分享一下LCT的学习笔记.提示:前置知识点需要树链剖分和splay. ...
- HDU5002 Tree(LCT)
今天做了一道LCT模板题之后忽然间好像记起来LCT的模板怎么用了,于是就把上次网络赛的一道LCT补一下.典型的删边,加边操作,还有路径加和路径set为一个数.维护的是路径第二大以及它有多少个,后来想想 ...
- LCT 模板及套路总结
这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
随机推荐
- iBatis框架使用 4步曲
iBatis是一款使用方便的数据訪问工具,也可作为数据持久层的框架.和ORM框架(如Hibernate)将数据库表直接映射为Java对象相比.iBatis是将SQL语句映射为Java对象. 相对于全自 ...
- 2.CString转换到char*
多字节模式下: CString -->char * CString str1 ="; char *t1 =str1.GetBuffer(str1.GetLength()); str1. ...
- android 客户端 Cookie处理
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). Cookie最早是网景公司的前雇员Lou Montul ...
- js 判断对touch事件支持
$.support = (function() { var support = { touch: !!(('ontouchstart' in window) || window.DocumentTou ...
- Linux PuTTY 更改字体
Linux PuTTY默认的字体比较小看着比较不舒服,Linux PuTTY的字体更改与Windows下的设置有所不同 1.Linux PuTTY默认的字体 ,Font used for ordina ...
- Python正则表达式初识(一)
首先跟大家简单唠叨两句为什么要学习正则表达式,为什么在网络爬虫的时候离不开正则表达式.正则表达式在处理字符串的时候扮演着非常重要的角色,在网络爬虫的时候也十分常用,大家可以把它学的简单一些,但是不能不 ...
- 使用form-create动态生成vue组件
使用form-create动态生成vue自定义组件和嵌套表单组件 [github] | [说明文档] 示例 let rule = [ { type:'row', children:[ { type:' ...
- 循环GridControl所有行
; i < gridView1.RowCount; i++) { DataRowView row = (DataRowView)gridView1.GetRow(i); } gridView1是 ...
- [Javascript] Compose multiple functions for new behavior in JavaScript
In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...
- poj2486--Apple Tree(树状dp)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...