#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
struct BIT {
int n;
vector<int> tree;
void config(int _n) {
n = _n + 5;
tree.resize(_n+10);
}
void update(int p, int v) {
for(p++; p<n; p+=p&-p) tree[p] += v;
}
int query(int p) {
int ans = 0;
for(p++; p>0; p-=p&-p) ans += tree[p];
return ans;
}
};
int main() {
int n, p, i;
cin >> n;
ll v[n+1], ans = 0; v[0] = 0;
for(i=1; i<=n; i++) cin >> v[i];
cin >> p;
ll sum = 0;
for(i=0; i<=n; i++) {
sum += v[i];
v[i] = sum - (ll)i * p;
}
vector<ll> comp;
for(int i=0; i<=n; i++) comp.push_back(v[i]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
int P;
BIT bit; bit.config(n);
for(i=0; i<=n; i++) {
P = lower_bound(comp.begin(), comp.end(), v[i]) - comp.begin();
ans += bit.query(P);
bit.update(P, 1);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
4 |
Correct |
482 ms |
19124 KB |
Output is correct |
5 |
Correct |
261 ms |
13640 KB |
Output is correct |
6 |
Correct |
379 ms |
17836 KB |
Output is correct |
7 |
Correct |
395 ms |
17840 KB |
Output is correct |
8 |
Correct |
343 ms |
16816 KB |
Output is correct |
9 |
Correct |
451 ms |
19836 KB |
Output is correct |
10 |
Correct |
386 ms |
18236 KB |
Output is correct |