#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
struct Fen {
vector<ll> tree;
void init(int n){
tree.assign(n + 1 , 0LL);
}
void set(int i , ll val , int n){
i++;
while(i <= n){
tree[i] += val;
i += (i & (-i));
}
}
ll sum(int i){
i++;
ll ans = 0;
while(i > 0){
ans += tree[i];
i -= (i & (-i));
}
return ans;
}
void build(vector<int>& a){
int n = a.size();
for(int i = 0; i < n; i++){
set(i , a[i] , n);
}
}
};
void solve(){
int n;
cin >> n;
ll arr[n];
for(int i = 0; i < n; i++)cin >> arr[i];
ll p;
cin >> p;
for(int i = 0; i < n; i++)arr[i] -= p;
ll pre[n + 1];
pre[n] = 0;
for(int i = n - 1; i >= 0; i--){
pre[i] = pre[i + 1] + arr[i];
}
vector<pair<ll,int>> v;
for(int i = 0; i < n + 1; i++){
v.pb(mp(pre[i] , i));
}
sv(v);
int cur = 1;
for(int i = 0; i < n + 1; i++){
for(int j = i; j < n + 1 && v[i].vf == v[j].vf; j++){
pre[v[j].vs] = cur;
//cout << v[j].vs << ' ' << cur << '\n';
i = j;
}
cur++;
}
ll ans = 0;
Fen ft;
ft.init(cur + 5);
for(int i = 0; i < n; i++){
ft.set(pre[i] , 1 , cur + 5);
ll x = pre[i + 1];
ans += (i + 1LL) - ((ll)ft.sum(x - 1));
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
247 ms |
47396 KB |
Output is correct |
5 |
Correct |
127 ms |
25416 KB |
Output is correct |
6 |
Correct |
200 ms |
41952 KB |
Output is correct |
7 |
Correct |
218 ms |
43644 KB |
Output is correct |
8 |
Correct |
182 ms |
30592 KB |
Output is correct |
9 |
Correct |
248 ms |
49004 KB |
Output is correct |
10 |
Correct |
210 ms |
42512 KB |
Output is correct |