只是想复健一下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. poj2773求第K个与m互质的数

    //半年前做的,如今回顾一下,还是有所收货的,数的唯一分解,.简单题. #include<iostream> #include<cstring> using namespace ...

  2. python学习之 - XML

    xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...

  3. git修改commit message及vi编辑器的简单使用

    1.修改commit信息 git commit --amend 2.进入vi编辑器修改 ‘i’进入insert模式,输入文字: ‘esc’回到命令模式,删除文字,移动光标: ‘:’进入底行模式,‘wq ...

  4. 最短路——Dijkstra算法

    模板 水模板ing #include <cstdio> #include <cstring> #include <algorithm> #include <i ...

  5. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  6. Maven新建webapp项目报错Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE

    Windows-Preferences 在搜索框输入maven,点击下面的Archetypes--->Add Remote Catalog... 对应输入 http://repo1.maven. ...

  7. Java中的字符

    以下内容引用自http://wiki.jikexueyuan.com/project/java/characters.html: 一般情况下,当处理字符时,使用的是原始数据类型char. 示例: ch ...

  8. OSChinaclient源代码学习(3)--轮询机制的实现

    主要以OSChina Androidclient源代码中Notice的轮询机制进行解读. 一.基础知识 一般IM(即使通讯)的实现有两种方式:推送和轮询,推送就是server主动向client发送消息 ...

  9. strstr-strcat实现

    strstr 调用格式 #include <string.h> char *strstr(const char *haystack, const char *needle); 功能说明 该 ...

  10. 纠结的链接——ln、ln -s、fs.symlink、require

    纠结的链接--ln.ln -s.fs.symlink.require 提交 我的留言 加载中 已留言 inode 我们首先来看看 linux 系统里面的一个重要概念:inode. 我们知道,文件存储在 ...