[BZOJ2843] 极地旅行社(LCT)

时间:2021-08-20 23:27:35

传送门

模板。

——代码

 #include <cstdio>
#include <iostream>
#define N 300001
#define get(x) (son[f[x]][1] == (x))
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
#define isroot(x) (son[f[x]][0] ^ (x) && son[f[x]][1] ^ (x)) int n, m;
int a[N], sum[N], son[N][], rev[N], f[N], s[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void update(int x)
{
if(x)
{
sum[x] = a[x];
if(son[x][]) sum[x] += sum[son[x][]];
if(son[x][]) sum[x] += sum[son[x][]];
}
} inline void pushdown(int x)
{
if(x && rev[x])
{
swap(son[x][], son[x][]);
if(son[x][]) rev[son[x][]] ^= ;
if(son[x][]) rev[son[x][]] ^= ;
rev[x] = ;
}
} inline void rotate(int x)
{
int old = f[x], oldf = f[old], wh = get(x); if(!isroot(old))
son[oldf][get(old)] = x;
f[x] = oldf; son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; son[x][wh ^ ] = old;
f[old] = x; update(old);
update(x);
} inline void splay(int x)
{
int i, fa, t = ;
s[++t] = x;
for(i = x; !isroot(i); i = f[i]) s[++t] = f[i];
for(i = t; i >= ; i--) pushdown(s[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fa = f[x]))
rotate(get(x) ^ get(fa) ? x : fa);
} inline void access(int x)
{
for(int t = ; x; t = x, x = f[x]) splay(x), son[x][] = t, update(x);
} inline void reverse(int x)
{
access(x);
splay(x);
rev[x] ^= ;
} inline int query(int x, int y)
{
reverse(x);
access(y);
splay(y);
return sum[y];
} inline int find(int x)
{
access(x);
splay(x);
while(son[x][]) x = son[x][];
return x;
} inline void link(int x, int y)
{
reverse(x);
f[x] = y;
access(x);
} inline void change(int x, int y)
{
access(x);
splay(x);
a[x] = y;
update(x);
} int main()
{
int i, x, y;
char s[];
n = read();
for(i = ; i <= n; i++) a[i] = read();
m = read();
for(i = ; i <= m; i++)
{
scanf("%s", s);
x = read();
y = read();
if(s[] == 'b')
{
if(find(x) ^ find(y)) link(x, y), puts("yes");
else puts("no");
}
if(s[] == 'p') change(x, y);
if(s[] == 'e')
{
if(find(x) ^ find(y)) puts("impossible");
else printf("%d\n", query(x, y));
}
}
return ;
}

[BZOJ2843] 极地旅行社(LCT)的更多相关文章

  1. BZOJ2843 极地旅行社 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...

  2. bzoj2843极地旅行社

    bzoj2843极地旅行社 题意: 一些点,每个点有一个权值.有三种操作:点与点连边,单点修改权值,求两点之间路径上点的权值和(需要判输入是否合法) 题解: 以前一直想不通为什么神犇们的模板中LCT在 ...

  3. &lbrack;bzoj2843&amp&semi;&amp&semi;bzoj1180&rsqb;极地旅行社 &lpar;lct&rpar;

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

  4. BZOJ2843&colon; 极地旅行社

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

  5. BZOJ 2843&colon; 极地旅行社&lpar; LCT &rpar;

    LCT.. ------------------------------------------------------------------------ #include<cstdio&gt ...

  6. 【bzoj2843】极地旅行社 LCT

    题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mir ...

  7. BZOJ2843极地旅行社&amp&semi;BZOJ1180&lbrack;CROATIAN2009&rsqb;OTOCI——LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作:  1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...

  8. 【BZOJ1180】&colon; &lbrack;CROATIAN2009&rsqb;OTOCI &amp&semi; 2843&colon; 极地旅行社 LCT

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

  9. BZOJ2843——极地旅行社

    1.题目大意:动态树问题,点修改,链查询.另外说明双倍经验题=bzoj1180 2.分析:lct模板题,练手的 #include <stack> #include <cstdio&g ...

随机推荐

  1. js转换数据库中DateTime字段类型

    在程序中,从数据库中读取到的日期时间类型数据一般是这种格式:"/Date(1355109408000+0800)/" 要经过js函数处理变为格式:'2012-12-10 11:05 ...

  2. R语言 小程序

    x<-sample(1:11) x 日期和时间 ###Data and Time### > data_time <- data.frame(data = c("2015-0 ...

  3. 软件工程 speedsnail 冲刺1

    2015-5-5 完成任务:背景音乐的添加: 遇到问题:循环播放的设置: 明日任务:snail的移动功能.

  4. PaaS平台资源

    http://www.vagrantup.com/ http://www.docker.com/

  5. bzoj1040

    论环形dp的重要! 其实这个环比较简单,稍微分析一下就知道, 这是一个简单环,并且每个联通块里只含有一个. 我觉得把处理环的关键,就是要找出环形和线形(树形)有什么区别. 如果我们从某处断开的做dp的 ...

  6. FLASH驱动之-块设备驱动系统构架

    一.  块设备是只能以块为单位进行访问的设备,块的大小一般是512个字节的整数倍,常见的块设备包括硬件,SD卡,光盘,flash等.驱动程序是块的整数倍从设备读写得到数据.块设备的最小访单位为块,不同 ...

  7. VVDocumenter安装过程的一些问题

    vvdocument是瞄神写的一个插件 作用的话大家都知道 这里就不多说了 插件下载地址:https://github.com/onevcat/VVDocumenter-Xcode 1.下载后解压 编 ...

  8. 相比Redis,Memcached真的过时了吗?

    下面内容来自Redis作者在*上的一个回答,对应的问题是<Is memcached a dinosaur in comparison to Redis?>(相比Re ...

  9. poi excel超出65536行数限制自动扩展Invalid row number &lpar;65536&rpar; outside allow

    1.xls一个sheet只能装65536行,多余则报错 poi包导出或写入excel超出65536报错: java.lang.IllegalArgumentException: Invalid row ...

  10. Python3&period;6安装OpenCV

    1.安装依赖 pip install --upgrade setuptools pip install numpy Matplotlib -i https://mirrors.aliyun.com/p ...