#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 1e6+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n;
ll a[SZ], p, res = 0;
pll b[SZ];
struct Fenwick
{
int nodes[SZ];
void update(int pos)
{
while(pos <= n)
{
nodes[pos]++;
pos += pos & (-pos);
}
}
int get(int pos)
{
int sum = 0;
while(pos > 0)
{
sum += nodes[pos];
pos -= pos & (-pos);
}
return sum;
}
} ft;
int main()
{
init();
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> p;
for(int i = 1; i <= n; i++)
{
a[i] -= p;
}
ll sum = 0;
for(int i = 1; i <= n; i++)
{
sum += a[i];
b[i] = {sum, i};
}
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; i++)
{
//cout << b[i].fi << " " << b[i].se << '\n';
if(b[i].fi >= 0) res++;
res += 1LL*ft.get(b[i].se);
ft.update(b[i].se);
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4572 KB |
Output is correct |
4 |
Correct |
203 ms |
36928 KB |
Output is correct |
5 |
Correct |
89 ms |
24148 KB |
Output is correct |
6 |
Correct |
149 ms |
35520 KB |
Output is correct |
7 |
Correct |
164 ms |
35708 KB |
Output is correct |
8 |
Correct |
133 ms |
32576 KB |
Output is correct |
9 |
Correct |
166 ms |
37460 KB |
Output is correct |
10 |
Correct |
149 ms |
35668 KB |
Output is correct |