#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,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 |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Incorrect |
763 ms |
15484 KB |
Output isn't correct |
5 |
Incorrect |
361 ms |
8952 KB |
Output isn't correct |
6 |
Incorrect |
693 ms |
13780 KB |
Output isn't correct |
7 |
Incorrect |
735 ms |
14328 KB |
Output isn't correct |
8 |
Incorrect |
595 ms |
12412 KB |
Output isn't correct |
9 |
Incorrect |
894 ms |
15992 KB |
Output isn't correct |
10 |
Incorrect |
662 ms |
13948 KB |
Output isn't correct |