动态树LCT模板题

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
inline int read()
{
int x = 0, flag = 1;
char c;
while(! isgraph(c = getchar()))
if(c == '-')
flag *= - 1;
while(isgraph(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
inline void println(int x)
{
if(x < 0)
putchar('-');
if(x == 0)
putchar('0');
int top = 0, ans[10 + (1 << 4)];
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int MAXN = (int)2e5 + (1 << 5);
struct node
{
int isroot, ch[2], fa, size;
}T[MAXN];
void update(int u)
{
T[u].size = ((T[u].ch[0] == - 1) ? 0 : T[T[u].ch[0]].size)
+ ((T[u].ch[1] == - 1) ? 0 : T[T[u].ch[1]].size) + 1;
}
inline int get_rela(int u)
{
return u == T[T[u].fa].ch[1];
}
void rotate(int u)
{
int fa = T[u].fa, fafa = T[T[u].fa].fa, rela = get_rela(u);
T[T[u].ch[rela ^ 1]].fa = fa;
T[fa].ch[rela] = T[u].ch[rela ^ 1];
T[u].ch[rela ^ 1] = fa;
T[fa].fa = u;
T[u].fa = fafa;
if(T[fa].isroot)
T[fa].isroot = 0, T[u].isroot = 1;
else
T[fafa].ch[T[fafa].ch[1] == fa] = u;
update(fa), update(u);
}
void splay(int u)
{
while(1)
{
if(T[u].isroot)
break;
if(! T[T[u].fa].isroot)
rotate((get_rela(u) == get_rela(T[u].fa)) ? T[u].fa : u);
rotate(u);
}
}
void access(int u)
{
splay(u);
while(T[u].fa != - 1)
{
int fa = T[u].fa;
splay(fa);
if(T[fa].ch[1] != - 1)
T[T[fa].ch[1]].isroot = 1;
T[u].isroot = 0;
T[fa].ch[1] = u;
update(fa);
splay(u);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("BZOJ2002.in", "r", stdin);
freopen("BZOJ2002.out", "w", stdout);
#endif
int n = read();
T[n].fa = - 1;
for(int i = 0; i < n; i ++)
T[i].fa = min(read() + i, n);
for(int i = 0; i <= n; i ++)
T[i].size = 1, T[i].isroot = 1, T[i].ch[0] = T[i].ch[1] = - 1;
int m = read();
for(int i = 0; i < m; i ++)
{
int opt = read();
if(opt == 1)
{
int u = read();
access(u);
println(T[T[u].ch[0]].size); //这里要稍微注意一下
continue;
}
int u = read();
splay(u);
T[T[u].ch[0]].fa = T[u].fa;
T[T[u].ch[0]].isroot = 1;
T[u].ch[0] = - 1;
T[u].size = ((T[u].ch[1] == - 1) ? 0 : T[T[u].ch[1]].size) + 1;
T[u].fa = min(n, read() + u);
}
}

BZOJ2002弹飞绵羊的更多相关文章

  1. BZOJ-2002 弹飞绵羊 Link-Cut-Tree (分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 6801 Solved: 3573 [Submi ...

  2. bzoj2002 弹飞绵羊

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  3. [Link-Cut-Tree][BZOJ2002]弹飞绵羊

    题面 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上\(n\)个 ...

  4. bzoj2002 弹飞绵羊 lct版

    这道题就是维护一个有根的lct 一开始建树全部建虚边 求多少次弹出就是求他到根的距离(根为n+1) 这里有个小技巧 将n+1作为根而没有虚根操作起来会比较方便 #include<cstdio&g ...

  5. bzoj2002 弹飞绵羊 分块

    这道题是分块的初尝试 讲给定的区间n进行分块处理 这个每次修改的复杂的只有logn 很方便 代码是学黄学长的 http://hzwer.com/3505.html 当然里面还是有一定我自己的想法在里面 ...

  6. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

  7. 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块

    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...

  8. 【BZOJ2002】弹飞绵羊(Link-Cut Tree)

    [BZOJ2002]弹飞绵羊(Link-Cut Tree) 题面 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lost ...

  9. 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊

    BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊 Solution 很早以前写的一道分块题,最近在搞LCT,又做了一遍. 1.LCT做法 看到这种动态修改,想下LCT怎么维护. 修改操作就 ...

随机推荐

  1. python3爬取墨迹天气并发送给微信好友,附源码

    需求: 1. 爬取墨迹天气的信息,包括温湿度.风速.紫外线.限号情况,生活tips等信息 2. 输入需要查询的城市,自动爬取相应信息 3. 链接微信,发送给指定好友 思路比较清晰,主要分两块,一是爬虫 ...

  2. 1507: [NOI2003]Editor(块状链表)

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4157  Solved: 1677[Submit][Stat ...

  3. 等待唤醒(wait / notify)机制

    如果一个线程从头到尾执行完也不和别的线程打交道的话,那就不会有各种安全性问题了.但是协作越来越成为社会发展的大势,一个大任务拆成若干个小任务之后,各个小任务之间可能也需要相互协作最终才能执行完整个大任 ...

  4. luogu1742 最小圆覆盖

    狗题卡我精度--sol #include <algorithm> #include <iostream> #include <cstdlib> #include & ...

  5. day03_01 Python历史、32bit和64bit系统的区别

    先看一下讲师的笔记,有python介绍 在python2.6版本之后,想清理一些东西,追求简单明了,就直接升级到了python3.0 但是python3.0导致很多企业都不更新,因为有很多企业的网站代 ...

  6. Python学习-day5 常用模块

    day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...

  7. [java开发篇][dom4j模块] 遍历xml文件

    http://blog.csdn.net/chenleixing/article/details/44353491 在android studio 导入dom4j库(build-gradle(Moud ...

  8. Linux下安装Mysql出现的常见问题以及解决办法

     1.安装时候出现 warning: mysql-community-server-5.7.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ...

  9. 【Luogu】P4035球形空间产生器(高斯消元)

    题目链接 水比题,把圆方程展开减一下把平方都减掉半径的平方也减掉,高斯消元即可. 然后我只输出两位小数,爆了两次零.我好菜啊. #include<cstdio> #include<c ...

  10. wine-qq 安装

    http://www.bubuko.com/infodetail-343048.html