bzoj 1180 OTOCI LCT 解题报告
来源:互联网 发布:淘宝网农村怎么加盟 编辑:程序博客网 时间:2024/06/08 09:11
Description
给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作: 1、bridge A B:询问结点A与结点B是否连通。如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。 2、penguins A X:将结点A对应的权值wA修改为X。 3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。否则输出结点A到结点B的路径上的点对应的权值的和。给出q个操作,要求在线处理所有操作。数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。
Input
第一行包含一个整数n(1<=n<=30000),表示节点的数目。第二行包含n个整数,第i个整数表示第i个节点初始时对应的权值。第三行包含一个整数q(1<=n<=300000),表示操作的数目。以下q行,每行包含一个操作,操作的类别见题目描述。任意时刻每个节点对应的权值都是1到1000的整数。
Output
输出所有bridge操作和excursion操作对应的输出,每个一行。
Sample Input
5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
Sample Output
4
impossible
yes
6
yes
yes
15
yes
15
16
思路
虽然这是裸的LCT题,但是作为一名蒟蒻,我还是没法很好的运用!
代码
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<vector>using namespace std;const int N=30000+5;int n,q,top;int c[N][2],fa[N],sum[N],v[N],st[N],rev[N];char ch[11];void update(int x){ int lf=c[x][0],rt=c[x][1]; sum[x]=sum[lf]+sum[rt]+v[x];}void pushdown(int x){ int lf=c[x][0],rt=c[x][1]; if (rev[x]) { rev[x]^=1;rev[lf]^=1;rev[rt]^=1; swap(c[x][0],c[x][1]); }}int isroot(int x){ if (c[fa[x]][0]==x||c[fa[x]][1]==x) return 0; else return 1;}void rotate(int &x){ int y=fa[x],z=fa[y],l,r; l=(c[y][1]==x);r=l^1; if(!isroot(y))c[z][c[z][1]==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 splay(int x){ top=0; st[++top]=x; for (int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i]; while(top) pushdown(st[top--]); while(!isroot(x)) { int y=fa[x],z=fa[y]; if (!isroot(y)) { if (c[y][0]==x^c[z][0]&&c[y][0]==y) rotate(x); else rotate(y); } rotate(x); }}void access(int x){ for (int t=0;x;t=x,x=fa[x]) splay(x),c[x][1]=t,update(x);}void makeroot(int x){ access(x);splay(x);rev[x]^=1;}void link(int x,int y){ makeroot(x);fa[x]=y;}int getrt(int x){ access(x);splay(x); while(c[x][0]) x=c[x][0]; return x;}int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&sum[i]); v[i]=sum[i]; } scanf("%d",&q); while(q--) { int x,y; scanf("%s",ch+1); scanf("%d%d",&x,&y); if (ch[1]=='b') { if (getrt(x)==getrt(y)) puts("no"); else puts("yes"),link(x,y); } if (ch[1]=='p') makeroot(x);v[x]=y;update(x); if (ch[1]=='e') { if (getrt(x)!=getrt(y)) puts("impossible"); else { makeroot(x);access(y);splay(y); printf("%d\n",sum[y]); } } } return 0;}
阅读全文
2 0
- bzoj 1180 OTOCI LCT 解题报告
- bzoj 1180 [CROATIAN2009]OTOCI - LCT
- 【BZOJ 1180】[CROATIAN2009]OTOCI LCT
- BZOJ 1180: [CROATIAN2009]OTOCI (LCT题解)
- BZOJ 1180 CROATIAN 2009 OTOCI/2843 极地旅行社 LCT
- 【BZOJ 1180】OTOCI【LCT】&【树链剖分+并查集】
- BZOJ 2049 [Sdoi 2008] LCT 解题报告
- bzoj-1180 OTOCI
- bzoj 1180: [CROATIAN2009]OTOCI
- BZOJ 1180: [CROATIAN2009]OTOCI
- BZOJ 1180: [CROATIAN2009]OTOCI
- 【SPOJ】 4155. OTOCI LCT
- 【bzoj1180】【CROATIAN2009】【OTOCI】【lct】
- [BZOJ1180][CROATIAN2009][LCT]OTOCI
- 【bzoj1180】OTOCI LCT
- bzoj1180: [CROATIAN2009]OTOCI(lct)
- BZOJ 1180 CROATIAN2009 OTOCI Link-Cut-Tree
- BZOJ 1180: [CROATIAN2009]OTOCI|动态树
- jsp页面查询附件并下载
- C语言创建单向链表之逆向建链
- bzoj 2730
- 顺序表、链表封装结构
- C++ STL-Vector基本原理
- bzoj 1180 OTOCI LCT 解题报告
- 关于input file按钮的美化
- 二分查找中的最大值的最小值问题
- GalaxyOJ-768 (重建图+SPFA)
- cygwin安装openssh服务
- display: inline-block
- POJ 1232 Ubiquitous Religions
- python文件压缩
- 医药公司的html和CSS文件1