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座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
随机推荐
- 常用机器视觉工具----图像分析工具(blob分析)
http://blog.sina.com.cn/s/blog_67cc4eb70100ivnt.html Blob分析:Blob分析目的在于对图像中的2-D形状进行检测和分析,得到诸如目标位置.形状. ...
- HTML学习笔记——选择器
1> ID选择器.交叉选择器.群组选择器.子代选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- 入门:HTML:hello world!
<html> <head> </head> <body> <h1>hello world!</h1> </body> ...
- ecshop 的transport.js 与jqueyr冲突
1111 {insert_scripts files='common.js,global.js,transport.js'} <script type="text/javascript ...
- Linux下非root用户如何安装软件
Linux下非root用户如何安装软件 从windows转移到Linux的用户最开始总会有各种不适,因为这种不适最终放弃linux的不在少数.对于这类人只能说可惜,还没有领略到linux的美好就过早放 ...
- C#:String.Format数字格式化输出
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
- 微型orm fluentdata
http://fluentdata.codeplex.com/documentation#Query
- Python之with语句
Python之with语句 在Python中,我们在打开文件的时候,为了代码的健壮性,通常要考虑一些异常情况,比如: try: ccfile = open('/path/data') content ...
- C#通用类型转换 Convert.ChangeType
]; object innerValue = ChangeType(value, innerType); return Activator.CreateInstance ...