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,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
随机推荐
- PAT 1075. PAT Judge
The ranklist of PAT is generated from the status list, which shows the scores of the submittions. Th ...
- Java设计模式之 — 单例(Singleton)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8860649 写软件的时候经常需要用到打印日志功能,可以帮助你调试和定位问题,项目上 ...
- 修改oracle数据库时间
1.修改前需要先停止 oracle 数据库服务 2.修改 oracle 数据库所在的服务器时间 3.再次启动 oracle 数据库,即可 以上就是小编修改 oracle 数据库的时间,修改完之后,其他 ...
- 在Win32 Application 环境下实现MFC窗口的创建
// Win32下MFC.cpp : Defines the entry point for the application.// #include "stdafx.h" clas ...
- Efficient ticket lock synchronization implementation using early wakeup in the presence of oversubscription
A turn-oriented thread and/or process synchronization facility obtains a ticket value from a monoton ...
- 洛谷——P1095 守望者的逃离
https://www.luogu.org/problem/show?pid=1095#sub 题目描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交 ...
- 经验总结18--EF改动关系,多对多
EF改动关系让我费事蛮多时间.能查的资料少,网上试了非常多方法都不正确. 最后还是自己研究出来了.在这里和大家分享下,有更好的方法也能够分享下. 首先说说我一般做改动功能时,前台传參数,后台使用对象接 ...
- java基础开发—jstl标签库
在DRP项目中.接触到了JSTL标签库. 在未使用Jstl之前,我们使用JSP脚本实现一些声明或是表达式任务,做一些业务相关操作时,须要在页面中嵌入大量的java代码.在DRP项目开发前期.使用jsp ...
- 刚開始学习的人非常有用:struts2中将jsp数据传到action的几种方式
先给上struts.xml代码: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- Linux命令(八)——vi编辑器的使用
vi编辑器是linux系统下的标准正文编辑器,有三种基本模式:命令行模式.插入模式和底行命令模式. 1.命令行模式:控制屏幕光标的移动,字符.字或行的删除,移动复制某区段及进入插入模式或底行命令模式下 ...