LCT
刚学LCT,对LCT的性质不太熟练,还需要多多练习。。
对每一个点,将其与它能够到达的点连一条虚边。弹出去的话就用n+1这个节点表示。
第一种操作我们需要从LCT的性质入手,问的问题其实就是x通过多少条边可以到达n+1这个点。。那么我们可以把他们两拉成一条链(也就是split(n + 1, x)),这样就把x splay到根了,根据LCT的性质,在x和n+1联通的这棵splay中,x一定没有右子树,因为他是深度最大的点。那么左子树所有点就是原树中x到n+1的路径上的所有点了。。直接输出答案size就好了
第二种操作很简单断边再连边就行了,因为这题的边一定合法,所以没什么需要考虑的特殊情况
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 200005;
int n, tot, ch[N][2], size[N], fa[N], st[N], rev[N], f[N];
int newNode(){
size[++tot] = 1, fa[tot] = ch[tot][0] = ch[tot][1] = 0;
return tot;
}
bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
}
void push_up(int x){
size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
}
void push_down(int x){
if(rev[x]){
reverse(ch[x][0]), reverse(ch[x][1]);
rev[x] ^= 1;
}
}
void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
}
void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
rotate(y);
}
rotate(x);
}
push_up(x);
}
void access(int x){
for(int p = 0; x; p = x, x = fa[x]){
splay(x), ch[x][1] = p, push_up(x);
}
}
void makeRoot(int x){
access(x), splay(x), reverse(x);
}
void link(int x, int y){
makeRoot(x), fa[x] = y, push_up(y);
}
void split(int x, int y){
makeRoot(x), access(y), splay(y);
}
void cut(int x, int y){
split(x, y);
fa[x] = ch[y][0] = 0, push_up(y);
}
int main(){
n = read();
for(int i = 1; i <= n + 1; i ++) newNode();
for(int i = 1; i <= n; i ++){
int k = read(), t = min(i + k, n + 1);
link(i, t), f[i] = t;
}
int m = read();
while(m --){
int opt = read();
if(opt == 1){
int x = read(); x ++;
split(n + 1, x); printf("%d
", size[x] - 1);
}
else{
int x = read(), y = read(); x ++;
cut(x, f[x]);
int t = min(x + y, n + 1);
link(x, t), f[x] = t;
}
}
return 0;
}