bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【LCT】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002
第一道LCT,调了3天,发现是智障bug,我的青春。。。
主要参考了黄学长的代码,也没啥好说的,反正就是LCT,就当存一份模版好了。
#include <cstdio> const int maxn = 200005; int n, m, t1, t2, t3, ori_tree_fa[maxn];
int left[maxn], right[maxn], fa[maxn], siz[maxn], root;
int stk[maxn], top, pushdown_tem;
bool rev[maxn]; inline void pushup(int x) {
siz[x] = siz[left[x]] + siz[right[x]] + 1;
}
inline void pushdown(int x) {
if (rev[x]) {
rev[x] = false;
rev[left[x]] = !rev[left[x]];
rev[right[x]] = !rev[right[x]];
pushdown_tem = left[x];
left[x] = right[x];
right[x] = pushdown_tem;
}
}
inline bool isroot(int x) {
return left[fa[x]] != x && right[fa[x]] != x;
}
inline void leftrotate(int x) {
int y = fa[x];
if (!isroot(y)) {
if (y == left[fa[y]]) {
left[fa[y]] = x;
}
else {
right[fa[y]] = x;
}
}
fa[x] = fa[y];
right[y] = left[x];
fa[left[x]] = y;
fa[y] = x;
left[x] = y;
pushup(y);
pushup(x);
}
inline void rightrotate(int x) {
int y = fa[x];
if (!isroot(y)) {
if (y == left[fa[y]]) {
left[fa[y]] = x;
}
else {
right[fa[y]] = x;
}
}
fa[x] = fa[y];
left[y] = right[x];
fa[right[x]] = y;
fa[y] = x;
right[x] = y;
pushup(y);
pushup(x);
}
inline void splay(int x) {
int p;
top = 0;
stk[top++] = x;
for (int i = x; !isroot(i); i = fa[i]) {
stk[top++] = fa[i];
}
for (int i = top - 1; i >= 0; --i) {
pushdown(stk[i]);
}
while (!isroot(x)) {
p = fa[x];
if (isroot(p)) {
if (x == right[p]) {
leftrotate(x);
}
else {
rightrotate(x);
}
}
else {
if (p == right[fa[p]]) {
if (x == right[p]) {
leftrotate(p);
leftrotate(x);
}
else {
rightrotate(x);
leftrotate(x);
}
}
else {
if (x == right[p]) {
leftrotate(x);
rightrotate(x);
}
else {
rightrotate(p);
rightrotate(x);
}
}
}
}
}
inline void acc(int x) {
int y = 0;
while (x) {
splay(x);
right[x] = y;
pushup(x);
y = x;
x = fa[x];
}
}
inline void make_root(int x) {
acc(x);
splay(x);
rev[x] = !rev[x];
}
inline void joyn(int x, int y) {
make_root(x);
fa[x] = y;
splay(x);
}
inline void cutt(int x, int y) {
make_root(x);
acc(y);
splay(y);
left[y] = fa[x] = 0;
pushup(y);
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d", &n);
root = n + 1;
for (int i = 1; i <= n; ++i) {
scanf("%d", &t2);
t2 = i + t2 < root? i + t2: root;
siz[i] = 1;
fa[i] = ori_tree_fa[i] = t2;
}
siz[root] = 1;
scanf("%d", &m);
while (m--) {
scanf("%d%d", &t1, &t2);
++t2;
if (t1 == 1) {
make_root(root);
acc(t2);
splay(t2);
printf("%d\n", siz[left[t2]]);
}
else {
scanf("%d", &t3);
t3 = t2 + t3 < root? t2 + t3: root;
cutt(t2, ori_tree_fa[t2]);
joyn(t2, t3);
ori_tree_fa[t2] = t3;
}
}
return 0;
}
我说的那个智障bug是指,splay开时前,pushdown那里,我错写成了
for (int i = x; !isroot(i); i = fa[i]) {
stk[top++] = i;
}
被这个弄了好久。
bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【LCT】的更多相关文章
- [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用 ...
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT
Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...
- bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块
Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...
随机推荐
- 前端3D、动画相关开源JS
WebGL http://taobaofed.org/blog/2015/12/21/webgl-handbook/ D3 (或者叫 D3.js )是一个基于 web 标准的 JavaScript 可 ...
- Java 8 中的 java.util.Optional
Java 8 中的 java.util.Optional 学习了:https://blog.csdn.net/sun_promise/article/details/51362838 package ...
- 用户代码未处理 UpdateException
无法更新 EntitySet"Project_project",由于它有一个 DefiningQuery.而 <ModificationFunctionMapping> ...
- 在学习c++过程中,总结类的三个用户以及使用权限,感觉非常实用
首先我们需要知道类的三个用户分别是:类的实现者,类的普通用户和类的继承者(派生类),接下来分别讲解这几种用户的区别. 1 .类的实现者:顾明思议,就是类的设计者,拥有最大的权限,可以访问类中任何权限的 ...
- Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu
While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...
- LruCache & DiskLruCache
在用户界面(UI)加载一张图片时很简单,然而,如果你需要加载多张较大的图像,事情就会变得更加复杂,.在许多情况下(如与像的ListView GridView或ViewPager的组件),屏幕上的图片的 ...
- 2015/12/29 eclipse应用 输出三角形
public class Myfirst { public static void main(String[] args) { System.out.println("hello world ...
- 修改系统时间(取得服务器时间,使用SetLocalTime API函数,需要UAC权限)
我的客户遇到系统时间不对,自己又不会改,于是想到利用服务端时间来修改本地的系统时间. 第一步,把下面xml存成uac.xml文件备用. <?xml version="1.0" ...
- session关联接口
#coding:utf-8 import requests,json,re ''' session关联接口,第一步访问登录接口,headers中要有cookie,不然会登录失败,登录成功后,添加随笔保 ...
- hdu1026(bfs+优先队列+打印路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...