只是想复健一下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】的更多相关文章

  1. BZOJ 3159: 决战 解题报告

    BZOJ 3159: 决战 1 sec 512MB 题意: 给你一颗\(n\)个点,初始点权为\(0\)的有跟树,要求支持 Increase x y w 将路径\(x\)到\(y\)所有点点权加上\( ...

  2. BZOJ 3159决战

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3159 题意:给出一棵树,(1)路径加一个值:(2)路径上的节点的值反转(只是值反转,不是节 ...

  3. bzoj 3159: 决战

    Description 树上链翻转,链加,查询链上的和/max/min 树链剖分套treap,修改查询可以用类似线段树的写法,翻转可以利用分裂合并和翻转标记实现,时间复杂度O(nlog2n) 实测除了 ...

  4. BZOJ.3510.首都(LCT 启发式合并 树的重心)

    题目链接 BZOJ 洛谷 详见这. 求所有点到某个点距离和最短,即求树的重心.考虑如何动态维护. 两棵子树合并后的重心一定在两棵树的重心之间那条链上,所以在合并的时候用启发式合并,每合并一个点检查sz ...

  5. BZOJ 2959 长跑 (LCT+并查集)

    题面:BZOJ传送门 当成有向边做的发现过不去样例,改成无向边就忘了原来的思路.. 因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和 这样保证了图是一颗森林 每次询问转化为, ...

  6. BZOJ 3306: 树 LCT + set 维护子树信息

    可以作为 LCT 维护子树信息的模板,写的还是比较优美的. 本地可过,bzoj 时限太紧,一直 TLE #include<bits/stdc++.h> #define setIO(s) f ...

  7. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  8. BZOJ 2631: tree( LCT )

    LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...

  9. bzoj 2157: 旅游 (LCT 边权)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec  Memory Lim ...

随机推荐

  1. Object-C Xcode 编译提示 note: please rebuild precompiled header ZWYLPrefixHeader

    错误提示如下图 解决思路: 由于手欠不小心,在.pch文件上的第一行加了几个文字,删除以后,还有一个空行,估计是这个空行引起的.删除这个空行,就好了.

  2. js删除数组对象中符合条件的数据

    var data = [{}, {}, {}, {Id:1}] var datawilldele = [];//2,4,5 data.forEach(function (v, i,arry) { if ...

  3. 2017多校Round2(hdu6045~hdu6055)

    补题进度:10/11 1001(不等式) 根据题意列不等式,解一解就行了 1002(套路) 题意: 给定一个随机产生的1e6*1e6的矩阵和一个1e3*1e3的矩阵,你要回答这个1e3*1e3的小矩阵 ...

  4. linux的shell的until循环举例说明

    执行脚本: sh login.sh user,其中user为第一个参数 如下所示,如果用户‘user’登录,'who | grep "$1"'为true,until循环结束,程序继 ...

  5. topcoder srm 550

    div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...

  6. BZOJ 1005 明明的烦恼 Prufer序列+组合数学+高精度

    题目大意:给定一棵n个节点的树的节点的度数.当中一些度数无限制,求能够生成多少种树 Prufer序列 把一棵树进行下面操作: 1.找到编号最小的叶节点.删除这个节点,然后与这个叶节点相连的点计入序列 ...

  7. c# 自定义Base16编码解码

               一.自定义Base16编码原理                  Base16编码跟Base64编码原理上有点不同,当然前面转换是一样的,都是是将输入的字符串根据默认编码转换成一 ...

  8. 在CentOS上把PHP从5.4升级到5.5

    在CentOS上把PHP从5.4升级到5.5 摘要:本文记录了在CentOS 6.3上,把PHP从5.4.8升级到5.5.13的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的一系列系统 ...

  9. 关于集成支付宝SDK的开发

    下载 首先,你要想找到这个SDK,都得费点功夫.如今的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 "请点此下载集成开发包" Baidu和Googlep排在前面的支付宝开 ...

  10. 2016/05/06 Sublime Text 3 常用插件以及安装方法(转)

    http://www.cnsecer.com/460.html 安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧, ...