#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
const int N = 1e6;
int n, p;
int aib[1 + N];
int a[1 + N];
int val[1 + N];
inline int lsb (int x) {
return x & -x;
}
int qry (int pos) {
int ans = 0;
while (pos) {
ans += aib[pos];
pos -= lsb (pos);
}
return ans;
}
void upd (int pos, int val) {
while (pos <= n) {
aib[pos] += val;
pos += lsb (pos);
}
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> p;
vector <pair <ll, int>> v;
v.pb ({0, 0});
ll sum = 0;
for (int i = 1; i <= n; i++) {
a[i] -= p;
sum += a[i];
v.pb ({sum, i});
}
sort (v.begin (), v.end ());
int cnt = 1;
val[v[0].second] = cnt;
for (int i = 1; i <= n; i++) {
if (v[i].first != v[i - 1].first)
cnt++;
val[v[i].second] = cnt;
}
ll ans = 0;
for (int i = 0; i <= n; i++) {
ans += qry (val[i]);
upd (val[i], 1);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
300 ms |
36288 KB |
Output is correct |
5 |
Correct |
170 ms |
24292 KB |
Output is correct |
6 |
Incorrect |
256 ms |
32192 KB |
Output isn't correct |
7 |
Correct |
259 ms |
33472 KB |
Output is correct |
8 |
Correct |
234 ms |
29120 KB |
Output is correct |
9 |
Correct |
297 ms |
37664 KB |
Output is correct |
10 |
Correct |
255 ms |
32576 KB |
Output is correct |