[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2002

[算法]

LCT动态维护森林连通性

时间复杂度 : O(NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull; int n;
int k[MAXN]; struct Link_Cut_Tree
{
struct Node
{
int father , son[] , size;
bool tag;
} a[MAXN];
inline void init()
{
for (int i = ; i <= n; i++)
{
a[i].father = ;
a[i].son[] = a[i].son[] = ;
a[i].size = ;
a[i].tag = false;
}
}
inline void pushdown(int x)
{
if (a[x].tag)
{
swap(a[x].son[] , a[x].son[]);
a[a[x].son[]].tag ^= ;
a[a[x].son[]].tag ^= ;
a[x].tag = false;
}
}
inline void update(int x)
{
a[x].size = ;
if (a[x].son[]) a[x].size += a[a[x].son[]].size;
if (a[x].son[]) a[x].size += a[a[x].son[]].size;
}
inline bool get(int x)
{
pushdown(a[x].father);
return a[a[x].father].son[] == x;
}
inline bool nroot(int x)
{
return a[a[x].father].son[] == x | a[a[x].father].son[] == x;
}
inline void rotate(int x)
{
int f = a[x].father , g = a[f].father;
int tmpx = get(x) , tmpf = get(f);
int w = a[x].son[tmpx ^ ];
if (nroot(f)) a[g].son[tmpf] = x;
a[x].son[tmpx ^ ] = f;
a[f].son[tmpx] = w;
if (w) a[w].father = f;
a[f].father = x;
a[x].father = g;
update(f);
}
inline void access(int x)
{
for (int y = ; x; x = a[y = x].father)
{
splay(x);
a[x].son[] = y;
update(x);
}
}
inline void splay(int x)
{
int y = x , z = ;
static int st[MAXN];
st[++z] = y;
while (nroot(y)) st[++z] = y = a[y].father;
while (z) pushdown(st[z--]);
while (nroot(x))
{
int y = a[x].father , z = a[y].father;
if (nroot(y))
rotate((a[y].son[] == x) ^ (a[z].son[] == y) ? x : y);
rotate(x);
}
update(x);
}
inline void make_root(int x)
{
access(x);
splay(x);
a[x].tag ^= ;
pushdown(x);
}
inline int find_root(int x)
{
access(x);
splay(x);
while (a[x].son[])
{
pushdown(x);
x = a[x].son[];
}
return x;
}
inline void link(int x , int y)
{
make_root(x);
if (find_root(y) != x) a[x].father = y;
}
inline void cut(int x , int y)
{
make_root(x);
if (find_root(y) == x && a[x].father == y && !a[x].son[])
{
a[x].father = a[y].son[] = ;
update(y);
}
}
inline int query(int u)
{
make_root(u);
access(n + );
splay(n + );
return a[n + ].size - ;
}
inline void modify(int u , int val)
{
if (u + k[u] <= n) cut(u , u + k[u]);
else cut(u , n + );
if (u + val <= n) link(u , u + val);
else link(u , n + );
k[u] = val;
}
} LCT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} int main()
{ read(n);
LCT.init();
for (int i = ; i <= n; i++)
{
read(k[i]);
if (i + k[i] <= n) LCT.link(i , i + k[i]);
else LCT.link(i , n + );
}
int m;
read(m);
while (m--)
{
int type;
read(type);
if (type == )
{
int x;
read(x);
++x;
printf("%d\n" , LCT.query(x));
} else
{
int x , val;
read(x); read(val);
++x;
LCT.modify(x , val);
}
} return ; }

[HNOI 2010] 弹飞绵羊的更多相关文章

  1. bzoj 2002 HNOI 2010 弹飞绵羊

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  2. 【BZOJ 2002】【Hnoi 2010】弹飞绵羊 分块||Link Cut Tree 两种方法

    ShallWe,Yveh,hmy,DaD3zZ,四人吃冰糕从SLYZ超市出来后在马路上一字排开,,,吃完后发现冰糕棍上写着:“向狮子座表白:愿做你的小绵羊”,,, 好吧在这道题里我们要弹飞绵羊,有分块 ...

  3. c++之路进阶——codevs2333(弹飞绵羊)

    2333 弹飞绵羊 2010年省队选拔赛湖南  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master       题目描述 Description Lostmonk ...

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

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

  5. 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 link-cut-tree

    2016-05-30 11:51:59 用一个next数组,记录点x的下一个点是哪个 查询时,moveroot(n+1),access(x),splay(x) ,输出size[ch[x][0]]即为答 ...

  6. BZOJ-2002 弹飞绵羊 Link-Cut-Tree (分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 6801 Solved: 3573 [Submi ...

  7. 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块

    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...

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

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

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

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

随机推荐

  1. lfu-cache(需要O(1),所以挺难的)

    https://leetcode.com/problems/lfu-cache/ 很难,看了下面的参考: https://discuss.leetcode.com/topic/69137/java-o ...

  2. How to set the initial value of a select element using AngularJS ng-options & track by

    原文: https://www.gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-element ...

  3. hadoop常见错误

    hadoop常见错误集锦: 1.DataXceiver error processing WRITE_BLOCK operation ERROR org.apache.hadoop.hdfs.serv ...

  4. sql的一些知识_where

    简单相同查询,查询age=22的三列信息,并按照降序排列 ORDER BY weight DESC 此外,where还支持一下判断操作符 值得注意的是,如果比较的值为数值,则不需要加引号,如果是字符, ...

  5. C++静态库与动态库深入研究

    什么是库 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可以被操 ...

  6. php实现类似淘宝最近浏览商品的功能模型代码

    <?php //TempNum 显示临时记录数 $TempNum=5; //setcookie("RecentlyGoods", "12,31,90,39" ...

  7. 总结一下vue调试的方法

    1.最推荐使用的是vue的chrome插件: 2.平时开发中常用到的调试代码:console.log().alert():此外还有console.error()这个我用的比较少,感觉跟console. ...

  8. 发送邮件程序报错454 Authentication failed以及POP3和SMTP简介

    一.发现问题 在测试邮件发送程序的时候,发送给自己的QQ邮箱,程序报错454 Authentication failed, please open smtp flag first. 二.解决问题 进入 ...

  9. Java ClassLoader详解(转载)

    Java ClassLoader详解 类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK ...

  10. Drupal 初次使用感受,兴许补充。

    非常久曾经就接触过.下载下来安装,结果界面太丑,太难看,直接删除. 近期又一次想到开源CMS,好奇看到那么多人推崇drupal.也便下载来又一次研究了下. 刚接触了下.只是总体使用感觉非常差.尤其几个 ...