This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// M
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct CHT
{
typedef pair < ll , ll > Line;
vector < pair < ll , Line > > A;
vector < vector < pair < ll , Line > > > U;
ll INF = 1e18;
inline void Add(Line X)
{
vector < pair < ll , Line > > nwu;
while (A.size() && Intersection(A.back().second, X) <= A.back().first)
nwu.push_back(A.back()), A.pop_back();
if (A.size())
A.push_back({Intersection(A.back().second, X), X});
else
A.push_back({-INF, X});
U.push_back(nwu);
}
inline ll GetMax(ll X)
{
int lb = upper_bound(A.begin(), A.end(), pair < ll , Line > {X, {INF, INF}}) - A.begin() - 1;
return (A[lb].second.first * X + A[lb].second.second);
}
inline ll Intersection(Line X, Line Y)
{
if (X.first == Y.first)
return X.second <= Y.second ? -INF : INF;
return ((X.second - Y.second) / (Y.first - X.first)) + ((X.second - Y.second) % (Y.first - X.first) > 0);
}
inline void Undo()
{
assert(A.size() && U.size());
vector < pair < ll , Line > > nwu = U.back();
U.pop_back(); A.pop_back();
while (nwu.size())
A.push_back(nwu.back()), nwu.pop_back();
}
};
const int N = 100005;
int n, H[N], K[N], C[N];
ll PS[N];
CHT Cht;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
scanf("%d%d", &H[i], &K[i]);
for (int i = 1; i <= n; i ++)
C[H[i] - K[i] + 1] ++, C[H[i] + 1] --;
for (int i = 1; i < N; i ++)
C[i] += C[i - 1];
for (int i = 1; i < N; i ++)
PS[i] = PS[i - 1] + C[i];
for (int i = N - 1; i; i --)
Cht.Add({-i, PS[i]});
for (int i = 1; i + 1 < N; i ++)
{
int le = max(C[i], 0) - 1, ri = n, md;
while (ri - le > 1)
{
md = (le + ri) >> 1;
if (Cht.GetMax(md) <= PS[i - 1] - (ll)md * (i - 1))
ri = md;
else
le = md;
}
PS[i] += ri - C[i];
C[i + 1] -= ri - C[i];
C[i] = ri;
Cht.Undo();
}
for (int i = 2; i < N; i ++)
assert(C[i - 1] >= C[i]);
ll tot = 0;
for (int i = 1; i < N; i ++)
tot += (ll)C[i] * (C[i] - 1) / 2;
printf("%lld\n", tot);
return 0;
}
Compilation message (stderr)
sails.cpp: In function 'int main()':
sails.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
48 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
sails.cpp:50:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d%d", &H[i], &K[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |