复习一下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. POJ1386Play on Words[有向图欧拉路]

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11846   Accepted: 4050 De ...

  2. UI的重用性

    UI抽取思路 一款手机游戏中UI有几十个到上百个不等,如果一个一个做这些UI,无疑会花费很多时间. 近期我们的游戏UI已经是第N次改版了,经过这N多次的修改,我总结了UI其实有很多的共性(就是相同性) ...

  3. iOS中NSScanner 的用法

    NSScanner是一个类,用于在字符串中扫描指定的字符,尤其是把它们翻译/转换为数字和别的字符串.可以创建NSScanner时制定他的String属性,然后scanner会按照你的要求从头到尾扫描这 ...

  4. Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package 1.0.1 supports: net (.NETFramework,Version=v0.0)

    Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package ...

  5. 在ASP.NET中如何运行后台任务

    from:https://blogs.msdn.microsoft.com/scott_hanselman/2014/12/21/asp-net/ [原文发表地址] How to run Backgr ...

  6. linux下安装安装pcre-8.32 configure: error: You need a C++ compiler for C++ support

    linux下安装安装pcre-8.32./configure --prefix=/usr/local/pcre 出现以下错误configure: error: You need a C++ compi ...

  7. iOS:CYLTabBarController【低耦合集成TabBarController】

    导航 与其他自定义TabBarController的区别 集成后的效果 项目结构 使用CYLTabBarController 第一步:使用CocoaPods导入CYLTabBarController ...

  8. 升级nodejs版本

    node有一个模块叫n,是专门用来管理node.js的版本的. 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stable n后面也可以跟随版本号比 ...

  9. codevs 1281 Xn数列

    题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...

  10. 有的机器不能通过session登录

    web.config <system.web>    <sessionState mode="InProc" cookieless="AutoDetec ...