复习一下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. FFT,NTT 专题

    学习傅里叶的基本性质及其代码,可以参考大神理解 还有 ACdream 的博客 贴一下NTT的模板: using namespace std; typedef long long ll; int n; ...

  2. 三维网格分割算法(Random Walks)

    首先以一维随机游走(1D Random Walks)为例来介绍下随机游走(Random Walks)算法,如下图所示,从某点出发,随机向左右移动,向左和向右的概率相同,都为1/2,并且到达0点或N点则 ...

  3. BC#86 1003NanoApe Loves Sequence Ⅱ[two-pointer]

    NanoApe Loves Sequence Ⅱ  Accepts: 374  Submissions: 946  Time Limit: 4000/2000 MS (Java/Others)  Me ...

  4. HDU3535AreYouBusy[混合背包 分组背包]

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. jmeter-HTTP COOKIE Manager

    http://wangsheng14591.blog.163.com/blog/static/327797102012829101351887/

  6. tshark过滤并保存包特定字段

    1.过滤端口为5001的tcp包,将时间输出 tshark -r h1.pcap -Y "tcp.port==5001" -T fields -e frame.time 时间格式如 ...

  7. Windows系统服务器IIS7.5 Asp.net支持10万请求的设置方法

    问题现象 ECS Windows系统服务器基于IIS搭建的网站由于IIS默认的配置,服务器最多只能处理5000个同时请求,访问量大时很容易导致报错: Error Summary:  HTTP Erro ...

  8. 多线程进行http请求

    昨天需要一个线下脚本进行单播推送,大约有1kw个用户,考虑到推送速度就临时搞了个请求线上的一个脚本 /** * 临时支持invoke单播推送 */ #include <stdio.h> # ...

  9. 为EXSi5.5上的Centos虚机增加硬盘容量

    宿主机调整 1. 关闭虚机, 2. 检查是否有存在的snapshot, 如果有, 需要删除, 否则不能调整磁盘容量 3. 虚机上编辑配置, 将磁盘容量调大后保存 虚机调整 参考这篇写得非常详细: 点击 ...

  10. Meet Python: little notes 3 - function

    Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...