/*#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define ordered_set tree<int, null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>
#define eps 1e-9
#define MOD1 998244353
#define MOD2 1000000007
#define INV_10 299473306
#define INF 1000000001
#define PI 3.14159265358979323846
using namespace std;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin >> n;
int a[n];
//cout << n << '\n';
for(int i = 0; i < n; i++)
cin >> a[i];
int p;
vector<pair<long long, int> >v;
cin >> p;
long long sum=0, cu=0, ans=0;
for(int i = n-1; i >= 0; i--)
{
sum+=a[i];
v.push_back(make_pair(sum-cu, i));
//cout << here[i] << ' ';
cu+=p;
}
//cout << '\n';
sort(v.begin(), v.end());
int m[n];
for(int i = 0; i < n; i++)
{
m[v[i].second]=i;
}
int pp=1;
while(pp <= n)
pp*=2;
cu=0;
int tree[2*pp+1];
for(int i = 0; i <2*pp+1; i++)
tree[i]=0;
for(int i = pp; i <= pp+n-1; i++)
tree[i]=1;
for(int i = pp-1; i >= 1; i--)
tree[i]=tree[2*i]+tree[2*i+1];
sum=0;
for(int i = n-1; i >= 0; i--)
{
vector<pair<long long, int> >::iterator it=lower_bound(v.begin(), v.end(), make_pair(p+sum-cu, -1));
if(it!=v.end())
{
int x=(it-v.begin())+pp, y=n-1+pp;
while(x <= y)
{
if(x%2==1)
{
ans+=tree[x];
x++;
}
if(y%2==0)
{
ans+=tree[y];
y--;
}
x/=2;
y/=2;
}
}
//cout << s.order_of_key(*s.lower_bound(make_pair(p+pr-cu, -1))) << '\n';
sum+=a[i];
int x=m[i]+pp;
while(x >= 1)
{
tree[x]--;
x/=2;
}
cu+=p;
}
cout << ans << '\n';
//printf("%lld", ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
4 |
Correct |
561 ms |
34752 KB |
Output is correct |
5 |
Correct |
280 ms |
21848 KB |
Output is correct |
6 |
Correct |
473 ms |
29044 KB |
Output is correct |
7 |
Correct |
455 ms |
30144 KB |
Output is correct |
8 |
Correct |
408 ms |
27204 KB |
Output is correct |
9 |
Correct |
534 ms |
41948 KB |
Output is correct |
10 |
Correct |
445 ms |
29380 KB |
Output is correct |