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 ;
}

最新文章

  1. DBlink与同义词
  2. java利用透明的图片轮廓抠图
  3. 161206、 Ionic、Angularjs、Cordova搭建Android开发环境
  4. 编译及load mydqli.so文件
  5. MCS51浮点计算程序
  6. Ant Table组件
  7. Webkit之资源加载
  8. seajs的那些坑
  9. EasyUI实现异步载入tree(整合Struts2)
  10. 前端的UI设计与交互之导航篇
  11. 解决AES算法CBC模式加密字符串后再解密出现乱码问题
  12. 基于 HTML5 WebGL 的 3D 工控裙房系统
  13. DMA 内存存取原理
  14. [转]Spark学习之路 (三)Spark之RDD
  15. 关于 RESTFUL API 安全认证方式的一些总结
  16. 转载:mybatis踩坑之——foreach循环嵌套if判断
  17. android IO流操作文件(存储和读取)
  18. A* search算法解迷宫
  19. TypeScript白鹭引擎Egret防止按钮事件冒泡穿透
  20. DOM实战

热门文章

  1. 如何在Chrome development tool里查看C4C前台发送的请求细节
  2. (转)每天一个linux命令(1):ls命令
  3. lua 语句学习
  4. public /protected/private的作用域
  5. qbxt Day4
  6. JS JavaScript实现杨辉三角
  7. 使用redux代码文件的组织方式
  8. 统计iOS产品不同渠道的下载量
  9. vim 输入特殊字符
  10. 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形