54042023-05-12 16:57:19szilBarátokcpp14Accepted 100/100174ms31016 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200001;

int a[MAXN], tree[2*MAXN], n;
vector<int> extra[MAXN];

void upd(int u) {
    u += n;
    tree[u]++;
    for (u /= 2; u >= 1; u /= 2) {
        tree[u] = tree[2*u]+tree[2*u+1];
    }
}

int qry(int l, int r) {
    l += n; r += n;
    int res = 0;
    while (l <= r) {
        if (l % 2 == 1) res += tree[l++];
        if (r % 2 == 0) res += tree[r--];
        l /= 2; r /= 2;
    }
    return res;
}


int main()
{
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        int l = max(1, i-a[i]);
        extra[l].push_back(i);
    }

    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += qry(i, n);
        for (int j : extra[i]) {
            ans -= qry(j, n);
        }
        upd(min(n, i+a[i]));
    }
    cout << ans << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted6ms11180 KiB
2Accepted96ms17496 KiB
subtask211/11
3Accepted96ms17496 KiB
4Accepted6ms11744 KiB
5Accepted9ms12144 KiB
6Accepted8ms12368 KiB
7Accepted8ms12436 KiB
8Accepted8ms12696 KiB
9Accepted8ms12584 KiB
10Accepted8ms12824 KiB
11Accepted8ms12588 KiB
subtask312/12
12Accepted8ms12588 KiB
13Accepted130ms25056 KiB
14Accepted126ms25224 KiB
15Accepted130ms25188 KiB
16Accepted115ms28644 KiB
17Accepted127ms25464 KiB
18Accepted112ms29812 KiB
19Accepted126ms27028 KiB
subtask431/31
20Accepted126ms27028 KiB
21Accepted118ms25664 KiB
22Accepted131ms25664 KiB
23Accepted111ms29680 KiB
24Accepted128ms26660 KiB
25Accepted158ms23388 KiB
26Accepted167ms24840 KiB
27Accepted145ms20220 KiB
28Accepted166ms26068 KiB
29Accepted136ms17884 KiB
subtask546/46
30Accepted136ms17884 KiB
31Accepted136ms26468 KiB
32Accepted150ms26396 KiB
33Accepted174ms26364 KiB
34Accepted115ms29124 KiB
35Accepted148ms24632 KiB
36Accepted111ms30808 KiB
37Accepted130ms17368 KiB
38Accepted131ms17660 KiB
39Accepted152ms24112 KiB
40Accepted112ms31016 KiB
41Accepted153ms24048 KiB
42Accepted158ms26848 KiB
43Accepted165ms26264 KiB
44Accepted144ms20916 KiB
45Accepted162ms25644 KiB