104022024-04-01 20:14:20Valaki2Barátokcpp17Accepted 100/100167ms40968 KiB
#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();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted7ms11140 KiB
2Accepted82ms18444 KiB
subtask211/11
3Accepted6ms12332 KiB
4Accepted8ms12976 KiB
5Accepted8ms13216 KiB
6Accepted8ms13408 KiB
7Accepted8ms13780 KiB
8Accepted8ms13984 KiB
9Accepted8ms14044 KiB
10Accepted8ms14232 KiB
subtask312/12
11Accepted143ms28412 KiB
12Accepted137ms29288 KiB
13Accepted141ms30112 KiB
14Accepted126ms33864 KiB
15Accepted133ms31508 KiB
16Accepted123ms35944 KiB
17Accepted135ms34024 KiB
subtask431/31
18Accepted133ms33132 KiB
19Accepted137ms34200 KiB
20Accepted125ms38712 KiB
21Accepted134ms36092 KiB
22Accepted137ms33936 KiB
23Accepted150ms35248 KiB
24Accepted112ms32624 KiB
25Accepted167ms36144 KiB
26Accepted93ms30960 KiB
subtask546/46
27Accepted143ms36584 KiB
28Accepted148ms36524 KiB
29Accepted162ms36400 KiB
30Accepted129ms39212 KiB
31Accepted136ms35536 KiB
32Accepted125ms40968 KiB
33Accepted87ms30420 KiB
34Accepted87ms30404 KiB
35Accepted136ms34032 KiB
36Accepted123ms40828 KiB
37Accepted136ms33876 KiB
38Accepted160ms36292 KiB
39Accepted157ms35704 KiB
40Accepted111ms32608 KiB
41Accepted150ms35316 KiB