-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrange_count.cpp
96 lines (79 loc) · 2.31 KB
/
range_count.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// added
#include <bits/stdc++.h>
using namespace std;
class RangeCount {
const int ST_SIZE = (1 << 20) - 1;
int n;
vector<int> data;
vector<vector<int>> segtree;
void init(int k, int l, int r) {
if (r - l == 1) {
segtree[k].push_back(data[l]);
} else {
int lch = k * 2 + 1;
int rch = k * 2 + 2;
init(lch, l, (l + r) / 2);
init(rch, (l + r) / 2, r);
segtree[k].resize(r - l);
merge(segtree[lch].begin(), segtree[lch].end(), segtree[rch].begin(), segtree[rch].end(),
segtree[k].begin());
}
}
// number of x in [i, j)
int query(int i, int j, int x, int k, int l, int r) {
if (j <= l || r <= i) {
return 0;
}
if (i <= l && r <= j) {
return upper_bound(segtree[k].begin(), segtree[k].end(), x) - segtree[k].begin();
}
int lc = query(i, j, x, k * 2 + 1, l, (l + r) / 2);
int rc = query(i, j, x, k * 2 + 2, (l + r) / 2, r);
return lc + rc;
}
public:
RangeCount(const vector<int> &v) {
n = v.size();
data = vector<int>(v);
segtree = vector<vector<int>>(ST_SIZE);
init(0, 0, n);
}
int exact(int i, int j, int x) {
return query(i, j, x, 0, 0, n) - query(i, j, x - 1, 0, 0, n);
}
int le(int i, int j, int x) {
return query(i, j, x, 0, 0, n);
}
int lt(int i, int j, int x) {
return query(i, j, x - 1, 0, 0, n);
}
int ge(int i, int j, int x) {
return query(i, j, 1e9, 0, 0, n) - query(i, j, x - 1, 0, 0, n);
}
int gt(int i, int j, int x) {
return query(i, j, 1e9, 0, 0, n) - query(i, j, x, 0, 0, n);
}
};
int main() {
int n;
cin >> n;
vector<int> vs(n);
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> vs[i];
vs[i]--;
}
RangeCount cnt(vs);
for (int i = 0; i < n; i++) {
if (vs[i] >= i) {
ans += cnt.ge(0, i, vs[i]);
ans += cnt.le(i, vs[i] + 1, vs[i]) - 1;
ans += cnt.le(vs[i] + 1, n + 1, vs[i]);
} else {
ans += cnt.ge(0, vs[i], vs[i]);
ans += cnt.ge(vs[i], i + 1, vs[i]) - 1;
ans += cnt.le(i + 1, n + 1, vs[i]);
}
}
cout << ans / 2 << endl;
}