bzoj 2843: 极地旅行社
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1077 Solved: 645
[Submit][Status][Discuss]
Description
Input
Output
对于每个bridge命令与excursion命令,输出一行,为题目描述所示。
Sample Input
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
impossible
yes
6
yes
yes
15
yes
15
16
/**************************************************************
Problem: 2843
User: JYYHH
Language: C++
Result: Accepted
Time:1584 ms
Memory:2124 kb
****************************************************************/ #include<bits/stdc++.h>
#define maxn 30005
using namespace std;
int ch[maxn][2],a[maxn];
int tot[maxn],tag[maxn];
int q[maxn],tp,n,m,f[maxn];
char s[11]; inline int get(int x){
return ch[f[x]][1]==x;
} inline int isroot(int x){
return (ch[f[x]][1]!=x&&ch[f[x]][0]!=x);
} inline void update(int x){
tot[x]=a[x]+tot[ch[x][0]]+tot[ch[x][1]];
} inline void pushdown(int x){
if(tag[x]){
tag[x]=0,swap(ch[x][0],ch[x][1]);
tag[ch[x][1]]^=1,tag[ch[x][0]]^=1;
}
} inline void rotate(int x){
int fa=f[x],ffa=f[fa],tp=get(x);
ch[fa][tp]=ch[x][tp^1],f[ch[fa][tp]]=fa;
ch[x][tp^1]=fa,f[fa]=x;
f[x]=ffa;
if(ch[ffa][1]==fa||ch[ffa][0]==fa) ch[ffa][ch[ffa][1]==fa]=x;
update(fa),update(x);
} inline void splay(int x){
for(int i=x;;i=f[i]){
q[++tp]=i;
if(isroot(i)) break;
} for(;tp;tp--) pushdown(q[tp]); for(;!isroot(x);rotate(x))
if(!isroot(f[x])) rotate(get(f[x])==get(x)?f[x]:x);
} inline void access(int x){
for(int t=0;x;t=x,x=f[x]) splay(x),ch[x][1]=t,update(x);
} inline void makeroot(int x){
access(x),splay(x),tag[x]^=1;
} inline int froot(int x){
access(x),splay(x);
while(ch[x][0]) x=ch[x][0];
return x;
} inline int link(int x,int y){
makeroot(x),f[x]=y;
} int main(){
int uu,vv;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",a+i);
scanf("%d",&m);
while(m--){
scanf("%s",s);
if(s[0]=='b'){
scanf("%d%d",&uu,&vv);
if(froot(uu)==froot(vv)) puts("no");
else link(uu,vv),puts("yes");
}
else if(s[0]=='p'){
scanf("%d%d",&uu,&vv);
splay(uu);
a[uu]=vv,update(uu);
}
else{
scanf("%d%d",&uu,&vv);
if(froot(uu)!=froot(vv)) puts("impossible");
else{
makeroot(vv),access(uu),splay(uu);
printf("%d\n",tot[uu]);
}
}
} return 0;
}
bzoj 2843: 极地旅行社的更多相关文章
- BZOJ 2843: 极地旅行社( LCT )
LCT.. ------------------------------------------------------------------------ #include<cstdio> ...
- bzoj 2843 极地旅行社(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2843 [题意] 给定一个森林,要求提供连边,修改点值,查询路径和的操作. [思路] L ...
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)
emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...
- 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...
- 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT
竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
- BZOJ2843: 极地旅行社
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 90 Solved: 56[Submit][Status] Descripti ...
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
随机推荐
- day06_04 购物车讲解02
1.0 补充知识 a,b = [2,3] print(a) print(b) #>>>2 #>>>3 a,b = (2,3) print(a) print(b) # ...
- Day3 UI:7种常用控件、4种基本布局
Android常用控件 TextView <TextView android:id="@+id/text_view" android:layout_width="m ...
- C 语言 进阶
清单狂魔,只挖坑不填坑.. 前言 最近经常被询问 C 语言 相关的问题,突然便也觉得需要思考一下 C 语言的进阶了. 我用 C 语言写过的最大的一个项目,也只是那个贪吃蛇,后来就断断续续地用 Pyth ...
- 二分查找树按照key值划分
#include <iostream>#include <vector>#include <algorithm>#include <string>#in ...
- SOA与WCF
背景: 高校平台马上就要进入编程阶段了,对于没怎么做过正式项目的我们来说,要学的东西实在太多了.一下子面对这么多学习资料时,我们也不能着急,还是踏踏实实,一个一个地去了解,其实他们都没那么神秘.这篇博 ...
- hadoop2.6.4【windows7】构建maven项目 系列2
准备windows版本的hadoop2.6.4 下载windows版本的hadoop2.6.4解压在本地 新建maven项目构建hadoop依赖 <?xml version="1.0& ...
- 【bzoj2506】calc 根号分治+STL-vector+二分+莫队算法
题目描述 给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个数. 输入 第一行两个正整数n ...
- 禅与园林艺术(garden)
禅与园林艺术(garden) 上了大学之后,小W和小Z一起报了一门水课,在做作业时遇到了问题. 有一个长度为nn的数列{ai},为一列树木的美观值. 现在有mm次询问,每次给出三个数l,r,p 询问对 ...
- Codeforces Round #504:D. Array Restoration
D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...
- Vijos P1007 绕钉子的长绳子
绕钉子的长绳子 背景 平面上有N个圆柱形的大钉子,半径都为R,所有钉子组成一个凸多边形. 现在你要用一条绳子把这些钉子围起来,绳子直径忽略不计. 描述 求出绳子的长度 格式 输入格式 第1行两个数:整 ...