2002: [Hnoi2010]Bounce 弹飞绵羊
2024-08-25 09:30:53
2002: [Hnoi2010]Bounce 弹飞绵羊
https://www.lydsy.com/JudgeOnline/problem.php?id=2002
分析:
绵羊在弹飞的路径中相当于一棵树,这棵树需要更改形态,删一条边,加一条边,所以LCT维护一下。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int ch[N][],fa[N],rev[N],val[N],siz[N],sk[N],a[N],Top,n; void pushup(int x) {
siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
}
void pushdown(int x) {
if (rev[x]) {
rev[ch[x][]] ^= ; rev[ch[x][]] ^= ;
swap(ch[x][], ch[x][]);
rev[x] ^= ;
}
}
bool isroot(int x) {
return ch[fa[x]][] != x && ch[fa[x]][] != x;
}
int son(int x) {
return x == ch[fa[x]][];
}
void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
void splay(int x) {
sk[Top = ] = x;
for (int i=x; !isroot(i); i=fa[i]) sk[++Top] = fa[i];
while (Top) pushdown(sk[Top--]);
while (!isroot(x)) {
int y = fa[x];
if (isroot(y)) rotate(x);
else {
if (son(x) == son(y)) rotate(y), rotate(x);
else rotate(x), rotate(x);
}
}
}
void access(int x) {
for (int last=; x; last=x, x=fa[x]) {
splay(x); ch[x][] = last; pushup(x);
}
}
void makeroot(int x) {
access(x); splay(x); rev[x] ^= ;
}
int find(int x) {
access(x); splay(x);
while (ch[x][]) x = ch[x][];
return x;
}
void link(int x,int y) {
makeroot(x);
fa[x] = y;
}
void cut(int x,int y) {
makeroot(x); access(y); splay(y);
if (fa[x] == y && !ch[x][]) fa[x] = ch[y][] = ;
} void query() {
makeroot(n + );
int p = read() + ;
access(p);
splay(p);
printf("%d\n",siz[p] - );
}
void change() {
int p = read() + ,x = read(),t = p+x > n+ ? n+: p+x; // --- 判断是否大于n+1,不判luogu上80
cut(p,a[p]);
a[p] = t;
link(p,a[p]);
} int main() {
n = read();
for (int i=; i<=n; ++i) {
a[i] = i + read();
a[i] = a[i] > n+ ? n+ : a[i];
}
for (int i=; i<=n; ++i) link(i,a[i]);
int m = read();
while (m--) {
int opt = read();
if (opt == ) query();
else change();
}
return ;
}
最新文章
- DBlink与同义词
- java利用透明的图片轮廓抠图
- 161206、 Ionic、Angularjs、Cordova搭建Android开发环境
- 编译及load mydqli.so文件
- MCS51浮点计算程序
- Ant Table组件
- Webkit之资源加载
- seajs的那些坑
- EasyUI实现异步载入tree(整合Struts2)
- 前端的UI设计与交互之导航篇
- 解决AES算法CBC模式加密字符串后再解密出现乱码问题
- 基于 HTML5 WebGL 的 3D 工控裙房系统
- DMA 内存存取原理
- [转]Spark学习之路 (三)Spark之RDD
- 关于 RESTFUL API 安全认证方式的一些总结
- 转载:mybatis踩坑之——foreach循环嵌套if判断
- android IO流操作文件(存储和读取)
- A* search算法解迷宫
- TypeScript白鹭引擎Egret防止按钮事件冒泡穿透
- DOM实战