-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeforces-961E.cc
52 lines (44 loc) · 904 Bytes
/
Codeforces-961E.cc
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
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int getSum(vector <int> &BIT, int x){
int sum = 0;
while(x){
sum += BIT[x];
x -= x & (-x);
}
return sum;
}
void update(vector <int> &BIT, int x, int a){
for(int i = x; i < BIT.size(); i += i & (-i)){
BIT[i] += a;
}
}
long long calc(vector <int> &BIT, int a, int b){
return getSum(BIT, a) - getSum(BIT, b);
}
int main(){
int n;
scanf("%d", &n);
vector <int> v(n + 1);
vector <vector<int>> g(n + 1);
vector <int> f(n + 1, 0);
for(int i = 1; i <= n; i++){
scanf("%d", &v[i]);
v[i] = min(v[i], n);
int e = min(v[i], i - 1);
g[e].push_back(i - 1);
}
long long ans = 0;
for(int i = 1; i <= n; i++){
update(f, v[i], 1);
for(auto e: g[i]){
ans += calc(f, n, e);
}
}
printf("%lld", ans);
return 0;
}