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

2002: [Hnoi2010]Bounce 弹飞绵羊的更多相关文章

  1. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9071  Solved: 4652[Submi ...

  2. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  3. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  4. bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 4055  Solved: 2172[Submi ...

  5. bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...

  6. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

  7. 【BZOJ】2002: [Hnoi2010]Bounce 弹飞绵羊

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 14802  Solved: 7507[Subm ...

  8. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...

  9. 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 时间限制: 10 Sec  内存限制: 259 MB 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他 ...

随机推荐

  1. 遍历目录树 - Unicode 模式

    =info     遍历目录树 支持 Unicode     Code by 523066680@163.com     2017-03         V0.5 使用Win32API判断目录硬链接 ...

  2. GraphQL 到底怎么用?看看这个例子就知道了

    转载自: https://www.infoq.cn/article/i5JMm54_aWrRZcem1VgH

  3. 用户级线程demo

    http://blog.csdn.net/dabing69221/article/details/17426953 前言: 前几天复习了一下多线程,发现有许多网上讲的都很抽象,所以,自己把网上的一些案 ...

  4. Android笔记之 图片自由裁剪

    前言--项目中须要用到对用户头像的裁剪和上传功能.关于裁剪.一開始是想自己来做,可是认为这个东西应该谷歌有开发吧,于是一搜索官方文档.果然有.于是.就果断无耻地用了Android自带有关于照片的自由裁 ...

  5. Vue状态管理-Bus

    1.父子组件之间进行通讯: 父组件通过属性和子组件通讯,子组件通过事件和父组件通讯.vue2.x只允许单向数据传递. 先定义一个子组件AInput.vue: <template> < ...

  6. P2939 改造路

    P2939 [USACO09FEB]改造路Revamping Trails 裸地分层图最短路 培训的时候考到过 但是-- 我考试的时候写了个基本没有的树状数组优化.然后顺利的被卡到了70分(裸的spf ...

  7. 【洛谷P3389】(模板)高斯消元

    对于高斯消元法求解线性方程组, 我的理解就类似于我们在做数学题时的加减消元法, 只是把它写成一个通用的程序运算过程 对于一个线性方程组,我们从左往右每次将一列对应的行以下的元通过加减消元消去, 每个元 ...

  8. js邮箱验证,身份证验证,正则表达式

    邮箱验证: html部分: 邮箱验证:<input type="text" id="mail" value="" / onkeyup= ...

  9. Less 常用基础知识

    LESS 中的注释 也可以额使用css 中的注释(/**/) 这种方式是可以被编译出来的. 也可以使用// 注释 不会被编译的 变量 声明变量的话一定要用@开头 例如:@变量名称:值: @test_w ...

  10. Redux学习笔记-----基础部分

    Redux的基本原则 唯一数据源(应用的状态数据应该只存储在唯一的一个store上): 保持状态只读(不能直接修改Store的状态,而是应该通过派发一个action对象来完成) 数据改变只能通过纯函数 ...