bzoj2157 旅游——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2157
仍然是LCT模板题~
不过有一些需要注意的地方,点和边的区分,0号点的 mx 和 mn 等等;
还有变成相反数的处理,要像线段树一样先修改再下传标记,那么查询时候就不用先 pushdown 了;
不过1A还是极好的~
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=,inf=;
int n,m,fa[maxn],mx[maxn],mn[maxn],sum[maxn],w[maxn],c[maxn][],rev[maxn],tag[maxn],sta[maxn],top;
bool isroot(int x){return c[fa[x]][]!=x && c[fa[x]][]!=x;}
void pushup(int x)
{
int ls=c[x][],rs=c[x][];
sum[x]=sum[ls]+sum[rs]+w[x];
mx[x]=max(mx[ls],mx[rs]); mn[x]=min(mn[ls],mn[rs]);
if(x<=n)return;
mx[x]=max(mx[x],w[x]); mn[x]=min(mn[x],w[x]);
}
void tg(int x)
{
tag[x]^=;
w[x]=-w[x]; sum[x]=-sum[x];
swap(mx[x],mn[x]); mx[x]=-mx[x]; mn[x]=-mn[x];
}
void pushdown(int x)
{
int ls=c[x][],rs=c[x][];
if(rev[x])
{
rev[ls]^=; rev[rs]^=; rev[x]=;
swap(c[x][],c[x][]);
}
if(tag[x])
{
tag[x]=;
if(ls)tg(ls); if(rs)tg(rs);//
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
for(;top;top--)pushdown(sta[top]);
for(;!isroot(x);rotate(x))
{
int y=fa[x],z=fa[y];
if(isroot(y))continue;
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
}
}
void access(int x)
{
for(int t=;x;c[x][]=t,pushup(x),t=x,x=fa[x])splay(x);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
void link(int x,int y)
{
makeroot(x); fa[x]=y;
}
void query(int x,int y)
{
makeroot(x); access(y); splay(y);
}
void change(int x,int t)
{
makeroot(x); w[x]=t; pushup(x);
}
int main()
{
scanf("%d",&n);
mx[]=-inf; mn[]=inf;//
for(int i=,x,y,z;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z); x++; y++;
w[i+n]=z;
link(x,i+n); link(y,i+n);
}
scanf("%d",&m); char ch[];
for(int i=,x,y;i<=m;i++)
{
scanf("%s%d%d",&ch,&x,&y);
if(ch[]=='C')change(x+n,y);
else
{
x++; y++; query(x,y);
if(ch[]=='N')tg(y);
if(ch[]=='S')printf("%d\n",sum[y]);
if(ch[]=='A')printf("%d\n",mx[y]);
if(ch[]=='I')printf("%d\n",mn[y]);
}
}
return ;
}
bzoj2157 旅游——LCT的更多相关文章
- [bzoj2157]旅游 (lct)
这个应该也算裸的模板题吧..主要是边权的问题,对于每条边u->v,我们可以新建一个节点代替他,把边的信息弄到新的点上,就变成u->x->v了... 当然了这样的话要防止u和v这些没用 ...
- bzoj2157旅游
bzoj2157旅游 题意: 给定有权树,支持单边权修改,路径边权取相反数,路径边权求和,路径边权求最大最小值. 题解: 用link-cut tree link-cut tree与树链剖分有些类似,都 ...
- 【BZOJ2157】旅游 LCT
模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...
- BZOJ2157 旅游 【树剖 或 LCT】
题目 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径. ...
- [BZOJ2157]旅游(树链剖分/LCT)
树剖裸题,当然LCT也可以. 树剖: #include<cstdio> #include<algorithm> #define ls (x<<1) #define ...
- BZOJ2157: 旅游(LCT)
Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...
- bzoj 2157: 旅游 (LCT 边权)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec Memory Lim ...
- BZOJ2157: 旅游
传送门 先讲一个悲伤地故事 RunID User Problem Result Memory Time Language Code_Length Submit_Time 1635823 Cydiate ...
- BZOJ2157旅游——树链剖分+线段树
题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路 ...
随机推荐
- jupyter 教程
官网: http://jupyter.org/
- ELK搭建过程中出现的问题与解决方法汇总
搭建过程中出现的问题 elasticsearch启动过程中报错[1] ERROR: [1] bootstrap checks failed [1]: the default discovery set ...
- java连接mysql数据库中文乱码问题
private static final String URL="jdbc:mysql://localhost:3306/ziye?useUnicode=true&character ...
- Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~
1002 - Co ...
- 【ZJOI2017 Round1练习】D7T1 graph(提答)
题意: n<=1000 m<=10000 思路:
- Tyvj 1221 微子危机——战略
背景 №.3Summer联盟战前兵力战略转移. 描述 Summer的兵力分布在各个星球上,现在需要把他们全部转移到某个星球上.Summer一共拥有N个星球(1-N),你要把这N个星球上的兵力转到第M个 ...
- Ubuntu12.04之vi的问题
版本:ubuntu12.04. 问题:vi不能正常使用方向键与退格键. 原因:ubuntu系统自带的 vi 不完整导致. 解决方法:安装完整的vi,sudo apt-get install vim-g ...
- ***每天一个linux命令(5):rm 删除命令
昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...
- POJ2367 拓扑排序 裸题 板子题
http://poj.org/problem?id=2367 队列版 #include <stdio.h> #include <math.h> #include <str ...
- n个点中求任意两点组成斜率的最大值
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 首先按x坐标排序,然后相邻的三个点A,B,C 组成的三条直线必然有 ...