bzoj2843&&1180
题解:
lct
和上一题差不多
这一题还要判断是否有链接
其实直接并查集判断就可以了
代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
const int N=;
using namespace std;
int read()
{
int x=;char ch=getchar();
for (;ch<''||ch>'';ch=getchar());
for (;ch>=''&&ch<='';ch=getchar())x=x*+ch-'';
return x;
}
int n,m,top,cnt,c[N][],fa[N],size[N],q[N],rev[N],f[N],sum[N],val[N],at[N],mt[N];
int isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void update(int x)
{
int l=c[x][],r=c[x][];
sum[x]=sum[l]+sum[r]+val[x];
size[x]=size[l]+size[r]+;
}
void pushdown(int x)
{
int l=c[x][],r=c[x][];
if (rev[x])
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=(c[y][]==x);r=l^;
if (!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
update(y);update(x);
}
void down(int x){if (!isroot(x))down(fa[x]);pushdown(x);}
void splay(int x)
{
down(x);
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))rotate((c[y][]==x)==(c[fa[y]][]==y)?y:x);
}
void access(int x)
{
for(int t=;x;t=x,x=fa[x])
{
splay(x);
c[x][]=t;
update(x);
}
}
void makeroot(int x){access(x);splay(x);rev[x]^=;}
void split(int x,int y){makeroot(y);access(x);splay(x);}
void link(int x,int y){makeroot(x);fa[x]=y;}
int find(int x){if (x==f[x])return f[x];return f[x]=find(f[x]);}
int main()
{
n=read();
for (int i=;i<=n;i++)val[i]=sum[i]=read(),f[i]=i,size[i]=;
m=read();char ch[];
while(m--)
{
scanf("%s",ch);
int u=read(),v=read();
if (ch[]=='b')
{
if (f[find(u)]==f[find(v)])puts("no");
else puts("yes"),link(u,v),f[find(u)]=f[find(v)];
}
if (ch[]=='p')
{
val[u]=v;
makeroot(u);
}
if (ch[]=='e')
{
if (f[find(u)]!=f[find(v)])
{
puts("impossible");
continue;
}
split(u,v);
printf("%d\n",sum[u]);
}
}
return ;
}
bzoj2843&&1180的更多相关文章
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Flex 1046: 找不到类型,或者它不是编译时常数;1180: 调用的方法 CompPropInfo 可能未定义
导入项目之后一直报这个错误, 1046: 找不到类型,或者它不是编译时常数: 1180: 调用的方法 CompPropInfo 可能未定义 想这应该是没有把当前这个类编译进项目当中,找了半天也没有找到 ...
- timus 1180. Stone Game 解题报告
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...
- Vijos 1180 (树形DP+背包)
题目链接: https://vijos.org/p/1180 题目大意:选课.只有根课选了才能选子课,给定选课数m, 问最大学分多少. 解题思路: 树形背包.cost=1. 且有个虚根0,取这个虚根也 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
- ural 1180 Stone Game
http://acm.timus.ru/problem.aspx?space=1&num=1180 #include <cstdio> #include <cstring&g ...
随机推荐
- Key Set---hud5363(快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363 #include <iostream> #include <cstdlib&g ...
- Python开发【项目】:博客后台
概述 通过自己写的博客后台代码.思路,来与武sir的代码进行一个差异化的比较,记录之间的差距,改善以后写代码的思路 博客后台这个项目,对之前Django学习的各个知识点都有涉及到,非常重要 用户登录验 ...
- mysql 大表优化
当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 转自:https://segmentfault.com/a/1190000006158186 单表优化 除非单表数据未 ...
- windos 查看指定端口,将指定进程杀死
>netstat -aon | findstr “80″ Proto Local Address Foreign Address State ...
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...
- [svc]ip地址划分
网络界有2个计算题,一个是子网掩码,另一个就是三次握手,四次回收序列号计算了. 学会如何划分等长子网 学会如何合并网段 学会ip是否能分配 理解特殊的ip地址 ip头部 ip地址分类 现在的IP网络使 ...
- [转] Delphi Socket Architecture
Delphi Socket Architecture - Felix John COLIBRI. abstract : The architecture of the ScktComp socket ...
- Thymeleaf使用说明
Thymeleaf使用说明 javascript操作: a.<script type="text/javascript" th:inline="javascript ...
- 587. Erect the Fence(凸包算法)
问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...
- mongoose 操作一直转圈
可能是:渲染时候 new content({ category:req.body.category, title:req.body.title, description:req.body.descri ...