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座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
随机推荐
- win7下 mysql主从配置实现
win7下学习 mysql主从复制 一.环境: 主服务器(master):192.168.1.23 mysql版本:5.5 从服务器(slave):192.168.1.24 mysql版本:5.5 ...
- mouseover与mouseenter与mousemove的区别mouseout与mouseleave的区别
<html> <head> <title></title> </head> <body> <p> 当鼠标进入div1 ...
- 关闭一个winform窗体刷新另外一个
例如Form1是你的主窗体,然后Form2是你的要关闭那个窗体,在Form1中SHOW FORM2的窗体那里加上一句f2.FormClosed += new FormClosedEventHandle ...
- centos用yum安装mysql-server
1.安装:#yum -y install mysql-server 2.修改配置:#vi /etc/my.cnf 暂时修改一下编码(添加在密码下方添加): default-character-set ...
- NOSQL之【Redis学习:配置说明】
# yes:后台运行:no:不是后台运行(老版本默认) daemonize yes # redis的进程文件 pidfile /var/run/redis.pid # 端口 port # bind_a ...
- php验证身份证号码正确性
发布:JB01 来源:脚本学堂 [大 中 小] 分享一例php代码,用于验证身份证号码的正确性,用到了preg_match.preg_replace函数,有需要的朋友可以参考学习下.本文转 ...
- ORA-24010 SMG-3000
参考:http://t.askmaclean.com/thread-528-1-1.html 运行脚本的时候一定要清楚脚本做了什么操作,注意看清脚本的报错. conn / as sysdba@?/rd ...
- RAC 安装完成后 节点间通信不依赖于SSH
RAC 安装完成后,想修改ssh 的端口.google了一下.原文https://community.oracle.com/thread/2444594?tstart=0 原文说的是11g,10g也好 ...
- HBase性能优化方法总结(转)
本文主要是从HBase应用程序设计与开发的角度,总结几种常用的性能优化方法.有关HBase系统配置级别的优化,这里涉及的不多,这部分可以参考:淘宝Ken Wu同学的博客. 1. 表的设计 1.1 Pr ...
- MVC去掉传参时的验证:从客户端中检测到有潜在危险的Request.QueryString值
解决方法:给Action添加属性[ValidateInput(false)]. 例: [ValidateInput(false)] public ActionResult Index(string o ...