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. linux下避免僵尸进程的几种方法

    linux下我们可以调用fork函数创建子进程,创建的子进程将会得到父进程的数据空间.堆.栈......副本(采用写时复制机制),子进程将会继承父进程的信号掩码.信号处理方式.当前工作目录.会话id. ...

  2. Mycat高可用解决方案三(读写分离)

    Mycat高可用解决方案三(读写分离) 一.系统部署规划 名称 IP 主机名称 配置 192.168.199.112 mycat01 2核/2G Mysql主节点 192.168.199.110 my ...

  3. 读取properties的简单方法,使用@Configuration

    配置类代码如下 import org.springframework.beans.factory.annotation.Value; import org.springframework.contex ...

  4. http 实战练习

    http 实战练习 建立httpd服务器,要求提供两个基于名称的虚拟主机: (1)www.X.com,页面文件目录为/web/vhosts/x:错误日志为/var/log/httpd/x.err,访问 ...

  5. IE console报错

    需要注意的是,使用console对象查看对象信息,在IE8浏览器下未打开开发人员工具(F12)的情况下 会报'console'未定义错误. 解决办法:1.打开开发人员调试工具(F12)        ...

  6. 判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备)

    判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) // ===== 判断浏览器环境 ===== // // 判断是否是QQ环境 function isQQ() { retur ...

  7. HDU:2594-Simpsons’ Hidden Talents

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...

  8. CSS需要注意的问题1(转生活因拼搏而精彩的网易博客)

      1.检查HTML元素(如:<ul>.<div>).属性(如:class=”")是否有拼写错误.是否忘记结束标记(如:<br />) 因为Xhtml 语 ...

  9. TCP的运输连接管理

    TCP的运输连接管理 TCP是面向连接的协议,有三个阶段:连接建立.数据传送 和 连接释放.运输连接的管理就是使运输连接的简历和释放都能正常地进行. 在TCP连接建立过程中要解决一下三个问题: 1.  ...

  10. IOS架构

    iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统. 1,Core OS: 是用FreeBSD和Mach所改写的Darwin, 是开源.符合POSI ...