好吧我并不想讲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的更多相关文章

  1. LCT模板

    之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...

  2. [BZOJ - 2631] tree 【LCT】

    题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...

  3. LCT 模板及套路总结

    这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...

  4. BZOJ2049[Sdoi2008]洞穴勘测——LCT

    题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...

  5. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...

  6. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. BZOJ 1180 / 2843 LCT模板题_双倍经验

    一大早上到机房想先拍一下模板,热热身. 结果....对照着染色敲的 LCT 竟然死活也调不过去(你说我抄都能抄错) 干脆自己重新敲了一遍,10min就敲完了....... 还是要相信自己 Code: ...

  9. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9705  Solved: 4674[Submit] ...

随机推荐

  1. java中long型时间戳的计算

    计算时间的时候碰到的问题: Date d = new Date(); long currtime = d.getTime(); //获取当前时间 long starttime = currtime - ...

  2. jenkins插件使用小结

    jenkins官网:https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project jenkins插件: 1.AnsiC ...

  3. 推荐一个入门最佳Git教程

    这是我最近发现的一个针对入门Git教程,浅显易懂,点到为止,很适合初学者及使用Git的爱好者,学完该教程应付开发工作绰绰有余. http://www.liaoxuefeng.com/wiki/0013 ...

  4. Centos Java 从1.7升级为1.8

    查看安装的sdk版本 yum list installed |grep java 卸载JDK相关文件输入和tzdata-java输入 yum -y remove java-1.7.0-openjdk* ...

  5. 每日英语:Yahoo's Rally: Made in China

    The typical honeymoon doesn't last too long before the hard work of marriage begins. And so it norma ...

  6. js基本知识1

    Javascript 作用 1. 网页特效 2. 用户交互 3. 表单验证 Js 就是可以用来控制 结构 和 样式 . 1.2 体验js 认识常用的三个输出语句. 都属于 js 内置对象 . 大家买手 ...

  7. line: 1: Syntax error: word unexpected (expecting ")")

    开发板上运行可执行程序报出错误: line1: 1: Syntax error: word unexpected (expecting ")") 解决思路: 1.编译器的问题 用a ...

  8. CentOS 6.2下更新GCC

    在centos中安装了QT5.1.0,发现无法启动,原因好像是GCC版本问题,所以就更新GCC. 1.下载GCC源码: 下载网址:http://ftp.gnu.org/gnu/gcc/gcc-4.8. ...

  9. Lucene:基于Java的全文检索引擎简介 (zhuan)

    http://www.chedong.com/tech/lucene.html ********************************************** Lucene是一个基于Ja ...

  10. java 远程调试 remote java application

    1.在本地eclipse中,打开debug configuration,在弹出的窗口中,点击左边的remote java application. 2.在右边的窗口中,输入项目名称.远程主机的地址和端 ...