题解【bzoj2002 [Hnoi2010]Bounce 弹飞绵羊】
Description
给 \(n\) 个点以及它们的弹力系数 \(k_i\) ,含义为 可以弹到 \(i + k_i\) 的位置。
支持两个东西,修改一个点的弹力系数;求一个点要弹多少次弹出 \(n\)
Solution
用 LCT 做。弹力系数是 \(k_i\) 可以看作是 \(i\) 和 \(i+k_i\) 连了一条边。如果弹出去了就不妨设和 \(0\) 连一条边。
对于修改操作,先把原来的边删除,修改 k 数组,再连上新边
对于查询操作,维护一个子树大小 siz (这里是 splay 上的 siz,不是原树上的),然后询问就相当于当前这个点 \(u\) 到 \(0\) 这条链上有几个点。所以就 split 出来这条链然后输出 siz - 1 就行了(注意要减 \(1\) 因为问的是弹多少次)
然后就做完了(注意输入的时候要加 1)
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int n, m, K[N];
struct node {
int siz, rev;
node *ch[2], *prt;
int dir() { return prt->ch[1] == this; }
int isr() { return !prt || (prt->ch[0] != this && prt->ch[1] != this); }
void setc(node *p, int k) { this->ch[k] = p; if(p) p->prt = this; }
void upd() { int s = 1; if(ch[0]) s += ch[0]->siz; if(ch[1]) s += ch[1]->siz; siz = s; }
void push() { if(!rev) return ; swap(ch[0], ch[1]);
if(ch[0]) ch[0]->rev ^= 1; if(ch[1]) ch[1]->rev ^= 1; rev = 0; }
}*P[N], pool[N], *cur = pool; node *sta[N]; int top;
node *New() { node *p = cur++; p->siz = 1; return p; }
void rotate(node *p) {
node *prt = p->prt; int k = p->dir();
if(!prt->isr()) prt->prt->setc(p, prt->dir());
else p->prt = prt->prt; prt->setc(p->ch[!k], k);
p->setc(prt, !k); prt->upd(); p->upd();
}
void splay(node *p) {
node *q = p;
while(1) { sta[++top] = q; if(q->isr()) break; q = q->prt; }
while(top) sta[top--]->push();
while(!p->isr()) {
if(p->prt->isr()) rotate(p);
else if(p->dir() == p->prt->dir()) rotate(p->prt), rotate(p);
else rotate(p), rotate(p);
} p->upd();
}
node *access(node *p) { node *q = 0; for(; p; p = p->prt) splay(p), p->ch[1] = q, (q = p)->upd(); return q; }
void mkroot(node *p) { access(p); splay(p); p->rev ^= 1; p->push(); }
void split (node *p, node *q) { mkroot(p); access(q); splay(p); }
void link (node *p, node *q) { mkroot(p); mkroot(q); p->setc(q, 1); }
void cut (node *p, node *q) { split(p, q); p->ch[1] = q->prt = 0; }
int main() {
scanf("%d", &n); P[0] = New();
for(int i = 1; i <= n; i++) scanf("%d", &K[i]), P[i] = New();
for(int i = 1; i <= n; i++) {
if(i + K[i] <= n) link(P[i], P[i + K[i]]);
else link(P[i], P[0]);
} int m; scanf("%d", &m);
for(int i = 1; i <= m; i++) {
int op, u; scanf("%d %d", &op, &u); u++;
if(op == 1) {
split(P[0], P[u]); printf("%d\n", P[0]->siz - 1);
}
if(op == 2) { int k; scanf("%d", &k);
if(u + K[u] <= n) cut(P[u], P[u + K[u]]);
else cut(P[u], P[0]); K[u] = k;
if(u + K[u] <= n) link(P[u], P[u + K[u]]);
else link(P[u], P[0]);
}
}
return 0;
}
题解【bzoj2002 [Hnoi2010]Bounce 弹飞绵羊】的更多相关文章
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
- [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 弹飞绵羊(LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...
- 【lct】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊
lct板子,此题主要有cut操作和link操作. #include<cstdio> #include<iostream> #include<cstring> #in ...
随机推荐
- 随手记录-linux-添加epel源
下载各种yum源 https://opsx.alibaba.com/mirror https://blog.csdn.net/harbor1981/article/details/51135623
- Django_rest_framework_组件(authentication、permission、throttle)
认证组件 说明 from rest_framework.authentication import BaseAuthentication class TestAuthentication(BaseAu ...
- “Hello World!”团队第六周第七次会议
博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.checkout&push代码 一.会议时间 2017年11月23日 ...
- ModelAndView在页面上取值时value="{contextConfigLocation=<NONE>}"
后台: mv.getModel().put("initParam", 1); 页面: <input type="hidden" id="init ...
- 【Alpha发布】贡献分分配
最后贡献分分配: (1211)王嘉豪:32 (1186)黄雨萌:36 (1182)佘彦廷:40 (1208)何小松:50 (1200)鲁聃:62 (1174)邢浩:64 (1193)刘乾:66
- validating & update ctabfolder css
总是查错 结果把validating全部都反选,然后老是update ctabfolder css update ctabfolder css has encountered a problem An ...
- HDU 4336 Card Collector 期望dp+状压
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...
- Sprint--5.21
看到作业要求组长就召开小组成员开了一个简短的会议,会议内容大致是这样的: 1.再次明确任务:就是每一个人都要清楚知道自己扮演的角色应该做些什么,怎么去做: 2.组长定时更新博客,每一位小组成员也要写进 ...
- week2-作业2
项目地址:https://git.coding.net/Rainoob/calculate.git ·1.需求分析:程序可以根据输入的参数n随机产生n道四则运算计算题,每个数字在0-100之间.运算符 ...
- dubbo面向服务使用
首先启动zookeeper dubbo集群,使用两个dubbo,一个服务,一个调用,使用zookeeper管理 zeekeeper的功能:管理集群,保证集群成员的数据一致性和动作的协调 服务端: se ...