#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
const int maxn = 2e5;
int n;
int v[1 + maxn];
vector<int> to_upd[1 + maxn];
int tree[1 + 4 * maxn];
void update(int v, int vl, int vr, int pos, int val) {
if(vl == vr) {
tree[v] += val;
} else {
int mid = (vl + vr) / 2;
if(pos <= mid) {
update(2 * v, vl, mid, pos, val);
} else {
update(2 * v + 1, mid + 1, vr, pos, val);
}
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
}
int query(int v, int vl, int vr, int ql, int qr) {
if(ql > vr || qr < vl) {
return 0;
}
if(vl == ql && vr == qr) {
return tree[v];
}
int mid = (vl + vr) / 2;
return query(2 * v, vl, mid, ql, min(qr, mid))
+ query(2 * v + 1, mid + 1, vr, max(ql, mid + 1), qr);
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> v[i];
}
long long ans = 0;
for(int i = n; i >= 1; i--) {
ans += query(1, 1, n, 1, min(n, i + v[i]));
update(1, 1, n, i, 1);
to_upd[max(1, i - v[i])].pb(i);
for(int x : to_upd[i]) {
update(1, 1, n, x, -1);
}
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 7ms | 11140 KiB | ||||
2 | Accepted | 82ms | 18444 KiB | ||||
subtask2 | 11/11 | ||||||
3 | Accepted | 6ms | 12332 KiB | ||||
4 | Accepted | 8ms | 12976 KiB | ||||
5 | Accepted | 8ms | 13216 KiB | ||||
6 | Accepted | 8ms | 13408 KiB | ||||
7 | Accepted | 8ms | 13780 KiB | ||||
8 | Accepted | 8ms | 13984 KiB | ||||
9 | Accepted | 8ms | 14044 KiB | ||||
10 | Accepted | 8ms | 14232 KiB | ||||
subtask3 | 12/12 | ||||||
11 | Accepted | 143ms | 28412 KiB | ||||
12 | Accepted | 137ms | 29288 KiB | ||||
13 | Accepted | 141ms | 30112 KiB | ||||
14 | Accepted | 126ms | 33864 KiB | ||||
15 | Accepted | 133ms | 31508 KiB | ||||
16 | Accepted | 123ms | 35944 KiB | ||||
17 | Accepted | 135ms | 34024 KiB | ||||
subtask4 | 31/31 | ||||||
18 | Accepted | 133ms | 33132 KiB | ||||
19 | Accepted | 137ms | 34200 KiB | ||||
20 | Accepted | 125ms | 38712 KiB | ||||
21 | Accepted | 134ms | 36092 KiB | ||||
22 | Accepted | 137ms | 33936 KiB | ||||
23 | Accepted | 150ms | 35248 KiB | ||||
24 | Accepted | 112ms | 32624 KiB | ||||
25 | Accepted | 167ms | 36144 KiB | ||||
26 | Accepted | 93ms | 30960 KiB | ||||
subtask5 | 46/46 | ||||||
27 | Accepted | 143ms | 36584 KiB | ||||
28 | Accepted | 148ms | 36524 KiB | ||||
29 | Accepted | 162ms | 36400 KiB | ||||
30 | Accepted | 129ms | 39212 KiB | ||||
31 | Accepted | 136ms | 35536 KiB | ||||
32 | Accepted | 125ms | 40968 KiB | ||||
33 | Accepted | 87ms | 30420 KiB | ||||
34 | Accepted | 87ms | 30404 KiB | ||||
35 | Accepted | 136ms | 34032 KiB | ||||
36 | Accepted | 123ms | 40828 KiB | ||||
37 | Accepted | 136ms | 33876 KiB | ||||
38 | Accepted | 160ms | 36292 KiB | ||||
39 | Accepted | 157ms | 35704 KiB | ||||
40 | Accepted | 111ms | 32608 KiB | ||||
41 | Accepted | 150ms | 35316 KiB |