Lostmonkey在地上沿着一条直线摆上nn个装置,每个装置设定初始弹力系数kik_i。当绵羊达到第ii个装置时,它会往后弹kik_i步,达到第i+kii+k_i个装置,若不存在第i+kii+k_i个装置,则绵羊被弹飞。绵羊想知道当它从第ii个装置起步时,被弹几次后会被弹飞。

为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。

n200000,m100000n\le 200000,m\le 100000

Luogu P3203

BZOJ2002

Solution

此题分块做法

每个点有且仅有一条出边,可以看作是树上的父亲边

新建一个n+1n+1的节点,表示到它就结束

就转化成动态维护每个点到n+1n+1的路径长度

LCT裸题,每次暴力linkcut,再维护一下Splay里的size(相当于是链的长度,而并非真正的子树信息)即可

Code

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
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void proc_status()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl;
}

template <typename T> T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int Maxn = 2e5 + 100;

int N, M, A[Maxn];

namespace LCT
{
#define ls (Tree[x].ch[0])
#define rs (Tree[x].ch[1])
struct tree
{
int fa, ch[2], rev, size;
}Tree[Maxn];

int Stack[Maxn], top;

inline int is_root (int x) { return Tree[Tree[x].fa].ch[0] != x && Tree[Tree[x].fa].ch[1] != x; }
inline int judge_dir (int x) { return Tree[Tree[x].fa].ch[1] == x; }
inline void push_up (int x) { Tree[x].size = Tree[ls].size + Tree[rs].size + 1; }
inline void push_down (int x) { if (Tree[x].rev) swap(ls, rs), Tree[ls].rev ^= 1, Tree[rs].rev ^= 1, Tree[x].rev = 0; }
inline void connect (int x, int f, int dir) { Tree[x].fa = f, Tree[f].ch[dir] = x; }
inline void rotate (int x)
{
int f = Tree[x].fa, anc = Tree[f].fa, dirx = judge_dir(x), dirf = judge_dir(f);
if (!is_root(f)) Tree[anc].ch[dirf] = x; Tree[x].fa = anc;
connect (Tree[x].ch[dirx ^ 1], f, dirx);
connect (f, x, dirx ^ 1);
/**/ push_up(f), push_up(x);
}
inline void splay (int x)
{
/**/ Stack[++top] = x; for (int y = x; !is_root(y); y = Tree[y].fa) Stack[++top] = Tree[y].fa;
while (top) push_down (Stack[top--]);
for (; !is_root(x); rotate(x)) if (!is_root(Tree[x].fa)) rotate (judge_dir(x) == judge_dir(Tree[x].fa) ? Tree[x].fa : x);
}
inline void access (int x) { for (int y = 0; x; y = x, x = Tree[x].fa) splay (x), rs = y, push_up (x); }
inline void make_root (int x) { access (x), splay (x), Tree[x].rev ^= 1; }
inline void link (int x, int y) { make_root(x); Tree[x].fa = y; }
/**/inline void cut (int x, int y) { make_root(x), access(y), splay(y); Tree[x].fa = Tree[y].ch[0] = 0; push_up(y); }
inline int query (int x) { make_root(N + 1), access(x), splay(x); return Tree[x].size; }
}

inline void Solve ()
{
for (int i = 1; i <= N; ++i)
if (i + A[i] <= N) LCT :: link (i, i + A[i]);
else LCT :: link (i, N + 1);
M = read<int>();
while (M--)
{
int op = read<int>(), x = read<int>() + 1;
if (op == 1) printf("%d\n", LCT :: query (x) - 1);
else
{
int k = read<int>();
if (x + A[x] <= N) LCT :: cut (x, x + A[x]);
else LCT :: cut (x, N + 1);
A[x] = k;
if (x + A[x] <= N) LCT :: link (x, x + A[x]);
else LCT :: link (x, N + 1);
}
}
}

inline void Input ()
{
N = read<int>();
for (int i = 1; i <= N; ++i) A[i] = read<int>();
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}

Debug

  • 58L, 69L:一定要记得push_up!!!
  • 62L:一定要记得push_down!!!