bzoj 3159: 决战【LCT】
只是想复健一下LCT没想到做了不得了的题……调了两天QAQ
题解是这么说的:



但是果然还不太理解……因为swap的前后问题调了好久,(所以一开始养成的习惯后面就不要再改啦……
总之大概就是把对位置lct的操作映射到权值lct上,然后权值lct可以随便转没问题,只要位置lct不动就可以……
注意reverse!!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=50005;
int n,q,root,h[N],cnt;
char c[20];
struct qwe
{
int ne,to;
}e[N<<1];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
struct val
{
int c[N][2],f[N],tg[N],si[N];
long long mn[N],mx[N],sum[N],va[N];
bool re[N];
void rev(int x)
{
swap(c[x][0],c[x][1]);
re[x]^=1;
}
void init(int x,int v)
{
si[x]=1;
mn[x]=mx[x]=va[x]=sum[x]=v;
}
void add(int x,int v)
{//////cerr<<x<<" "<<v<<endl;
if(x)
{
sum[x]+=1ll*v*si[x];
tg[x]+=v;
mn[x]+=v;
mx[x]+=v;
va[x]+=v;
}
}
int fdrt(int x)
{////cerr<<"fdrt"<<x<<endl;
while(f[x])
x=f[x];//cerr<<x<<endl;
return x;
}
void ud(int x)
{
si[x]=1,mn[x]=mx[x]=sum[x]=va[x];
if(c[x][0])
{
si[x]+=si[c[x][0]];
mn[x]=min(mn[x],mn[c[x][0]]);
mx[x]=max(mx[x],mx[c[x][0]]);
sum[x]+=sum[c[x][0]];
}
if(c[x][1])
{
si[x]+=si[c[x][1]];
mn[x]=min(mn[x],mn[c[x][1]]);
mx[x]=max(mx[x],mx[c[x][1]]);
sum[x]+=sum[c[x][1]];
}
}
void pd(int x)
{
if(re[x])
{
rev(c[x][0]);
rev(c[x][1]);
re[x]=0;
}
if(tg[x])
{
add(c[x][0],tg[x]);
add(c[x][1],tg[x]);
tg[x]=0;
}
}
int ppd(int x)
{
int anc=srt(x)?x:ppd(f[x]);
pd(x);
return anc;
}
bool srt(int x)
{
return c[f[x]][0]!=x&&c[f[x]][1]!=x;
}
void zhuan(int x)
{
int y=f[x],z=f[y],l=c[y][0]!=x,r=l^1;
if(!srt(y))
c[z][c[z][0]!=y]=x;
f[x]=z;
c[y][l]=c[x][r];
f[c[x][r]]=y;
c[x][r]=y;
f[y]=x;
ud(y);
}
void splay(int x)
{
ppd(x);
while(!srt(x))
{
int y=f[x],z=f[y];
if(!srt(y))
{
if((c[y][0]==x)^(c[z][0]==y))
zhuan(x);
else
zhuan(y);
}
zhuan(x);
}
ud(x);
}
int zhao(int &x,int k)
{
while(1)
{
pd(x);
if(k<=si[c[x][0]])
x=c[x][0];
else if(k==si[c[x][0]]+1)
return x;
else
k-=(si[c[x][0]]+1),x=c[x][1];
}
}
}v;
struct LCT
{
int si[N],c[N][2],f[N],rt[N];
bool re[N];
void rev(int x)
{
swap(c[x][0],c[x][1]);
re[x]^=1;
}
void ud(int x)
{
si[x]=si[c[x][0]]+si[c[x][1]]+1;
}
void pd(int x)
{
if(re[x])
{
rev(c[x][0]);
rev(c[x][1]);
re[x]=0;
}
}
int ppd(int x)
{
int anc=srt(x)?x:ppd(f[x]);
pd(x);
return anc;
}
bool srt(int x)
{
return c[f[x]][0]!=x&&c[f[x]][1]!=x;
}
void zhuan(int x)
{
int y=f[x],z=f[y],l=c[y][0]!=x,r=l^1;
if(!srt(y))
c[z][c[z][0]!=y]=x;
f[x]=z;
c[y][l]=c[x][r];
f[c[x][r]]=y;
c[x][r]=y;
f[y]=x;
ud(y);
}
void splay(int x)
{
rt[x]=rt[ppd(x)];
while(!srt(x))
{////cerr<<x<<endl;
int y=f[x],z=f[y];
if(!srt(y))
{
if((c[y][0]==x)^(c[z][0]==y))
zhuan(x);
else
zhuan(y);
}
zhuan(x);
}
ud(x);
}
void acc(int x)
{////cerr<<"ACC"<<endl;
for(int y=0;x;y=x,x=f[x])
{//cerr<<x<<" "<<y<<endl;
splay(x);//cerr<<"accoksplay"<<endl;
int x2=v.fdrt(rt[x]),y2=v.fdrt(rt[y]);//cerr<<"accokflrt"<<endl;
if(!y)
y2=0;
v.zhao(x2,si[c[x][0]]+1);
v.splay(x2);
rt[x]=x2;
rt[c[x][1]]=v.c[x2][1];
v.f[v.c[x2][1]]=0;
v.c[x2][1]=y2;
v.f[y2]=x2;
v.ud(x2);
c[x][1]=y;
ud(x);
}
}
void mkrt(int x)
{
acc(x);//cerr<<"OKACC"<<endl;
splay(x);//cerr<<"oksplay"<<endl;
rev(x);
v.rev(rt[x]);
}
void dfs(int u,int fa)
{
rt[u]=u,si[u]=1,f[u]=fa;
v.init(u,0);
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fa)
dfs(e[i].to,u);
}
}w;
int main()
{
n=read(),q=read(),root=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y);
add(y,x);
}
w.dfs(root,0);
while(q--)
{
scanf("%s",c);
int x=read(),y=read();//cerr<<"read"<<endl;
w.mkrt(x);//cerr<<"mkrt"<<endl;
w.acc(y);//cerr<<"acc"<<endl;
int vy=v.fdrt(w.rt[y]);
if(c[2]=='c')
{
int z=read();
v.add(vy,z);
}
else if(c[2]=='m')
printf("%lld\n",v.sum[vy]);
else if(c[2]=='j')
printf("%lld\n",v.mx[vy]);
else if(c[2]=='n')
printf("%lld\n",v.mn[vy]);
else
v.rev(vy);
}
return 0;
}
/*
5 8 1
1 2
2 3
3 4
4 5
Sum 2 4
Increase 3 5 3
Minor 1 4
Sum 4 5
Invert 1 3
Major 1 2
Increase 1 5 2
Sum 1 5
*/
bzoj 3159: 决战【LCT】的更多相关文章
- BZOJ 3159: 决战 解题报告
BZOJ 3159: 决战 1 sec 512MB 题意: 给你一颗\(n\)个点,初始点权为\(0\)的有跟树,要求支持 Increase x y w 将路径\(x\)到\(y\)所有点点权加上\( ...
- BZOJ 3159决战
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3159 题意:给出一棵树,(1)路径加一个值:(2)路径上的节点的值反转(只是值反转,不是节 ...
- bzoj 3159: 决战
Description 树上链翻转,链加,查询链上的和/max/min 树链剖分套treap,修改查询可以用类似线段树的写法,翻转可以利用分裂合并和翻转标记实现,时间复杂度O(nlog2n) 实测除了 ...
- BZOJ.3510.首都(LCT 启发式合并 树的重心)
题目链接 BZOJ 洛谷 详见这. 求所有点到某个点距离和最短,即求树的重心.考虑如何动态维护. 两棵子树合并后的重心一定在两棵树的重心之间那条链上,所以在合并的时候用启发式合并,每合并一个点检查sz ...
- BZOJ 2959 长跑 (LCT+并查集)
题面:BZOJ传送门 当成有向边做的发现过不去样例,改成无向边就忘了原来的思路.. 因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和 这样保证了图是一颗森林 每次询问转化为, ...
- BZOJ 3306: 树 LCT + set 维护子树信息
可以作为 LCT 维护子树信息的模板,写的还是比较优美的. 本地可过,bzoj 时限太紧,一直 TLE #include<bits/stdc++.h> #define setIO(s) f ...
- BZOJ 3282: Tree( LCT )
LCT.. -------------------------------------------------------------------------------- #include<c ...
- BZOJ 2631: tree( LCT )
LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...
- bzoj 2157: 旅游 (LCT 边权)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec Memory Lim ...
随机推荐
- SQL SERVER 2012 第四章 连接 JOIN の INNER JOIN
所有JOIN语句的共同点是:将一个记录与另外一个或多个记录匹配,从而生成一个新记录,这个记录是由两个记录的合并列所产生的一个超集. 内部连接: 内部连接语法结构:SELECT <select l ...
- FatMouse's Speed--hdu1160(dp+输出路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- 寒武纪camp Day3
补题进度:9/10 A(多项式) 题意: 在一个长度为n=262144的环上,一个人站在0点上,每一秒钟有$\frac{1}{2}$的概率待在原地不动,有$\frac{1}{4}$的概率向前走一步,有 ...
- 附加数据库时,提示“Microsoft SQL Server,错误: 5120”, 解决方案
错误的提示内容为:
- javascript array-like object
http://www.nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/ ...
- 报错** is not accessible due to restriction on required library
报错: Description Resource Path Location TypeAccess restriction: The type Map<String,Object> is ...
- VUE组件如何与iframe通信问题
vue组件内嵌一个iframe,现在想要在iframe内获取父vue组件内信息,由于本人技术有限,采用的是H5新特性PostMessage来解决跨域问题. postMessage内涵两个API: on ...
- JSON序列化-化繁为简
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python爬虫开发【第1篇】【Scrapy入门】
Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...
- Android开发pool解析xml
xml在开发中的作用不可小觑,很多时候我们都要用到这种文件,所以学习它的解析方式很是必要. 我们都知道java中xml的解析有:dom,SAX,但是Android下我们使用pool解析,是更为方便,而 ...