BZOJ2843——极地旅行社
1、题目大意:动态树问题,点修改,链查询。另外说明双倍经验题=bzoj1180
2、分析:lct模板题,练手的
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
namespace LinkCutTree{
struct Node{
Node *ch[2], *fa;
int sum, num;
bool rev;
inline int which();
inline void reverse(){
if(this) rev ^= 1;
}
inline void pd(){
if(rev){
swap(ch[0], ch[1]);
ch[0] -> reverse();
ch[1] -> reverse();
rev = false;
}
}
inline void maintain(){
sum = num + ch[0] -> sum + ch[1] -> sum;
}
Node();
} *null = new Node, tree[30010], *pos[30010];
Node::Node(){
num = sum = 0;
rev = false;
ch[0] = ch[1] = fa = null;
}
inline int Node::which(){
if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1;
return this == fa -> ch[1];
}
inline void rotate(Node *o){
Node *p = o -> fa;
int l = o -> which(), r = l ^ 1;
o -> fa = p -> fa;
if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
p -> ch[l] = o -> ch[r];
if(o -> ch[r]) o -> ch[r] -> fa = p;
o -> ch[r] = p; p -> fa = o;
o -> ch[r] -> maintain();
o -> maintain();
}
inline void splay(Node *o){
static stack<Node*> st;
if(!o) return;
Node *p = o;
while(1){
st.push(p);
if(p -> which() == -1) break;
p = p -> fa;
}
while(!st.empty()){
st.top() -> pd(); st.pop();
}
while(o -> which() != -1){
p = o -> fa;
if(p -> which() != -1){
if(p -> which() ^ o -> which()) rotate(o);
else rotate(p);
}
rotate(o);
}
}
inline void Access(Node *o){
Node *y = null;
while(o != null){
splay(o);
o -> ch[1] = y;
o -> maintain();
y = o; o = o -> fa;
}
}
inline void MovetoRoot(Node *o){
Access(o);
splay(o);
o -> reverse();
}
inline Node* FindRoot(Node *o){
Access(o);
splay(o);
while(o -> ch[0] != null) o = o -> ch[0];
return o;
}
inline void Link(Node *x, Node *y){
MovetoRoot(x);
x -> fa = y;
}
inline void Cut(Node *x, Node *y){
MovetoRoot(x);
Access(y);
splay(y);
y -> ch[0] = x -> fa = null;
y -> maintain();
}
}
int main(){
using namespace LinkCutTree;
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
int x; scanf("%d", &x);
pos[i] = &tree[i];
pos[i] -> sum = pos[i] -> num = x;
}
int m;
scanf("%d", &m);
char op[20];
int x, y;
for(int i = 1; i <= m; i ++){
scanf("%s%d%d", op, &x, &y);
if(op[0] == 'b'){
if(FindRoot(pos[x]) != FindRoot(pos[y])){
puts("yes");
Link(pos[x], pos[y]);
}
else puts("no");
}
else if(op[0] == 'p'){
Access(pos[x]);
splay(pos[x]);
pos[x] -> num = y;
pos[x] -> maintain();
}
else{
if(FindRoot(pos[x]) != FindRoot(pos[y])) puts("impossible");
else{
MovetoRoot(pos[x]);
Access(pos[y]);
splay(pos[y]);
printf("%d\n", pos[y] -> sum);
}
}
}
return 0;
}
BZOJ2843——极地旅行社的更多相关文章
- bzoj2843极地旅行社
bzoj2843极地旅行社 题意: 一些点,每个点有一个权值.有三种操作:点与点连边,单点修改权值,求两点之间路径上点的权值和(需要判输入是否合法) 题解: 以前一直想不通为什么神犇们的模板中LCT在 ...
- BZOJ2843: 极地旅行社
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 90 Solved: 56[Submit][Status] Descripti ...
- BZOJ2843 极地旅行社 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...
- 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座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
随机推荐
- HTML1
1. 前端:html:做网页的内容 CSS: 做网页的外观 JS:执行网页的动作 前端是静态网页,内容写死.要有变化的内容,需要数据库 浏览器去解析生成漂亮的界面 后台技术:.Net C# 数据库: ...
- IIS------Http错误:50019,由于权限不足无法读取配置文件
转载: http://niutuku.com/tech/2008/273661.shtml 注意:该用户名称必须是:Everyone
- Maven环境变量配置
Maven 3.0.4版本下载地址: http://www.apache.org/dyn/closer.cgi?path=/maven/binaries/apache-maven-3.0.4-bin. ...
- C#的两个大方向
http://zhidao.baidu.com/link?url=wG9G_EaT3tRd5_7ndU3vpFqmuc6S8N7F5TpBYsDPEui3HQ-wcU7nqw-_aRpIwtXQbC3 ...
- NumberFormat类
NumberFormat表示数字的格式化类,即可以按照本地的风格习惯进行数字的显示. NumberFormat是一个抽象类,和MessageFormat类一样,都是Format类的子类,本类在使用时可 ...
- 当SVN服务器端IP地址发生变化时,客户端重新定位
第一种方法: 重新设置URL: 第二种方法: 找到客户端数据库文件 ,在隐藏的文件夹.svn中,找到文件夹中的文件 *.db文件 ,用SQLite打开,修改表Repository中的数据
- Ubuntu 15.10安装KVM
1.查看cpu是否支持硬件虚拟化 egrep "svm|vmx" /proc/cpuinfo 2.安装KVM apt-get install qemu-kvm virt-manag ...
- 平均值mean,众数mode,中值median 和 标准差stddev
平均值mean,众数mode,中值median 和 标准差stddev 均值,众数,中位数,标称差: 均值是就全部数据计算的,它具有优良的数学性质,是实际中应用最广泛的集中趋势测度值.其主要缺点是易受 ...
- C#异常类相关总结
C#异常类相关总结 C#异常类相关总结 C#异常类一.基类ExceptionC#异常类二.常见的异常类1.SystemException类:该类是System命名空间中所有其他异常类的基类.(建议:公 ...
- 求1到n的阶乘
#include<stdio.h> int main() { int data; ; scanf("%d",&data); ){ int j; ;j<=d ...