BZOJ 1180 / 2843 LCT模板题_双倍经验
一大早上到机房想先拍一下模板,热热身.
结果....对照着染色敲的 LCT 竟然死活也调不过去(你说我抄都能抄错)
干脆自己重新敲了一遍,10min就敲完了.......
还是要相信自己
Code:
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
#define maxn 40000
struct LCT
{
#define lson ch[x][0]
#define rson ch[x][1]
int tag[maxn],val[maxn],sumv[maxn],ch[maxn][2],f[maxn],sta[maxn];
int isRoot(int x)
{
return !(ch[f[x]][1] == x || ch[f[x]][0] == x);
}
int get(int x)
{
return ch[f[x]][1] == x;
}
void mark(int x)
{
if(!x) return ;
swap(lson, rson), tag[x] ^= 1;
}
void pushdown(int x)
{
if(!x) return ;
if(tag[x])
{
mark(lson), mark(rson), tag[x] ^=1 ;
}
}
void pushup(int x)
{
sumv[x] = sumv[lson] + sumv[rson] + val[x];
}
void rotate(int x)
{
int old = f[x], fold = f[old], which = get(x);
if(!isRoot(old)) ch[fold][ch[fold][1] == old] = x;
ch[old][which] = ch[x][which ^ 1], f[ch[old][which]] = old;
ch[x][which ^ 1 ] = old, f[old] = x, f[x] = fold;
pushup(old),pushup(x);
}
void splay(int x)
{
int u = x, v = 0;
sta[++v] = u;
while(!isRoot(u)) sta[++v] = f[u], u = f[u];
while(v) pushdown(sta[v--]);
u = f[u];
for(int fa; (fa = f[x]) != u; rotate(x))
if(f[fa] != u) rotate(get(fa) == get(x) ? fa: x);
}
void Access(int x)
{
for(int y = 0; x ; y = x,x = f[x])
{
splay(x), rson = y, pushup(x);
}
}
void makeRoot(int x)
{
Access(x), splay(x), mark(x);
}
void link(int a,int b)
{
makeRoot(a), f[a] = b;
}
void split(int a,int b)
{
makeRoot(a), Access(b), splay(b);
}
}T;
struct Union_Find
{
int p[maxn];
void init()
{
for(int i = 0;i < maxn ;++i) p[i] = i;
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
int merge(int a,int b)
{
int x = find(a), y = find(b);
if(x == y) return 0;
p[x] = y;
return 1;
}
}U;
char str[20];
int main()
{
// setIO("input");
U.init();
int n;
scanf("%d",&n);
for(int i = 1;i <= n; ++i) scanf("%d",&T.val[i]), T.sumv[i] = T.val[i];
int q,a,b,c;
scanf("%d",&q);
while(q --)
{
scanf("%s",str);
if(str[0] == 'b')
{
scanf("%d%d",&a,&b);
if(!U.merge(a,b))
printf("no\n");
else
{
T.link(a, b);
printf("yes\n");
}
}
if(str[0] == 'p' )
{
scanf("%d%d",&a,&b);
T.makeRoot(a),T.val[a] = b, T.pushup(a);
}
if(str[0] == 'e')
{
scanf("%d%d",&a,&b);
if(U.find(a) == U.find(b))
{
T.split(a,b);
printf("%d\n",T.sumv[b]);
}else printf("impossible\n");
}
}
return 0;
}
BZOJ 1180 / 2843 LCT模板题_双倍经验的更多相关文章
- 高手过愚人节 Manancher模板题_双倍经验
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- bzoj2049-洞穴勘测(动态树lct模板题)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- BZOJ 2724 蒲公英 | 分块模板题
题意 给出一个序列,在线询问区间众数.如果众数有多个,输出最小的那个. 题解 这是一道分块模板题. 一个询问的区间的众数,可能是中间"整块"区间的众数,也可能是左右两侧零散的数中的 ...
- 【BZOJ2049,2631,3282,1180】LCT模板四连A
好吧我并不想讲LCT 只是贴4个代码~ [BZOJ2049][Sdoi2008]Cave 洞穴勘测 #include <cstdio> #include <cstring> # ...
- BZOJ 2982: combination Lucas模板题
Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...
- BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 989 Solved: 611[Submit][S ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)
emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...
- 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题
2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
随机推荐
- ajax跨域 (转)
题纲 关于跨域,有N种类型,本文只专注于ajax请求跨域(,ajax跨域只是属于浏览器”同源策略”中的一部分,其它的还有Cookie跨域iframe跨域,LocalStorage跨域等这里不做介绍), ...
- 从0到1发布一个Vue Collapse组件
需求背景 最近在项目中遇到了一个类似Collapse的交互需求,因此到github上找了一圈关于Vue Collapse的相关轮子,但是多少都有些问题.有的是实现问题,例如vue2-collapse, ...
- 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- fzoj 2113数位dp
参考http://blog.csdn.net/xingyeyongheng/article/details/8785785 #include<stdio.h> #define ll lon ...
- Linux学习笔记:什么是x86
什么是x86 和硬件打交道常常会听说x86,疑惑的时候自己翻过书上网查过资料.可是都不甚明白.近期再次遇到x86这个词,随具体了解并做笔记记录. 想要知道什么是x86应该先区分CPU的分类. CPU ...
- Spark Streaming源代码学习总结(一)
1.Spark Streaming 代码分析: 1.1 演示样例代码DEMO: 实时计算的WorldCount: import org.apache.spark.streaming.{Seconds, ...
- python使用pytest+pytest报告
需要安装pytest和pytest-html pip3 install -U pytest pip3 install -U pytest-html
- Linux开发环境搭建与使用——Linux必备软件之Samba
假如我们是在ubuntu环境上做对应的开发.有的时候,我们须要把我们写的程序共享给别人,或者,自己拷贝出来备份一份.我们习惯用U盘拷贝,假设须要频繁拷贝的话,这样会不太方便.这里给大家介绍一种更好的方 ...
- Swift - 获取当前时间的时间戳(时间戳与时间互相转换)
(本文代码已升级至Swift3) 1,时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 2,获取当前时间的时 ...
- 【POJ 3696】 The Luckiest number
[题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...