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

Description

Solution

并不会LCT,于是考虑分块

对于每个点暴力处理出Step[i]Step[i]表示跳到下一个块需要的次数和Pos[i]Pos[i]表示跳到下一个块的位置

对于修改的点暴力重构块就行了

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
#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;
}

inline int read ()
{
int 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 = 200000 + 100, Block_Size = 520, Block_Cnt = Maxn / Block_Size + 26;

int N, M, A[Maxn], Step[Maxn], Pos[Maxn];
int Belong[Maxn], block_cnt, L[Block_Cnt], R[Block_Cnt];

inline void dfs (int x, int right)
{
/**/if (Step[x]) return ;
int to = x + A[x];
if (to > right) { Pos[x] = to; Step[x] = 1; return ;}
dfs (to, right);
if (Belong[x] == Belong[to]) { Pos[x] = Pos[to]; Step[x] = Step[to] + 1; }
else Pos[x] = to, Step[x] = 1;
}

inline void Init ()
{
for (int i = 1; i <= N; ++i) Belong[i] = (i - 1) / Block_Size + 1;
block_cnt = Belong[N];
for (int i = 1; i <= block_cnt; ++i) L[i] = (i - 1) * Block_Size + 1, R[i] = i * Block_Size;
Chkmin (R[block_cnt], N);
for (int i = 1; i <= N; ++i) if (!Step[i]) dfs(i, N);
}

inline int Query (int x)
{
int ans = 0;
while (x <= N) ans += Step[x], x = Pos[x];
return ans;
}

inline void Modify (int pos, int val)
{
A[pos] = val;
for (int i = L[Belong[pos]]; i <= R[Belong[pos]]; ++i) Step[i] = Pos[i] = 0;
for (int i = L[Belong[pos]]; i <= R[Belong[pos]]; ++i) if (!Step[i]) dfs(i, R[Belong[pos]]);
}

inline void Solve ()
{
M = read();
Init();
while (M--)
{
int op = read(), x = read() + 1;
if (op == 1) printf("%d\n", Query (x));
else
{
int y = read();
Modify (x, y);
}
}
}

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

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

Debug

  • 39L: 一开始忘记加这一行,愉快地疯狂TLE