【BZOJ2049,2631,3282,1180】LCT模板四连A
好吧我并不想讲LCT
只是贴4个代码~
【BZOJ2049】[Sdoi2008]Cave 洞穴勘测
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
- using namespace std;
- const int maxn=10010;
- int n,m;
- struct NODE
- {
- int ch[2],fa,rev;
- }s[10010];
- char str[20];
- void pushdown(int x)
- {
- if(s[x].rev)
- {
- swap(s[x].ch[0],s[x].ch[1]);
- if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
- if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
- s[x].rev=0;
- }
- }
- void rotate(int x)
- {
- int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
- if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
- s[y].ch[d]=s[x].ch[d^1],s[y].fa=x,s[x].fa=z;
- if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
- s[x].ch[d^1]=y;
- }
- void pd(int x){if(!isr(x)) pd(s[x].fa); pushdown(x);}
- void splay(int x)
- {
- pd(x);
- while(!isr(x))
- {
- int y=s[x].fa,z=s[y].fa;
- if(!isr(y))
- {
- if((x==s[y].ch[1])^(y==s[z].ch[1])) rotate(x);
- else rotate(y);
- }
- rotate(x);
- }
- }
- void access(int x)
- {
- int y=0;
- while(x) splay(x),s[x].ch[1]=y,y=x,x=s[x].fa;
- }
- void maker(int x)
- {
- access(x),splay(x),s[x].rev^=1;
- }
- int findr(int x)
- {
- access(x),splay(x);
- while(s[x].ch[0]) pushdown(x),x=s[x].ch[0];
- return x;
- }
- void link(int x,int y)
- {
- maker(x),s[x].fa=y;
- }
- void cut(int x,int y)
- {
- maker(y),access(x),splay(x);
- s[x].ch[0]=s[y].fa=0;
- }
- int rd()
- {
- int ret=0; char gc=getchar();
- while(gc<'0'||gc>'9') gc=getchar();
- while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
- return ret;
- }
- int main()
- {
- n=rd(),m=rd();
- int i,a,b;
- for(i=1;i<=m;i++)
- {
- scanf("%s",str);
- a=rd(),b=rd();
- switch(str[0])
- {
- case 'D':cut(a,b); break;
- case 'C':link(a,b); break;
- case 'Q':if(findr(a)==findr(b)) printf("Yes\n");
- else printf("No\n");
- }
- }
- return 0;
- }
【BZOJ2631】tree
此题的下传标记实在是长,好在我把它写到结构体里了
据说此题必须用unsigned int,不明觉厉~
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
- #define mod 51061
- using namespace std;
- typedef unsigned int ui;
- struct node
- {
- ui ch[2],fa,rev;
- ui ts,tc,sum,siz,v;
- void C(ui x) {v=v*x%mod,sum=sum*x%mod,tc=tc*x%mod,ts=ts*x%mod;}
- void S(ui x) {v=(v+x)%mod,sum=(sum+siz*x)%mod,ts=(ts+x)%mod;}
- }s[100010];
- char str[10];
- ui n,m;
- void pushup(ui x)
- {
- s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
- s[x].sum=(s[s[x].ch[0]].sum+s[s[x].ch[1]].sum+s[x].v)%mod;
- }
- void pushdown(ui x)
- {
- if(s[x].rev)
- {
- swap(s[x].ch[0],s[x].ch[1]);
- if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
- if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
- s[x].rev=0;
- }
- if(s[x].tc!=1)
- {
- if(s[x].ch[0]) s[s[x].ch[0]].C(s[x].tc);
- if(s[x].ch[1]) s[s[x].ch[1]].C(s[x].tc);
- s[x].tc=1;
- }
- if(s[x].ts)
- {
- if(s[x].ch[0]) s[s[x].ch[0]].S(s[x].ts);
- if(s[x].ch[1]) s[s[x].ch[1]].S(s[x].ts);
- s[x].ts=0;
- }
- }
- void rotate(ui x)
- {
- ui y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
- if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
- s[y].ch[d]=s[x].ch[d^1],s[x].fa=z,s[y].fa=x;
- if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
- s[x].ch[d^1]=y;
- pushup(y),pushup(x);
- }
- void updata(ui x)
- {
- if(!isr(x)) updata(s[x].fa);
- pushdown(x);
- }
- void splay(ui x)
- {
- updata(x);
- while(!isr(x))
- {
- ui y=s[x].fa,z=s[y].fa;
- if(!isr(y))
- {
- if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
- else rotate(y);
- }
- rotate(x);
- }
- }
- void access(ui x)
- {
- ui y=0;
- while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
- }
- void maker(ui x)
- {
- access(x),splay(x),s[x].rev^=1;
- }
- void cut(ui x,ui y)
- {
- maker(y),access(x),splay(x);
- s[x].ch[0]=s[y].fa=0,pushup(x);
- }
- void link(ui x,ui y)
- {
- maker(y),s[y].fa=x;
- }
- ui rd()
- {
- ui ret=0; char gc=getchar();
- while(gc<'0'||gc>'9') gc=getchar();
- while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
- return ret;
- }
- int main()
- {
- n=rd(),m=rd();
- ui i,a,b,c,d;
- for(i=1;i<=n;i++) s[i].v=1;
- for(i=1;i<n;i++) link(rd(),rd());
- for(i=1;i<=m;i++)
- {
- scanf("%s",str),a=rd(),b=rd();
- switch(str[0])
- {
- case '*':c=rd(),maker(a),access(b),splay(b),s[b].C(c); break;
- case '+':c=rd(),maker(a),access(b),splay(b),s[b].S(c); break;
- case '-':c=rd(),d=rd(),cut(a,b),link(c,d); break;
- case '/':maker(a),access(b),splay(b),printf("%u\n",s[b].sum); break;
- }
- }
- return 0;
- }
【BZOJ3282】Tree
判断一下是否联通就好了
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
- using namespace std;
- int n,m;
- struct node
- {
- int ch[2],fa,sum,v,rev;
- }s[300010];
- void pushup(int x)
- {
- s[x].sum=s[s[x].ch[0]].sum^s[s[x].ch[1]].sum^s[x].v;
- }
- void pushdown(int x)
- {
- if(s[x].rev)
- {
- swap(s[x].ch[0],s[x].ch[1]);
- if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
- if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
- s[x].rev=0;
- }
- }
- void rotate(int x)
- {
- int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
- if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
- s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
- if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
- s[x].ch[d^1]=y;
- pushup(y),pushup(x);
- }
- void updata(int x)
- {
- if(!isr(x)) updata(s[x].fa);
- pushdown(x);
- }
- void splay(int x)
- {
- updata(x);
- while(!isr(x))
- {
- int y=s[x].fa,z=s[y].fa;
- if(!isr(y))
- {
- if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
- else rotate(y);
- }
- rotate(x);
- }
- }
- void access(int x)
- {
- int y=0;
- while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
- }
- void maker(int x)
- {
- access(x),splay(x),s[x].rev^=1;
- }
- void cut(int x,int y)
- {
- maker(x),access(y),splay(y);
- if(s[x].fa==y) s[x].fa=s[y].ch[0]=0,pushup(y);
- }
- void link(int x,int y)
- {
- maker(x),s[x].fa=y;
- }
- void split(int a,int b)
- {
- maker(a),access(b),splay(b);
- }
- int findr(int x)
- {
- access(x),splay(x);
- while(s[x].ch[0]) x=s[x].ch[0];
- return x;
- }
- int rd()
- {
- int ret=0; char gc=getchar();
- while(gc<'0'||gc>'9') gc=getchar();
- while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
- return ret;
- }
- int main()
- {
- n=rd(),m=rd();
- int i,a,b,c;
- for(i=1;i<=n;i++) s[i].sum=s[i].v=rd();
- for(i=1;i<=m;i++)
- {
- c=rd(),a=rd(),b=rd();
- switch(c)
- {
- case 0:split(a,b),printf("%d\n",s[b].sum); break;
- case 1:if(findr(a)!=findr(b)) link(a,b); break;
- case 2:cut(a,b); break;
- case 3:splay(a),s[a].v=b,pushup(a); break;
- }
- }
- return 0;
- }
[CROATIAN2009]OTOCI
我经常把ch[]数组开成int ch[0];不知道有谁跟我经常犯一样的错误。。。
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
- using namespace std;
- int n,m;
- struct node
- {
- int ch[2],fa,sum,v,rev;
- }s[30010];
- char str[20];
- void pushup(int x)
- {
- s[x].sum=s[s[x].ch[0]].sum+s[s[x].ch[1]].sum+s[x].v;
- }
- void pushdown(int x)
- {
- if(s[x].rev)
- {
- swap(s[x].ch[0],s[x].ch[1]);
- if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
- if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
- s[x].rev=0;
- }
- }
- void rotate(int x)
- {
- int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
- if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
- s[y].fa=x,s[x].fa=z,s[y].ch[d]=s[x].ch[d^1];
- if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
- s[x].ch[d^1]=y;
- pushup(y),pushup(x);
- }
- void updata(int x)
- {
- if(!isr(x)) updata(s[x].fa);
- pushdown(x);
- }
- void splay(int x)
- {
- updata(x);
- while(!isr(x))
- {
- int y=s[x].fa,z=s[y].fa;
- if(!isr(y))
- {
- if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
- else rotate(y);
- }
- rotate(x);
- }
- }
- void access(int x)
- {
- int y=0;
- while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
- }
- void maker(int x)
- {
- access(x),splay(x),s[x].rev^=1;
- }
- void link(int x,int y)
- {
- maker(x),s[x].fa=y;
- }
- int findr(int x)
- {
- access(x),splay(x);
- while(s[x].ch[0]) x=s[x].ch[0];
- return x;
- }
- void split(int x,int y)
- {
- maker(y),access(x),splay(x);
- }
- int rd()
- {
- int ret=0; char gc=getchar();
- while(gc<'0'||gc>'9') gc=getchar();
- while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
- return ret;
- }
- int main()
- {
- n=rd();
- int i,a,b,c;
- for(i=1;i<=n;i++) s[i].sum=s[i].v=rd();
- m=rd();
- for(i=1;i<=m;i++)
- {
- scanf("%s",str),a=rd(),b=rd();
- switch(str[0])
- {
- case 'b':if(findr(a)!=findr(b)) printf("yes\n"),link(a,b);
- else printf("no\n"); break;
- case 'p':splay(a),s[a].v=b,pushup(a); break;
- case 'e':if(findr(a)!=findr(b)) printf("impossible\n");
- else split(a,b),printf("%d\n",s[a].sum); break;
- }
- }
- return 0;
- }
【BZOJ2049,2631,3282,1180】LCT模板四连A的更多相关文章
- LCT模板
之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- LCT 模板及套路总结
这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- Xamarin XAML语言教程构建ControlTemplate控件模板 (四)
Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- BZOJ 1180 / 2843 LCT模板题_双倍经验
一大早上到机房想先拍一下模板,热热身. 结果....对照着染色敲的 LCT 竟然死活也调不过去(你说我抄都能抄错) 干脆自己重新敲了一遍,10min就敲完了....... 还是要相信自己 Code: ...
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
随机推荐
- machine_learning-knn算法具体解释(近邻算法)
近邻算法是机器学习算法中的入门算法,该算法用于针对已有数据集对未知数据进行分类. 该算法核心思想是通过计算预測数据与已有数据的相似度猜測结果. 举例: 如果有例如以下一组数据(在下面我们统一把该数据作 ...
- Spring注解@ResponseBody,@RequestBody
@RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象. @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMess ...
- openWRT学习之LUCI之中的一个helloworld演示样例
备注1:本文 讲述的是原生的openWRT环境下的LUCI 备注2:本文參考了诸多资料.感谢网友分享.參考资料: http://www.cnblogs.com/zmkeil/archive/2013/ ...
- ubuntu下安装自动补全YouCompleteMe
一.安装预备软件.#vim要带python2.7的支持,curl是下载插件必须用到的软件,还有git apt install vim-nox-py2 curl git #安装python头文件 apt ...
- SCUT入门-环境搭建
SCUT是一款基于C#且开源的游戏服务端框架,并且有一定的上线项目.最近正在入门中... 1.安装 去官网可以直接下载安装版:http://www.scutgame.com/ 源代码建议OSC Chi ...
- angularJs 页面定时刷新
angularJs 页面定时刷新 页面定时刷新并在页面离开时停止自动刷新 var autoRefresh; //自动刷新 autoRefresh = $interval($scope.loadData ...
- MySQL 5.7.16 zip包配置
截止2016/10/16 最新版本Mysql为5.7.16,之前写过一篇APMW搭建的文章(传送门:http://www.cnblogs.com/airoot/p/4131906.html)里面介绍的 ...
- ParagraphFormat 对象【精品】
ParagraphFormat 对象 贡献者:motolola 日期:2009-05-27 阅读:5261 回复: 相关标签:wps > API > paragraphfo ...
- [开机启动]Linux开机自启和运行级别
嵌入式系统中程序自启动方法 在很多嵌入式系统中,由于可用资源较少,常常在系统启动后就直接让应用程序自动启动,以减少用户操作和节省资源.如何让自己的应用程序自动启动呢? 在Linux系统中,配置应 ...
- 数据库 Proc编程二
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...