#include<bits/stdc++.h>
// 6 47
typedef long long ll;
#define NMAX 1000005
#define SMAX 2300000
using namespace std;
int bit[NMAX];
int n;
void add(int indx){
for(int i = indx; i <= n; i += (i&-i)){
bit[i] += 1;
}
}
ll query(int indx){
ll ans = 0;
for(int i = indx; i > 0; i -= (i&-i)){
ans += bit[i];
}
return ans;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
ll a[n];
for(int i = 0; i < n; i++){
cin>>a[i];
}
ll p; cin>>p;
vector<pair<ll, int> > vi;
ll zero = 0;
for(int i = n-1; i >= 0; i--){
a[i] -= p;
zero -= a[i];
ll cur = zero + a[i];
//cout<<i<<" PUSH: "<<cur<<endl;
vi.push_back({cur, i});
}
sort(vi.begin(), vi.end());
vector<int> indx(n);
for(int i = 0; i < n; i++){
indx[vi[i].second] = i;
}
ll ans = 0; zero = 0;
for(int i = n-1; i >= 0; i--){
add(indx[i]+1);
zero -= a[i];
pair<ll, int> temp = {zero, 0};
int index = lower_bound(vi.begin(), vi.end(), temp) - vi.begin();
ans += query(n) - query(index);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
6 ms |
740 KB |
Output is correct |
3 |
Correct |
5 ms |
740 KB |
Output is correct |
4 |
Correct |
644 ms |
30816 KB |
Output is correct |
5 |
Correct |
321 ms |
30816 KB |
Output is correct |
6 |
Correct |
479 ms |
30816 KB |
Output is correct |
7 |
Correct |
529 ms |
30816 KB |
Output is correct |
8 |
Correct |
467 ms |
30816 KB |
Output is correct |
9 |
Correct |
636 ms |
32020 KB |
Output is correct |
10 |
Correct |
485 ms |
32020 KB |
Output is correct |