BZOJ2843: 极地旅行社
2843: 极地旅行社
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 90 Solved: 56
[Submit][Status]
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 |
题解:
看到这种只有连边没有删边的总是想写个启发式合并。。。不过既然是练习LCT,就写LCT吧。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 100000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,v[maxn],sum[maxn],c[maxn][],fa[maxn],f[maxn],sta[maxn],top;
bool rev[maxn];
inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
inline bool isroot(int x)
{
return c[fa[x]][]!=x&&c[fa[x]][]!=x;
}
inline void pushup(int x)
{
sum[x]=sum[c[x][]]+sum[c[x][]]+v[x];
}
inline void rever(int x)
{
rev[x]^=;swap(c[x][],c[x][]);
}
inline void pushdown(int x)
{
if(!rev[x])return;
rever(c[x][]);rever(c[x][]);
rev[x]=;
}
inline void rotate(int x)
{
int y=fa[x],z=fa[y],l=c[y][]==x,r=l^;
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}
inline void splay(int x)
{
sta[++top]=x;
for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
for(;top;)pushdown(sta[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
{
if(c[z][]==y^c[y][]==x)rotate(x);else rotate(y);
}
rotate(x);
}
}
inline void access(int x)
{
for(int y=;x;x=fa[x])
{
splay(x);c[x][]=y;pushup(x);y=x;
}
}
inline void makeroot(int x)
{
access(x);splay(x);rever(x);
}
inline void link(int x,int y)
{
if(find(x)==find(y)){printf("no\n");return;}
printf("yes\n");
makeroot(x);fa[x]=y;f[find(x)]=find(y);splay(x);
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)v[i]=sum[i]=read(),f[i]=i;
m=read();
while(m--)
{
char ch[];
scanf("%s",ch);int x=read(),y=read();
if(ch[]=='b')link(x,y);
else if(ch[]=='p')splay(x),v[x]=y,pushup(x);
else if(find(x)!=find(y))printf("impossible\n");
else split(x,y),printf("%d\n",sum[y]);
}
return ;
}
BZOJ2843: 极地旅行社的更多相关文章
- bzoj2843极地旅行社
bzoj2843极地旅行社 题意: 一些点,每个点有一个权值.有三种操作:点与点连边,单点修改权值,求两点之间路径上点的权值和(需要判输入是否合法) 题解: 以前一直想不通为什么神犇们的模板中LCT在 ...
- BZOJ2843 极地旅行社 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...
- BZOJ2843——极地旅行社
1.题目大意:动态树问题,点修改,链查询.另外说明双倍经验题=bzoj1180 2.分析:lct模板题,练手的 #include <stack> #include <cstdio&g ...
- BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...
- [BZOJ2843] 极地旅行社(LCT)
传送门 模板. ——代码 #include <cstdio> #include <iostream> #define N 300001 #define get(x) (son[ ...
- bzoj2843极地旅行社题解
题目大意 有n座小岛,当中每一个岛都有若干帝企鹅. 一開始岛与岛之间互不相连.有m个操作.各自是在两个岛之间修一座双向桥,若两岛已连通则不修并输出no,若不连通就输出yes并修建.改动一个岛上帝企鹅的 ...
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组
[BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
随机推荐
- 在Linux上进行QT UI开发
在QT Creator UI编辑器上通过拖拽各种控件产生UI界面,然后点击编译/Build按钮,会自动生成对应的ui_xxxx.h的 头文件/header file. 参考: 1.Linux上使用Qt ...
- C语言 goto, return等跳转
C语言 goto, return等跳转 Please don't fall into the trap of believing that I am terribly dogmatical about ...
- Google 编码风格
一.Google JavaScript编码风格 简体中文版 Google JavaScript Style Guide 二.Google HTML/CSS代码风格指南 简体中文版 三.Google C ...
- [PR & ML 2] [Introduction] Example: Polynomial Curve Fitting
啊啊啊,竟然不支持latex,竟然HTML代码不能包含javascript,代码编辑器也不支持Matlab!!!我要吐槽博客的编辑器...T_T只能贴图凑合看了,代码不是图,但这次为了省脑细胞,写的不 ...
- DataGridView默认不选中
NurseGridList.CurrentCell = null; NurseGridList.ClearSelection(); Nurs ...
- 判断滚动条到底部的JS代码
这篇文章介绍了判断滚动条到底部的JS代码,有需要的朋友可以参考一下 判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop.clientHeight.scrollHeight. scrol ...
- SQL技术内幕四
数据类型: sql server只接受两种数据类型 1. 普通字符 varchar char 用一个字节表示一个字符,表示英文 2.unicode nchar nvarchar 用两个字节表示一个 ...
- 细说PHP中strlen和mb_strlen的区别
在PHP中,strlen与mb_strlen是求字符串长度的函数,但是对于一些初学者来说,如果不看手册,也许不太清楚其中的区别.下面通过例子,讲解这两者之间的区别. $str='中文a字1符'; ec ...
- 在Windows Server 2008上部署SVN代码管理总结
这段时间在公司开发Flex程序,所以使用TortoiseSVN作为团队代码管理器,今天在公司服务器上部署SVN服务器,并实验成功,总结如下: 服务器环境: 操作系统:Windows Server 20 ...
- This transaction has been rolled back, rather than only the current.
今天上午,收到运维组同事反映某应用系统的其中一个功能报错,不是偶然性事件,每个使用该功能的用户都报错.报错内容为:This transaction has been rolled back, rath ...