bzoj2843极地旅行社

题意:

一些点,每个点有一个权值。有三种操作:点与点连边,单点修改权值,求两点之间路径上点的权值和(需要判输入是否合法)

题解:

以前一直想不通为什么神犇们的模板中LCT在link和cut后都要在根节点打翻转标记。现在明白了,因为只有这样才能保持深度的正确性,以前没有因此炸过是因为我以前都是把LCT拿来当链剖用的,根本不用link和cut~~这道题是LCT模板题也没什么好说的。不过CCZ大爷有更快的做法,就是离线读入所有连边操作,然后建一棵树用链剖,判断输入是否合法就在线用并查集查询与维护。orzCCZ!

代码:

 #include <cstdio>
#include <algorithm>
#include <cstring>
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define maxn 50000
using namespace std; int fa[maxn],ch[maxn][],sm[maxn],v[maxn];bool rev[maxn];
inline void update(int x){
if(x==)return; sm[x]=v[x];
if(ch[x][])sm[x]+=sm[ch[x][]]; if(ch[x][])sm[x]+=sm[ch[x][]];
}
inline bool is_root(int x){
if(x==||fa[x]==)return ;
return x!=ch[fa[x]][]&&x!=ch[fa[x]][];
}
inline void pushdown(int x){
if(rev[x]){
swap(ch[x][],ch[x][]); if(ch[x][])rev[ch[x][]]^=; if(ch[x][])rev[ch[x][]]^=; rev[x]^=;
}
}
void rotate(int x){
if(x==||is_root(x))return;
int a1=fa[x],a2=fa[fa[x]],a3; bool b1=(x==ch[a1][]),b2=(a1==ch[a2][]),b3=is_root(a1); a3=ch[x][!b1];
if(!b3)ch[a2][b2]=x; fa[x]=a2; ch[a1][b1]=a3; if(a3)fa[a3]=a1; ch[x][!b1]=a1; fa[a1]=x;
update(a1); update(x); if(!b3)update(a2);
}
int dt[maxn],dts,y;
void splay(int x){
if(x==)return; dts=; y=x; while(! is_root(y))dt[++dts]=y,y=fa[y];
dt[++dts]=y; while(dts)pushdown(dt[dts]),dts--;
while(!is_root(x)){
if(!is_root(fa[x]))((x==ch[fa[x]][])^(fa[x]==ch[fa[fa[x]]][]))?rotate(x):rotate(fa[x]);
rotate(x);
}
}
int access(int x){
if(x==)return ; int t=;
while(x){splay(x); ch[x][]=t; update(x); if(t)fa[t]=x; t=x; x=fa[x];}
return t;
}
bool link(int x,int y){
if(x==||y==)return ; access(x); splay(x); rev[x]^=; fa[x]=y; return ;
}
int querysum(int x,int y){
if(x==||y==)return ; if(x==y)return v[x]; access(x); int a=access(y); splay(x);
if(x==a)return v[a]+sm[ch[a][]];else return sm[x]+v[a]+sm[ch[a][]];
}
void change(int x,int y){
splay(x); v[x]=y; update(x);
}
int find(int x){
access(x); splay(x); while(ch[x][])x=ch[x][]; return x;
}
int n,m; char s[];
int main(){
//freopen("test.txt","r",stdin);
scanf("%d",&n); inc(i,,n)scanf("%d",&v[i]),fa[i]=,ch[i][]=ch[i][]=rev[i]=,sm[i]=v[i]; scanf("%d",&m);
inc(i,,m){
int a,b; scanf("%s%d%d",s,&a,&b);
if(s[]=='b'){if(find(a)==find(b))puts("no");else link(a,b),puts("yes");}
if(s[]=='p')change(a,b);
if(s[]=='e')if(find(a)!=find(b))puts("impossible");else printf("%d\n",querysum(a,b));
}
return ;
}

20160420

bzoj2843极地旅行社的更多相关文章

  1. BZOJ2843: 极地旅行社

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 56[Submit][Status] Descripti ...

  2. BZOJ2843 极地旅行社 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...

  3. BZOJ2843——极地旅行社

    1.题目大意:动态树问题,点修改,链查询.另外说明双倍经验题=bzoj1180 2.分析:lct模板题,练手的 #include <stack> #include <cstdio&g ...

  4. BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作:  1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...

  5. [BZOJ2843] 极地旅行社(LCT)

    传送门 模板. ——代码 #include <cstdio> #include <iostream> #define N 300001 #define get(x) (son[ ...

  6. bzoj2843极地旅行社题解

    题目大意 有n座小岛,当中每一个岛都有若干帝企鹅. 一開始岛与岛之间互不相连.有m个操作.各自是在两个岛之间修一座双向桥,若两岛已连通则不修并输出no,若不连通就输出yes并修建.改动一个岛上帝企鹅的 ...

  7. [bzoj2843&&bzoj1180]极地旅行社 (lct)

    双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...

  8. 【BZOJ2843】极地旅行社(Link-Cut Tree)

    [BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...

  9. 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组

    [BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...

随机推荐

  1. sorted排序

    sorted(['bob', 'about', 'Zoo', 'Credit']) # ['Credit', 'Zoo', 'about', 'bob'] ''' 默认情况下,对字符串排序,是按照AS ...

  2. cb21a_c++_string对象的比较

    *cb21a_c++_string对象的比较s.compare(s2)--区分大小的s.compare(pos1,n1,s2)s.compare(pos1,n1,s2,pos2,n2)s.compar ...

  3. 富文本插件tinymce初始化配置参数说明

    { language: _this.language, // 显示语种 selector: #${_this.tinymceId}, // 容器的id height: _this.height, // ...

  4. 强大的IntelliJ IDEA怎么破解?

    IntelliJ IDEA是非常好用的一个开发工具,怎么样才可以破解也是非常关键的问题,本文简单介绍破解方法. 第一种方式,我们进入以下网站http://idea.lanyus.com/ 这里要注意一 ...

  5. disruptor架构三 使用场景 使用WorkHandler和BatchEventProcessor辅助创建消费者

    在helloWorld的实例中,我们创建Disruptor实例,然后调用getRingBuffer方法去获取RingBuffer,其实在很多时候,我们可以直接使用RingBuffer,以及其他的API ...

  6. Python3-pymysql模块-数据库操作之MySQL

    博客转载 http://www.cnblogs.com/alex3714/articles/5950372.html 代码示例 import pymysql conn = None cursor = ...

  7. CListCtrl 控件即使跟新数据,即时刷新以及属性设置

    用 m_CtrItem.Update( i );来即使跟新每行的数据,因为有时用某些函数如SetItemText()来设置某一行一列的数据是,控件上面的显示数据没有即使跟新,这是就有update来跟新 ...

  8. UID,GID,口令

    摘自:http://cn.linux.vbird.org/linux_basic/0410accountmanager_1.php (完)

  9. 正确卸载vs2015及以前版本方式

    官网工具:https://github.com/Microsoft/VisualStudioUninstaller/releases 亲自测试过,很好用. (完)

  10. 多核CPU硬件架构介绍

    转自:http://book.51cto.com/art/201004/197196.htm SISD.MIMD.SIMD.MISD计算机的体系结构 1. 计算平台介绍 Flynn于1972年提出了计 ...