#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define ins insert
#define mp make_pair
#define pb push_back
#define all(x) begin(x), end(x)
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
const int mx = 1000005;
int N;
ll a[mx];
ll P;
int bt[mx];
void upd(int pos, int val){
for(int i = pos; i < mx; i+=i&-i){
bt[i]+=val;
}
}
int sum(int pos){
int res = 0;
for(int i = pos; i > 0; i-=i&-i){
res+=bt[i];
}
return res;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> N;
for(int i = 1; i <= N; i++){
cin >> a[i];
}
cin >> P;
for(int i = 1; i <= N; i++){
a[i]-=P;
}
for(int i = 1; i <= N; i++){
a[i]+=a[i-1];
}
vi v;
for(int i = 0; i <= N; i++){
v.pb(i);
}
auto sort_func = [&](const int i, const int j){
return mp(a[i], i) < mp(a[j], j);
};
sort(all(v), sort_func);
ll ans = 0;
for(auto u: v){
//cout << u << "\n";
ans+=ll(sum(u+1));
upd(u+1, 1);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
285 ms |
24992 KB |
Output is correct |
5 |
Correct |
151 ms |
8932 KB |
Output is correct |
6 |
Correct |
237 ms |
17176 KB |
Output is correct |
7 |
Correct |
246 ms |
20424 KB |
Output is correct |
8 |
Correct |
230 ms |
12464 KB |
Output is correct |
9 |
Correct |
287 ms |
25668 KB |
Output is correct |
10 |
Correct |
237 ms |
18248 KB |
Output is correct |