#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod1 = (ll)1e9 + 7;
const ll mod2 = (ll)1e9 + 9;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
void solve() {
ll n;
cin >> n;
vector<ll> arr(n);
cin >> arr;
ll P;
cin >> P;
vector<ll> s(n + 1), f(n + 1);
vector<pair<ll, ll>> to_s;
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + arr[i - 1];
to_s.pb({s[i] - i * P, i});
}
to_s.pb({0, 0});
sort(all(to_s));
for (int i = 0; i <= n; i++) {
f[to_s[i].second] = i;
}
vector<ll> t(n + 1);
function<void(ll)> upd = [&](ll i) {
while (i <= n) {
t[i]++;
i = (i | (i + 1));
}
};
function<ll(ll)> sum = [&](ll i) {
ll res = 0;
while (i >= 0) {
res += t[i];
i = (i & (i + 1)) - 1;
}
return res;
};
ll answer = 0;
for (int i = 0; i <= n; i++) {
answer += sum(f[i]);
upd(f[i]);
}
cout << answer;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
300 ms |
55168 KB |
Output is correct |
5 |
Correct |
160 ms |
35044 KB |
Output is correct |
6 |
Correct |
249 ms |
48796 KB |
Output is correct |
7 |
Correct |
297 ms |
50872 KB |
Output is correct |
8 |
Correct |
235 ms |
44340 KB |
Output is correct |
9 |
Correct |
295 ms |
57220 KB |
Output is correct |
10 |
Correct |
257 ms |
49672 KB |
Output is correct |