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

结果....对照着染色敲的 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. 0809MySQL-InnoDB Compact 行记录格式

    InnoDB存储引擎提供了compact(5.1后的默认格式)和redundant两个格式来存放行记录数据.redundant格式是为了兼容之前的版本而保留. mysql> show table ...

  2. Spring MVC-环境设置(转载实践)

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

  3. 支付宝钱包手势password破解实战(root过的手机可直接绕过手势password)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...

  4. 时间格式字符串转化为date和时间戳

    NSString *dateStr=@"2012-05-17 11:23:23"; NSLog(@"dateStr=%@",dateStr); NSDateFo ...

  5. Android更新带进度条的通知栏

    在网上查询了下.Android版本号更新通知栏带进度条,醉了,基本都是复制过来.有的代码不全,连源代码下载都没有.有下载也须要积分,还不能用,真黑心啊!!之前自己也写过自己定义通知栏Notificat ...

  6. 【转】坑爹的AsyncTask之根本停不下来

    原文网址:http://www.jianshu.com/p/0c6f4b6ed558 上篇<坑爹的AsyncTask之内存泄露>已经简单的探讨过线程使用不当会造成内存泄露的问题,在Acti ...

  7. 【BZOJ1597】【Usaco2008 Mar】土地购买 斜率优化DP

    题目: 题目在这里 思路与做法: 这题如果想要直接dp的话不太好处理. 不过, 我们发现如果\(a[i].x>=a[j].x\)且\(a[i].y>=a[j].y\) \((\)a是输入的 ...

  8. window下svn开机自动启动

  9. JavaScrip——插入地图

    具体操作步骤:1.百度搜索:百度地图生成器 2.打开第一个,复制网址http://api.map.baidu.com/lbsapi/creatmap/index.html,打开3.页面显示为 4.根据 ...

  10. Java基础7一面向对象

    1.构造方法: (1)定义:方法名称必须和类名相同,没有返回值,也没有void (2)语法: [访问修饰符] 类名(){ } (3)作用:创建对象.初始化成员变量. (4)构造方法的分类: A.无参数 ...