BZOJ2002弹飞绵羊
动态树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弹飞绵羊的更多相关文章
- BZOJ-2002 弹飞绵羊 Link-Cut-Tree (分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 6801 Solved: 3573 [Submi ...
- bzoj2002 弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- [Link-Cut-Tree][BZOJ2002]弹飞绵羊
题面 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上\(n\)个 ...
- bzoj2002 弹飞绵羊 lct版
这道题就是维护一个有根的lct 一开始建树全部建虚边 求多少次弹出就是求他到根的距离(根为n+1) 这里有个小技巧 将n+1作为根而没有虚根操作起来会比较方便 #include<cstdio&g ...
- bzoj2002 弹飞绵羊 分块
这道题是分块的初尝试 讲给定的区间n进行分块处理 这个每次修改的复杂的只有logn 很方便 代码是学黄学长的 http://hzwer.com/3505.html 当然里面还是有一定我自己的想法在里面 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2843 Solved: 1519[Submi ...
- 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块
[bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...
- 【BZOJ2002】弹飞绵羊(Link-Cut Tree)
[BZOJ2002]弹飞绵羊(Link-Cut Tree) 题面 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lost ...
- 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊
BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊 Solution 很早以前写的一道分块题,最近在搞LCT,又做了一遍. 1.LCT做法 看到这种动态修改,想下LCT怎么维护. 修改操作就 ...
随机推荐
- leetcode-22-string
521. Longest Uncommon Subsequence I find the longest uncommon subsequence of this group of two strin ...
- leetcode-11-dfs
DFS算法: explore(G, v) visited(v) = trueprevisit(v) for each edge(v, u) in E: if not visited(u): explo ...
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Windows清理打印池的方法
另存为bat运行 @echo off title 快速清除打印队列 echo. echo 停止打印机服务 net stop spooler>nul echo. del /q /f %wind ...
- MySQL基本命令和常用数据库对象
MySQL基本命令: 连接远程主机的MySQL服务(为了保证安全性,执行下面命令时,可以省略-p后面的密码,执行命令后系统会提示输入密码) mysql -p 密码 -u 用户名 -h 主机地址 --d ...
- 程序员必需知道的Mac OS使用技巧
macos sierra正式版发布了,于是我把我沉寂了一年没有用过了的macbook拿出来玩玩,顺便把一些常用技巧mark. 1.apple store下载软件无响应(经常出现的问题) 解决方法:更改 ...
- Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比
1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...
- 再谈H2的MVStore与MVMap
对H2的[MVStore]: http://www.cnblogs.com/simoncook/p/5188105.html 这篇文章的补充. 概述 我们通常用的map,比如HashMap Linke ...