bzoj 1180: [CROATIAN2009]OTOCI
来源:互联网 发布:北邮网络教育登录系统 编辑:程序博客网 时间:2024/06/08 10:52
1180: [CROATIAN2009]OTOCI
Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 778 Solved: 479
[Submit][Status][Discuss]
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
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
impossible
yes
6
yes
yes
15
yes
15
16
HINT
Source
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define N 30003using namespace std;int n,m;int size[N],key[N],sum[N],ch[N][3],fa[N],top,st[N],rev[N];int isroot(int x){ return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}int get(int x){ return ch[fa[x]][1]==x;}void pushdown(int x){ if (!x) return; if (rev[x]) { rev[ch[x][0]]^=1; rev[ch[x][1]]^=1; rev[x]=0; swap(ch[x][0],ch[x][1]); }}void update(int x){ if (!x) return; size[x]=size[ch[x][0]]+size[ch[x][1]]+1; sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+key[x];}void rotate(int x){ int y=fa[x]; int z=fa[y]; int which=get(x); if (!isroot(y)) ch[z][ch[z][1]==y]=x; ch[y][which]=ch[x][which^1]; fa[ch[y][which]]=y; ch[x][which^1]=y; fa[y]=x; fa[x]=z; 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]; for (int i=top;i>=1;i--) pushdown(st[i]); while (!isroot(x)) { int y=fa[x]; if (!isroot(y)) rotate(get(y)==get(x)?y:x); rotate(x); }}void access(int x){ int t=0; while(x) { splay(x); ch[x][1]=t; update(x); t=x; x=fa[x]; }}void rever(int x){ access(x); splay(x); rev[x]^=1;}void link(int x,int y){ rever(x); fa[x]=y; splay(x); update(x);}int find(int x){ access(x); splay(x); while (ch[x][0]) x=ch[x][0]; return x;}void change(int x,int y){ key[x]=y; access(x); splay(x);}int main(){ freopen("a.in","r",stdin); scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&key[i]); scanf("%d",&m); for (int i=1;i<=m;i++) { char s[20]; int x,y; scanf("%s%d%d",s,&x,&y); if (s[0]=='e') { if (find(x)==find(y)) { rever(x); access(y); splay(y); printf("%d\n",sum[y]); } else printf("impossible\n"); } else if (s[0]=='b') { if (find(x)==find(y)) printf("no\n"); else link(x,y),printf("yes\n"); } else change(x,y); }}
0 0
- bzoj 1180: [CROATIAN2009]OTOCI
- BZOJ 1180: [CROATIAN2009]OTOCI
- BZOJ 1180: [CROATIAN2009]OTOCI
- bzoj 1180 [CROATIAN2009]OTOCI - LCT
- 【BZOJ 1180】[CROATIAN2009]OTOCI LCT
- BZOJ 1180 CROATIAN2009 OTOCI Link-Cut-Tree
- BZOJ 1180: [CROATIAN2009]OTOCI|动态树
- bzoj 1180: [CROATIAN2009]OTOCI link cut tree
- BZOJ 1180: [CROATIAN2009]OTOCI (LCT题解)
- BZOJ 1180 [CROATIAN2009]OTOCI Link Cut Trees
- 1180: [CROATIAN2009]OTOCI/2843: 极地旅行社
- BZOJ1180 [CROATIAN2009]OTOCI
- bzoj1180: [CROATIAN2009]OTOCI
- [BZOJ1180] [CROATIAN2009]OTOCI
- [BZOJ1180][CROATIAN2009]OTOCI
- 【bzoj1180】【CROATIAN2009】【OTOCI】【lct】
- 【bzoj1180】[CROATIAN2009]OTOCI
- bzoj1180: [CROATIAN2009]OTOCI
- 5-17 The World's Richest (25分)
- python 把文件中每列的第一个值作为字典中的key键值索引
- iOS系统键盘和自定义键盘的切换
- hdoj-1020-Encoding
- java中的抽象类
- bzoj 1180: [CROATIAN2009]OTOCI
- 网络时延的几个概念
- malloc原理和内存碎片
- 集合框架--ListIterator接口
- 338. Counting Bits
- 一些不错的WEB前端书籍
- .NET AOP (六)在编译阶段PostSharp
- ”伪集群“导致的Hibernate主键increment生成策略异常
- 编辑文章 - 博客频道 - CSDN.NET