#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ld long double
#define ll long long
//#define int ll
#define ii pair<ll,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int A[1000005],fen[1000005],n,k,idx=1;
ll h,ans;
void upd(int i,int val){
for (; i <= n+1; i+=(i&(-i)))
fen[i]+=val;
}
int get(int i){
int l=0;
for (; i >= 1; i-=(i&(-i)))l+=fen[i];
return l;
}
int sum(int a,int b){
return get(b)-get(a-1);
}
int32_t main(){
boost;
cin>>n;
vii vec(n+1);
vec[n].fi=0;
for (int i = 0; i < n; ++i)
cin>>A[i];
cin>>k;
for (int i = 0; i < n; ++i)
{
h+=A[i]-k;
vec[i]={h,INF};
}
sort(all(vec));
for (int i = 0; i < sz(vec);)
{
int j=i,cnt=0;
if(!vec[i].fi)cnt--;
while(j<sz(vec) && vec[j].fi==vec[i].fi)
cnt++,j++;
vec[i].se=idx;
if(cnt)upd(idx,cnt);
i=j,idx++;
}
h=0;
for (int i = 0; i < n; ++i)
{
int m=(*lower_bound(all(vec),ii(h,-1))).se;
ans+=sum(m,n+1);
h+=A[i]-k;
m=(*lower_bound(all(vec),ii(h,-1))).se;
upd(m,-1);
}
cout<<ans<<"\n";
return 0;
}
//do smth instead of nothing and stay organized
//long long
//array bounds
//special cases
//binary search
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
578 ms |
23012 KB |
Output is correct |
5 |
Correct |
293 ms |
13084 KB |
Output is correct |
6 |
Correct |
464 ms |
20472 KB |
Output is correct |
7 |
Correct |
514 ms |
21368 KB |
Output is correct |
8 |
Correct |
411 ms |
18424 KB |
Output is correct |
9 |
Correct |
518 ms |
23932 KB |
Output is correct |
10 |
Correct |
451 ms |
20728 KB |
Output is correct |