1180: [CROATIAN2009]OTOCI

Time Limit: 50 Sec  Memory Limit: 162 MB
Submit: 1032  Solved: 638
[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

Sample Output

4
impossible
yes
6
yes
yes
15
yes
15
16

HINT

Source

分析:

LCT板子...

询问A到B的路径上的权值和就是把A变成根节点然后询问B到根节点路径的权值和...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std; const int maxn=30000+5; int n,m; char opt[13]; struct M{ int val,sum;
M *son[2],*father;
bool reverse; inline M(int v=0){
val=v,sum=v;
son[0]=son[1]=NULL;
father=NULL;
reverse=false;
} inline void update(void){
sum=val;
if(son[0]) sum+=son[0]->sum;
if(son[1]) sum+=son[1]->sum;
} inline bool isroot(void){
if(father==NULL)
return true;
if(father->son[0]==this)
return false;
if(father->son[1]==this)
return false;
return true;
} inline void pushdown(void){
if(reverse){
reverse=false;
swap(son[0],son[1]);
if(son[0]) son[0]->reverse^=true;
if(son[1]) son[1]->reverse^=true;
}
} }tr[maxn]; inline void connect(M *f,M *t,bool k){
if(t!=NULL) t->father=f;
if(f!=NULL) f->son[k]=t;
} inline void rotate(M *t){
M *f=t->father;
M *g=f->father;
bool s=(f->son[1]==t);
connect(f,t->son[!s],s);
connect(t,f,!s);
t->father=g;
if(g&&g->son[0]==f) g->son[0]=t;
if(g&&g->son[1]==f) g->son[1]=t;
f->update();
t->update();
} inline void push(M *t){
static M *stk[maxn];
int top=0;
stk[top++]=t;
while(!t->isroot())
stk[top++]=t=t->father;
while(top) stk[--top]->pushdown();
} inline void splay(M *t){
push(t);
while(!t->isroot()){
M *f=t->father;
M *g=f->father;
if(f->isroot())
rotate(t);
else{
bool a=(f&&f->son[1]==t);
bool b=(g&&g->son[1]==f);
if(a==b)
rotate(f),rotate(t);
else
rotate(t),rotate(t);
}
}
} inline void access(M *t){
M *p=NULL;
while(t!=NULL){
splay(t);
t->son[1]=p,t->update();
p=t,t=t->father;
}
} inline void makeroot(M *t){
access(t),splay(t),t->reverse^=true;
} inline void link(M *t,M *f){
makeroot(t),t->father=f;
} inline void cut(M *t){
splay(t);
if(t->son[0]) t->son[0]->father=NULL;
if(t->son[1]) t->son[1]->father=NULL;
t->son[0]=t->son[1]=NULL,t->update();
} inline M *find(M *t){
access(t);
splay(t);
M *r=t;
while(r->son[0])
r=r->son[0];
return r;
} signed main(void){
scanf("%d",&n);
for(int i=1,w;i<=n;i++)
scanf("%d",&w),tr[i]=M(w);
scanf("%d",&m);
for(int q=1,x,y;q<=m;q++){
scanf("%s%d%d",opt,&x,&y);
if(opt[0]=='b'){
if(find(tr+x)==find(tr+y))
puts("no");
else
puts("yes"),link(tr+x,tr+y);
}
else if(opt[0]=='p'){
access(tr+x),splay(tr+x);
tr[x].val=y,tr[x].update();
}
else{
if(find(tr+x)!=find(tr+y))
puts("impossible");
else
makeroot(tr+x),access(tr+y),splay(tr+y),
printf("%d\n",tr[y].sum);
}
}
return 0;
}

  


By NeighThorn

1180: [CROATIAN2009]OTOCI的更多相关文章

  1. BZOJ 1180: [CROATIAN2009]OTOCI [LCT]

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 961  Solved: 594[Submit][S ...

  2. BZOJ 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 989  Solved: 611[Submit][S ...

  3. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  4. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  5. 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...

  6. bzoj 1180: [CROATIAN2009]OTOCI【LCT】

    一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...

  7. BZOJ1180: [CROATIAN2009]OTOCI

    传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...

  8. [CROATIAN2009] OTOCI

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1180 [算法] 动态树维护森林连通性 时间复杂度 : O(NlogN ^ 2) [代 ...

  9. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

随机推荐

  1. php 递归

    function digui($data,$j=0,$lev=0){ $subs=array();//存放子孙数组 foreach ($data as $v){ if ($v['parent_id'] ...

  2. Mysql--7种Join查询

    0.sql的执行顺序 手写顺序 机读顺序 总结 ①From:对from左边的表和右边的表计算笛卡尔积,产生虚拟表c1 ②On:对c1中的数据进行on过滤,只有符合过滤条件的数据记录才会记录在虚拟表c2 ...

  3. Centos7在运行yum命令时出现报错及排查处理过程

    1.1  现象描述 Centos系统在正常重启后,运行yum命令安装软件工具的时候出现以下报错: cannot open Packages index using db5 - Structure ne ...

  4. 在基于vue-cli的项目自定义打包环境

    在工作当中,遇到了下面这个问题: 测试环境与生产环境中的用户权限不一样,因此,就需要根据测试环境打一个包,生产环境又打一个包.可是,如果每次打包都需要更改权限的配置文件的话,会很麻烦,而且,体现不出一 ...

  5. hprose 1.0(rpc 框架) - 关于跨域和P3P的声明

    private function sendHeader($context) { if ($this->onSendHeader !== null) { $sendHeader = $this-& ...

  6. Union found

    Use array. class UnionFound { public: vector<int> v; int cnt; UnionFound(int n) { v = vector&l ...

  7. Codeforces Round #460 (Div. 2)-C. Seat Arrangements

    C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  8. Patrick and Shopping

    Patrick and Shopping 今天 Patrick 等待着他的朋友 Spongebob 来他家玩.为了迎接 Spongebob,Patrick 需要去他家附近的两家商店  买一些吃的.他家 ...

  9. Linux命令之---which简单介绍

    命令简介 which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的 ...

  10. 大数据面试(HR电话了解)

    1什么是HA集群? 所谓HA,即高可用(7*24小时不中断服务) HA集群是hadoop高可用集群,即有两个namenode,一个active,一个stanby,active的name挂掉之后,sta ...