复习一下Link Cut Tree的模板。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 30003
#define read(x) x=getint()
using namespace std;
struct node *null;
struct node {
node *ch[2], *fa;
int d, sum;
short rev;
bool pl() {return fa->ch[1] == this;}
bool check() {return fa == null || (fa->ch[0] != this && fa->ch[1] != this);}
void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
void push() {
if (rev) {
rev = 0; swap(ch[0], ch[1]);
ch[0]->rev ^= 1;
ch[1]->rev ^= 1;
}
}
void count() {sum = ch[0]->sum + ch[1]->sum + d;}
};
node pool[N];
int n;
inline int getint() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
namespace LCT {
void Build() {
null = &pool[0]; null->ch[0] = null->ch[1] = null->fa = null;
null->d = null->sum = null->rev = 0;
read(n);
for(int i = 1; i <= n; ++i) {
read(pool[i].d);
pool[i].ch[0] = pool[i].ch[1] = pool[i].fa = null;
pool[i].sum = pool[i].rev = 0;
}
}
void rotate(node *r) {
node *f = r->fa; bool c = r->pl();
if (f->check()) r->fa = f->fa;
else f->fa->setc(r, f->pl());
f->setc(r->ch[!c], c);
r->setc(f, !c);
f->count();
}
void update(node *r) {if (!r->check()) update(r->fa); r->push();}
void splay(node *r) {
update(r);
for(; !r->check(); rotate(r))
if (!r->fa->check()) rotate(r->pl() == r->fa->pl() ? r->fa : r);
r->count();
}
node *access(node *r) {
node *y = null;
for(; r != null; y = r, r = r->fa)
splay(r), r->ch[1] = y;
return y;
}
node *findrt(node *r) {
access(r); splay(r);
while (r->ch[0] != null) r = r->ch[0];
return r;
}
void changert(node *r) {
access(r)->rev ^= 1; splay(r);
}
void link(node *r, node *t) {
changert(r); r->fa = t;
}
} int main() {
LCT::Build();
int m = getint(), a, b;
node *aa, *bb;
char c;
for(int i = 1; i <= m; ++i) {
for(c = getchar(); c < 'a' || c > 'z'; c = getchar());
read(a); read(b);
switch (c) {
case 'b':
if (LCT::findrt(&pool[a]) == LCT::findrt(&pool[b])) puts("no");
else {puts("yes"); LCT::link(&pool[a], &pool[b]);}
break;
case 'p':
LCT::changert(&pool[a]); pool[a].d = b; pool[a].count();
break;
case 'e':
if (LCT::findrt(&pool[a]) != LCT::findrt(&pool[b])) puts("impossible");
else {
LCT::changert(&pool[a]); LCT::access(&pool[b]); LCT::splay(&pool[b]);
printf("%d\n",pool[b].sum);
}
break;
}
}
return 0;
}

水啊水~~~

【BZOJ 2843】极地旅行社的更多相关文章

  1. BZOJ 2843: 极地旅行社( LCT )

    LCT.. ------------------------------------------------------------------------ #include<cstdio> ...

  2. bzoj 2843: 极地旅行社

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1077  Solved: 645[Submit][Status][Discuss] Descripti ...

  3. bzoj 2843 极地旅行社(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2843 [题意] 给定一个森林,要求提供连边,修改点值,查询路径和的操作. [思路] L ...

  4. BZOJ 2843: 极地旅行社 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...

  5. BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)

    emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...

  6. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  7. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  8. 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 323  Solved: 218[Submit][Status][Discuss ...

  9. BZOJ2843: 极地旅行社

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 56[Submit][Status] Descripti ...

  10. [bzoj2843&&bzoj1180]极地旅行社 (lct)

    双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...

随机推荐

  1. stm32中断无电平触发的解决办法

    这几天在用stm32读取FPGA中FIFO里的数据,遇到了不少的问题.其中有个自己觉得比较好玩的问题,就拿出来写写.其实这个问题也比较简单,开始我觉得没必要拿出来写,不过,想想后觉得还是写写吧,就当做 ...

  2. 第16章 调色板管理器_16.4 一个DIB位图库的实现(2)

    //接上一篇 //DibPal.h /*----------------------------------------------------------------- DIBPAL.H heade ...

  3. Unity热门插件推荐

    前言 Unite2015的笔记 ,本文所提到的扩展主要针对 mobile上使用. 文中资源在Asset Store描述的截图日期:2016-04-28 Mesh Baker https://www.a ...

  4. web 小知识

    document.write和innerHTML的区别   document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关 ...

  5. C++在字符串前加一个L作用:

    在字符串前加一个L作用:    如 L"我的字符串" 表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节.    strlen("asd" ...

  6. Rhino 是一个完全使用Java语言编写的开源JavaScript实现。Rhino通常用于在Java程序中,为最终用户提供脚本化能力。它被作为J2SE 6上的默认Java脚本化引擎。

    https://developer.mozilla.org/zh-CN/docs/Mozilla/Projects/Rhino

  7. ArrayList的线程安全测试

    public class TestThread implements Runnable{ private List list; CountDownLatch cdl; public TestThrea ...

  8. tyvj[1089]smrtfun

    描述  现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式   ...

  9. java代码封装与编译

    代码封装: 在这个java程序内调用另一个类 在arrayTool中把这两个函数封装起来. 编译顺序:(由下文可知应该是先进行语法检查再进行编译) 先编译ArrayTool再编译ArrayOperat ...

  10. Oracle job procedure 存储过程定时任务

    Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...