[bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福。。。
所以另一道是300大洋的世界T_T。。。虽然题目是一样的,不过2843数据范围小了一点。。。
都是lct基本操作
#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn=;
struct zs{
int c[],fa,sum,val;
bool rev;
}tree[maxn];
int i,j,x,y,n,m;
int stack[maxn];
char id[];
inline bool isroot(int x){return tree[tree[x].fa].c[]!=x&&tree[tree[x].fa].c[]!=x;
}
inline void update(int x){tree[x].sum=tree[tree[x].c[]].sum+tree[x].val+tree[tree[x].c[]].sum;
}
inline void pushdown(int x){
if(!tree[x].rev)return;
int l=tree[x].c[],r=tree[x].c[];
if(l)tree[l].rev^=;if(r)tree[r].rev^=;tree[x].rev^=;
swap(tree[x].c[],tree[x].c[]);
}
inline void rotate(int x){
int fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))tree[gfa].c[tree[gfa].c[]==fa]=x;
int l=tree[fa].c[]==x,r=l^;
tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
update(fa);update(x);
}
void splay(int x){
int top=,tmp=x;stack[++top]=x;
while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
while(top)pushdown(stack[top]),top--;
int fa,gfa;
while(!isroot(x)){
fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))
if((tree[gfa].c[]==fa)^(tree[fa].c[]==x))rotate(x);
else rotate(fa);
rotate(x);
}
}
inline void access(int x){
int son=;
while(x){
splay(x);tree[x].c[]=son;
update(x);son=x;x=tree[x].fa;
}
}
inline void makeroot(int x){
access(x);splay(x);tree[x].rev^=;
}
inline void cut(int x,int y){
makeroot(x);access(y);splay(y);tree[y].c[]=tree[x].fa=;
}
inline void link(int x,int y){
makeroot(x);tree[x].fa=y;
}
inline int getfa(int x){
access(x);splay(x);
while(tree[x].c[]){x=tree[x].c[];pushdown(x);}
splay(x);return x;
}
inline int query(int x,int y){
makeroot(x);access(y);splay(y);
return tree[y].sum;
}
inline void change(int x,int val){
makeroot(x);tree[x].val=val;update(x);
}
int main(){
scanf("%d",&n);for(i=;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val;
scanf("%d",&m);
while(m--){
scanf("%s%d%d",id,&x,&y);
if(id[]=='e'){
if(getfa(x)!=getfa(y))printf("impossible\n");else printf("%d\n",query(x,y));
}else if(id[]=='b'){
if(getfa(x)==getfa(y))printf("no\n");
else printf("yes\n"),link(x,y);
}else if(id[]=='p')change(x,y);
}
return ;
}
16.1.8:重写了一发
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=;
int ch[maxn][],fa[maxn],sum[maxn],num[maxn],st[maxn];
int i,j,x,y,n,m;
bool rev[maxn]; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
} inline bool isrt(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline void upd(int x){sum[x]=sum[ch[x][]]+num[x]+sum[ch[x][]];}
inline void pushdown(int x){
if(!rev[x])return;
rev[x]=,swap(ch[x][],ch[x][]),rev[ch[x][]]^=,rev[ch[x][]]^=;
}
inline void rotate(int x){
int f=fa[x],gfa=fa[f],l=ch[f][]==x,r=l^;
if(!isrt(f))ch[gfa][ch[gfa][]==f]=x;
ch[f][l]=ch[x][r],ch[x][r]=f,fa[x]=gfa,fa[f]=x,fa[ch[f][l]]=f;
sum[x]=sum[f],upd(f);
}
void splay(int x){
int f,gfa;
for(st[st[]=]=f=x;!isrt(f);)st[++st[]]=(f=fa[f]);
while(st[])pushdown(st[st[]--]);
for(f=fa[x],gfa=fa[f];!isrt(x);rotate(x),f=fa[x],gfa=fa[f])
if(!isrt(f))rotate(((ch[f][]==x)^(ch[gfa][]==f))?x:f);
}
inline void access(int x){
for(int t=;x;t=x,x=fa[x])splay(x),ch[x][]=t,upd(x);
}
inline void makert(int x){
access(x),splay(x),rev[x]^=;
}
void link(int x,int y){
makert(x),fa[x]=y;if(!(x&))splay(x);
}
inline int getfa(int x){
for(access(x),splay(x);ch[x][];x=ch[x][]);
return x;
}
int main(){
n=read();for(i=;i<=n;i++)num[i]=read();
m=read();char id;
while(m--){
for(id=getchar();id<'a'||id>'z';id=getchar());
x=read(),y=read();
if(id=='b')if(getfa(x)!=getfa(y))link(x,y),puts("yes");else puts("no");
if(id=='e')if(getfa(x)==getfa(y))makert(x),access(y),splay(y),printf("%d\n",sum[y]);else puts("impossible");
if(id=='p')makert(x),num[x]=y,upd(x);
}
return ;
}
2843: 极地旅行社
Time Limit: 10 Sec Memory Limit: 256 MB
Description
Input
第一行一个正整数N,表示冰岛的数量。
第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。
第三行一个正整数M,表示命令的数量。
接下来M行即命令,为题目描述所示。
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
HINT
1<=N<=30000,1<=M<=100000(bzoj2843)
1<=n<=30000, 1<=m<=300000 (bzoj1180)
[bzoj2843&&bzoj1180]极地旅行社 (lct)的更多相关文章
- 【bzoj2843】极地旅行社 LCT
题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mir ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组
[BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
- BZOJ 2843: 极地旅行社( LCT )
LCT.. ------------------------------------------------------------------------ #include<cstdio> ...
- BZOJ2843:极地旅行社(LCT入门题)
不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服 务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mirko的旅 ...
- BZOJ2843 极地旅行社 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...
- 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT
竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- BZOJ1180 [CROATIAN2009]OTOCI LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1180 本题和BZOJ2843一样. BZOJ2843 极地旅行社 LCT 题意概括 有n座岛 每座 ...
随机推荐
- Robotframework-Appium系列:登录操作
之前Appium的环境已经配置完成(参考Robotframework-Appium系列:安装配置),接下来就是如何使用Appium来完成我们的apk的测试工作. 一.环境准备 所需的软件列表如下 Ro ...
- python变量字符拼接
cpu = instances['vcpus_current'] cpu1 = str(cpu) + '核' memory = instances['memory_current'] / 1024 m ...
- Java I/O---添加属性和有用的接口—FilterlnputStream&FilterOutputStream
0.装饰器模式 Java I/O类库需要多种不同功能的组合,这正是使用装饰器模式的理由所在.这也是Java I/O类库里存在filter(过滤器)类的原因所在,抽象类filter是所有装饰器类的基类. ...
- 598. Range Addition II
Given an m * n matrixMinitialized with all0's and several update operations. Operations are represen ...
- npm lodash
在数据操作时,Lodash 就是我的弹药库,不管遇到多复杂的数据结构都能用一些函数轻松拆解. ES6 中也新增了诸多新的对象函数,一些简单的项目中 ES6 就足够使用了,但还是会有例外的情况引用了少数 ...
- Q:javax.comm 2.0 windows下Eclipse的配置
@转自http://blog.csdn.net/zhuanghe_xing/article/details/7523744处 要在Windows下,对计算机的串口或并口等进行编程,可以选择使用Java ...
- 使用MethodType函数将方法绑定到类或实例上
在开始正文之前,需要了解下Python的绑定方法(bound method)和非绑定方法. 简单做个测试: 定义一个类,类中由实例方法.静态方法和类方法. class ClassA: def inst ...
- ASP.NET Core 一步步搭建个人网站(5)_Api模拟和网站分析
前言 经过前面几章,我们的网站已经最基本的功能,接下来就是继续拓展其他的功能,这期一起来实现一个该网站流量分析的工具,统计出这个网站每天用户相关数据,不仅要满足了我们对流量统计数字的基本要求,并且用更 ...
- mysql如何执行关联查询与优化
mysql如何执行关联查询与优化 一.前言 在数据库中执行查询(select)在我们工作中是非常常见的,工作中离不开CRUD,在执行查询(select)时,多表关联也非常常见,我们用的也比较多,那么m ...
- 第四章:大数据 の HBase 基础
本课主题 NoSQL 数据库介绍 HBase 基本操作 HBase 集群架构与设计介紹 HBase 与HDFS的关系 HBase 数据拆分和紧缩 引言 介绍什么是 NoSQL,NoSQL 和 RDBM ...