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;
using int64 = long long;
struct SegTree {
int Size;
vector<int> tree, lazy;
SegTree(int N) {
Size = 1;
while (Size < N)
Size <<= 1;
tree.resize(Size << 1);
lazy.resize(Size << 1);
}
void push(int x) {
if (lazy[x] == 0)
return;
for (int i = 0; i < 2; ++i) {
int son = x << 1 | i;
tree[son] += lazy[x];
lazy[son] += lazy[x];
}
lazy[x] = 0;
}
void update(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
++tree[x], ++lazy[x];
return;
}
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (st <= mid)
update(lSon, lx, mid, st, dr);
if (mid < dr)
update(rSon, mid + 1, rx, st, dr);
tree[x] = min(tree[lSon], tree[rSon]);
}
int get_val(int x, int lx, int rx, int poz) {
if (lx == rx)
return tree[x];
push(x);
int mid = (lx + rx) >> 1;
if (poz <= mid)
return get_val(x << 1, lx, mid, poz);
return get_val(x << 1 | 1, mid + 1, rx, poz);
}
int first_lower(int x, int lx, int rx, int val) {
if (lx == rx)
return lx;
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (tree[lSon] < val)
return first_lower(lSon, lx, mid, val);
return first_lower(rSon, mid + 1, rx, val);
}
int first_equal(int x, int lx, int rx, int val) {
if (lx == rx)
return lx;
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (tree[lSon] <= val)
return first_equal(lSon, lx, mid, val);
return first_equal(rSon, mid + 1, rx, val);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<pair<int,int>> m(n);
for (auto &p : m)
cin >> p.first >> p.second;
sort(m.begin(), m.end());
int H = m.back().first;
SegTree tree(H);
for (auto p : m) {
int h, k;
tie(h, k) = p;
int val = tree.get_val(1, 1, H, h - k + 1);
if (tree.tree[1] < val) {
int nxt = tree.first_lower(1, 1, H, val);
if (nxt <= h) {
tree.update(1, 1, H, nxt, h);
k -= h - nxt + 1;
}
}
int poz = tree.first_equal(1, 1, H, val);
tree.update(1, 1, H, poz, poz + k - 1);
}
int64 ans = 0;
for (int h = 1; h <= H; ++h) {
int64 sails = tree.get_val(1, 1, H, h);
ans += (sails * (sails - 1)) >> 1;
}
cout << ans << '\n';
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... |