一大早上到机房想先拍一下模板,热热身.

结果....对照着染色敲的 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模板题_双倍经验的更多相关文章

  1. 高手过愚人节 Manancher模板题_双倍经验

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  2. bzoj2049-洞穴勘测(动态树lct模板题)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  3. BZOJ 2724 蒲公英 | 分块模板题

    题意 给出一个序列,在线询问区间众数.如果众数有多个,输出最小的那个. 题解 这是一道分块模板题. 一个询问的区间的众数,可能是中间"整块"区间的众数,也可能是左右两侧零散的数中的 ...

  4. 【BZOJ2049,2631,3282,1180】LCT模板四连A

    好吧我并不想讲LCT 只是贴4个代码~ [BZOJ2049][Sdoi2008]Cave 洞穴勘测 #include <cstdio> #include <cstring> # ...

  5. BZOJ 2982: combination Lucas模板题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...

  6. BZOJ 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 989  Solved: 611[Submit][S ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)

    emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...

  9. 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题

    2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...

随机推荐

  1. 解决ExpressSpreadSheet 中文乱码问题[备查]

    cxSpreadSheet和F1Book等都提供了类似Excel的操作, 但是相比F1Book, cxSpreadSheet的中文支持还不是很完善.在设置了wordbreak为true的时候,里面的中 ...

  2. UVA 10328 Coin Toss

    Coin Toss Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: ...

  3. BALNUM - Balanced Numbers

    BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB Balanced numbers have been used ...

  4. Centos6.4建立本地yum源

    https://jingyan.baidu.com/article/e9fb46e1aed8207521f7662c.html

  5. mongodb--win10安装

    安装完成后再将安装后的bin目录加入PATH路径

  6. Spring MVC-页面重定向示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm 说明:示例基于Spring MVC 4. ...

  7. String 经常用法最优算法实现总结 (一)

    <pre name="code" class="java"><span style="font-family: Arial, Hel ...

  8. iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

    iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  9. JS判断手机浏览器内核

    function is_weixn_qq() { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) ...

  10. Codeforces 115A- Party(DFS)

    A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...