This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
namespace
{
template <typename T>
struct Fenwick
{
Fenwick(int _n) : n(_n), tree(n + 1, 0) {}
void add(int p, T x)
{
for (++p; p <= n; p += p & -p)
tree[p] += x;
}
T sum(int p)
{
T ret = 0;
for (; p; p -= p & -p)
ret += tree[p];
return ret;
}
private:
int n;
vector<T> tree;
};
} // namespace
void solve()
{
const int X = 100'000;
int n;
cin >> n;
assert(LONG_MAX > INT_MAX);
vector<long> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
Fenwick<long> fenw(X);
for (int i = 0; i < n; i++)
{
fenw.add(a[i] - b[i], 1);
fenw.add(a[i], -1);
}
vector<long> v(X);
for (int x = 0; x < X; x++)
v[x] = fenw.sum(x + 1);
stack<pair<long, int>> st;
for (int i = 0; i < n; i++)
{
long cur = v[i];
int cnt = 1;
while (!st.empty() && st.top().first <= v[i])
{
auto [x, y] = st.top();
st.pop();
cur += x * y;
cnt += y;
}
long q = cur / cnt, d = cur % cnt;
if (d > 0)
st.emplace(q + 1, d);
st.emplace(q, cnt - d);
}
long res = 0;
while (!st.empty())
{
auto [x, y] = st.top();
st.pop();
res += y * x * (x - 1) / 2;
}
cout << res << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |