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 ...
随机推荐
- 一个初学者的辛酸路程-初识Django
前言: 主要是关于JavaScript的高级部分以及Django 主要内容: 一.jQuery 事件绑定: DOM事件绑定: -直接在标签上绑定 第一种: $('.title').click(func ...
- hp raid配置
http://www.cnblogs.com/zhangxinglong/p/5585139.html [root@192e168e100e27 ~]# fdisk -l Disk /dev/nvme ...
- 聊聊、SpringBoot 上传文件大小
#2.0#spring.servlet.multipart.max-file-size=10Mb#spring.servlet.multipart.max-request-size=10Mb #1.3 ...
- NodeJs02 美女爬虫
note: demo代码要编号 导出模块 一个js文件就是一个模块,模块内部的所有变量,对象,方法对外界都不可见.如果想暴漏出去让别人用,就需要导出模块.语法如下: module.exports = ...
- Alpha 冲刺(5/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- 【bzoj3894】文理分科 网络流最小割
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用 ...
- 关于each()、find()、filter()遍历节点的操作方法
关于each().find().filter()遍历节点的操作方法 each语法: each(fn) ; 返回值:jQuery fn:代表对于每个匹配元素所要执行的函数 each()方法共有三种形式 ...
- C#操作windows事件日志项
/// <summary> /// 指定事件日志项的事件类型 /// </summary> public enum EventLogLevel { /// <summar ...
- Python数据结构——栈
栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种后入先出(LIFO,last-in-first-out)的数据结构. 由于栈具有后入先出的特点,所以任何不在栈顶的元素 ...
- 安装python 科学计算库
http://www.softpedia.com/get/Programming/Other-Programming-Files/Python-x-y.shtml Pythonxy兴趣小组QQ群237 ...