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 ...
随机推荐
- winform对图片进行灰度处理
//图片进行灰度处理 //originalImage为原图像 返回灰度图像 private Bitmap GrayImage(Bitmap originalImage) { ImageAttribut ...
- 孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句
孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 从结构化编程流行以来,代码便被分块存储,称之为模块或库. 在pyt ...
- LeetCode-N皇后
LeetCode-N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. ...
- Python-map、filter、reduce方法
介绍 1.map()函数,会让列表中每一个元素都执行一某个函数(传递1个参数), 并且将执行函数返回的结果(无论是什么结果)放在结果列表中 2.filter()函数,会让列表中的每一个元素都执行一次某 ...
- django之HTTPResponse和JsonResponse详解
HttpResponse对象 Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数.那么视图函数在处理完相关的逻辑后,也需要返回一个响 ...
- python鉴黄程序
最近有客户向服务器上传了些垃圾图片,和正常图片混合在一起,大概有10W张的数量,在经历了大概3个小时翻了2000多张的时候,手指抽了下,感觉很不舒服,硬着头皮上,写个程序鉴别下吧,但是怎么搞呢,经过从 ...
- JavaWeb笔记(十)非关系型数据库Redis
Redis Redis是一款高性能的NOSQL系列的非关系型数据库 主流的NOSQL产品 键值(Key-Value)存储数据库 相关产品: Tokyo Cabinet/Tyrant.Redis.Vol ...
- log4j配置打印mybatis的sql到控制台(复制)
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender ...
- 软件架构---.net框架介绍
https://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生 ...
- npm & npm config
npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...