forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
78 lines (68 loc) · 2.26 KB
/
E.cpp
File metadata and controls
78 lines (68 loc) · 2.26 KB
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
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 100111;
const int BLOCK = 511;
int blockId[MN], a[MN], lastInBlock[MN], cntBlock[MN];
int n, q;
pair<int,int> get(int i) {
int to = i + a[i];
if (to > n) return make_pair(i, 0);
else if (blockId[to] != blockId[i]) return make_pair(i, 0);
else return make_pair(lastInBlock[to], cntBlock[to] + 1);
}
void init() {
FOR(i,1,n) blockId[i] = i / BLOCK;
FORD(i,n,1) {
auto t = get(i);
lastInBlock[i] = t.first;
cntBlock[i] = t.second;
}
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
while (scanf("%lld%lld", &n, &q) == 2) {
FOR(i,1,n) scanf("%lld", &a[i]);
init();
while (q--) {
int typ; scanf("%lld", &typ);
if (typ == 0) {
int u, val; scanf("%lld%lld", &u, &val);
int b = blockId[u];
a[u] = val;
FORD(i,u,1) {
if (blockId[i] != b) break;
auto t = get(i);
lastInBlock[i] = t.first;
cntBlock[i] = t.second;
}
}
else {
int u; scanf("%lld", &u);
int res = 0;
while (true) {
// we go to last position in u 's block
res += cntBlock[u];
u = lastInBlock[u];
// from here we try to go to next block
int to = u + a[u];
++res;
if (to > n) break;
u = to;
}
printf("%lld %lld\n", u, res);
}
}
}
}