yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P3203 [HNOI2010] 弹飞绵羊

P3203 [HNOI2010] 弹飞绵羊

题面

题目链接

解法

好家伙,全都往后弹不就形成了一棵树的结构,然后要求动态维护,那就LCT呗,然后维护动态$dep$数组就可。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @file: P3203.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P3203
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 2e5 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;
struct LCT {
int fa[MAXN], ch[MAXN][2];
int val[MAXN], siz[MAXN], tag[MAXN];
void pushup(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; }
void connect(int x, int f, int w) { fa[x] = f, (~w) && (ch[f][w] = x); }
int get(int x) {
if (ch[fa[x]][0] == x)
return 0;
if (ch[fa[x]][1] == x)
return 1;
return -1;
}
void pushdown(int x) {
if (tag[x]) {
swap(ch[x][0], ch[x][1]);
if (ch[x][0])
tag[ch[x][0]] ^= 1;
if (ch[x][1])
tag[ch[x][1]] ^= 1;
tag[x] = 0;
}
}
void pushall(int x) {
if (~get(x))
pushall(fa[x]);
pushdown(x);
}
void rotate(int x) {
int y = fa[x], z = fa[y], w = get(x);
if (~w)
connect(ch[x][w ^ 1], y, w);
connect(x, z, get(y));
connect(y, x, w ^ 1);
pushup(y);
pushup(x);
}
void splay(int x) {
pushall(x);
for (int f = fa[x]; f = fa[x], ~get(x); rotate(x))
if (~get(f))
rotate(get(f) == get(x) ? f : x);
}
void access(int x) {
int pre = 0;
while (x) {
splay(x);
ch[x][1] = pre;
pushup(x);
pre = x;
x = fa[x];
}
}
void makeroot(int x) {
access(x);
splay(x);
tag[x] ^= 1;
}
void split(int x, int y) {
makeroot(x);
access(y);
splay(y);
}
int findroot(int x) {
access(x);
splay(x);
while (ch[x][0])
pushdown(x), x = ch[x][0];
splay(x);
return x;
}
bool link(int x, int y) {
makeroot(x);
if (findroot(y) == x)
return false;
fa[x] = y;
return true;
}
bool cut(int x, int y) {
if (findroot(x) != findroot(y))
return false;
split(x, y);
if (fa[x] != y || ch[x][1])
return false;
fa[x] = ch[y][0] = 0;
return true;
}
} lct;
int n, q, a[MAXN];
signed main() {
read(n);
for (int i = 1; i <= n; ++i)
read(a[i]), lct.siz[i] = 1;
for (int i = 1; i <= n; ++i)
lct.link(i, min(n + 1, i + a[i]));
read(q);
while (q--) {
int op, x, y;
read(op), read(x)++;
if (op == 1) {
lct.split(x, n + 1);
write(lct.siz[n + 1] - 1), putchar('\n');
} else {
read(y);
lct.cut(x, min(n + 1, x + a[x]));
a[x] = y;
lct.link(x, min(n + 1, x + a[x]));
}
}
return 0;
}

nnd现在数组越界全部都叫WA了是吧。。。