最开始没发现是 LCT,还以为要搞什么模拟,看了题解才发现是 LCT 题。
每个点都只会和另一个点连一条边,如果那个点大小超过 就不连(经典的 ),差点记得两个操作都要加 。然后我们会发现这构成了一个森林,而且很好的事,你不需要换根,所以要写的东西少很多,因为你不需要求随便两个点的路径信息,而是求一个点到树根,这需要打通两点道路,然后直接 splay,相当于这道题自动帮你 make_root() 了,然后由于是要弹的次数而非点数,弹飞不算,所以答案要减一。修改就先断边再连边,连边也要注意点的大小,断边 splay 转上去以后左儿子就是父亲,直接设为 0 就行,最后别忘了 push_up()。
代码:
//Just Sayori
#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 500005
#define M 1000000007
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9')
ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int sta[39], top = 0;
if (x < 0)
pr('-'), x = -x;
do
sta[++top] = x % 10, x /= 10;
while (x);
while (top)
pr(sta[top--] ^ 48);
}
int n, m, k, u, v;
#define fa(x) s[x].fa
#define lc(x) s[x].ch[0]
#define rc(x) s[x].ch[1]
#define is_root(x) lc(fa(x))==x||rc(fa(x))==x
struct tree
{
int ch[2];
int fa, tag;
int w, sum;
} s[N << 1];
inline void push_up(int u)
{
s[u].sum = s[lc(u)].sum + s[rc(u)].sum + 1;
}
inline void rotate(int x)
{
int y = fa(x), z = fa(y);
bool k1 = rc(y) == x, k2 = rc(z) == y;
if (is_root(y))
s[z].ch[k2] = x;
fa(x) = z;
s[y].ch[k1] = s[x].ch[k1 ^ 1];
fa(s[x].ch[k1 ^ 1]) = y;
s[x].ch[k1 ^ 1] = y;
fa(y) = x;
push_up(y), push_up(x);
}
void splay(int x)
{
while (is_root(x))
{
int y = fa(x), z = fa(y);
if (is_root(y))
(rc(y) == x)^(rc(z) == y) ? rotate(x) : rotate(y);
rotate(x);
}
}
void access(int x)
{
for (rnt y = 0; x; y = x, x = fa(x))
{
splay(x);
rc(x) = y;
push_up(x);
}
}
int main()
{
n = read();
for (rnt i = 1; i <= n; i++)
{
k = read(), s[i].sum = 1;
if (i + k <= n)
fa(i) = i + k;
}
m = read();
for (rnt i = 1; i <= m; i++)
switch (read())
{
case 1:
u = read() + 1;
access(u), splay(u);
write(s[u].sum), pr(10);
break;
case 2:
u = read() + 1, v = read();
access(u), splay(u);
fa(lc(u)) = 0;
lc(u) = 0;
if (u + v <= n)
fa(u) = u + v;
push_up(u);
break;
}
return 0;
}