-
Notifications
You must be signed in to change notification settings - Fork 3
/
vudu.cpp
62 lines (56 loc) · 1.27 KB
/
vudu.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<tuple<ll, int>> vt;
vt S;
vi st;
int left(int p) { return p << 1; }
int right(int p) { return (p << 1) + 1; }
void insert(int p, int L, int R, int n) {
if (L == R) st[p] = 1;
else {
int mid = (L + R) / 2;
if (L <= n && n <= mid) insert(left(p), L, mid, n);
if (mid + 1 <= n && n <= R) insert(right(p), mid + 1, R, n);
++st[p];
}
}
int query(int p, int L, int R, int n) {
if (n < L) return 0;
if (R <= n) return st[p];
int mid = (L + R) / 2;
return query(left(p), L, mid, n) + \
query(right(p), mid + 1, R, n);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, p, j;
cin >> n;
vi N(n);
for (int i = 0; i < n; ++i) cin >> N[i];
cin >> p;
ll sum = 0, res = 0;
S.resize(n);
st.assign(4 * n, 0);
for (int i = 0; i < n; ++i) {
sum += N[i] - p;
S[i] = {sum, i};
if (sum >= 0) ++res;
}
sort(S.begin(), S.end());
for (int i = 0; i < n; ++i) {
tie(sum, j) = S[i];
#ifdef DEBUG
printf("sum: %lld, index: %d, res: %lld\n", sum, j, res);
#endif
res += query(1, 0, n - 1, j);
#ifdef DEBUG
printf("new res: %lld\n", res);
#endif
insert(1, 0, n - 1, j);
}
printf("%lld\n", res);
return 0;
}