#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<int,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[1000001],fen[1000001],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;
//freopen(".in","r",stdin);
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].fi=h;
}
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,idx-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 |
5 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 |
Incorrect |
768 ms |
24792 KB |
Output isn't correct |
5 |
Incorrect |
384 ms |
12664 KB |
Output isn't correct |
6 |
Incorrect |
637 ms |
17528 KB |
Output isn't correct |
7 |
Incorrect |
669 ms |
20856 KB |
Output isn't correct |
8 |
Incorrect |
632 ms |
19064 KB |
Output isn't correct |
9 |
Incorrect |
797 ms |
18936 KB |
Output isn't correct |
10 |
Incorrect |
687 ms |
18680 KB |
Output isn't correct |