#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
template<typename T>
class fenwick {
private:
int n;
vector<int> fenw;
public:
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int p;
cin >> p;
for (int i = 0; i < n; i++) {
a[i] -= p;
}
b[0] = a[0];
for (int i = 1; i < n; i++) {
a[i] += a[i - 1];
b[i] = a[i];
}
sort(b.begin(), b.end());
map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[a[i]] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
}
fenwick<int> fenw(n);
long long ans = 0ll;
for (int i = 0; i < n; i++) {
if (a[i] >= 0) {
ans++;
}
ans += (long long) (fenw.get(mp[a[i]]));
fenw.modify(mp[a[i]], +1);
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
760 KB |
Output is correct |
2 |
Correct |
7 ms |
796 KB |
Output is correct |
3 |
Correct |
5 ms |
876 KB |
Output is correct |
4 |
Execution timed out |
1068 ms |
50552 KB |
Time limit exceeded |
5 |
Execution timed out |
1076 ms |
50552 KB |
Time limit exceeded |
6 |
Execution timed out |
1078 ms |
64608 KB |
Time limit exceeded |
7 |
Execution timed out |
1087 ms |
66560 KB |
Time limit exceeded |
8 |
Execution timed out |
1086 ms |
66560 KB |
Time limit exceeded |
9 |
Execution timed out |
1086 ms |
66560 KB |
Time limit exceeded |
10 |
Execution timed out |
1085 ms |
66560 KB |
Time limit exceeded |