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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 5;
struct stree {
#define lc v*2
#define rc v*2+1
vector <pair <ll, ll>> tree;
stree (int sz) {
tree = vector <pair <ll, ll>> (sz*4, {1e9, 1e9});
}
void pushup(int v) {
tree[v].first = tree[lc].first;
if (tree[lc].first == 1e9)
tree[v].first = tree[rc].first;
tree[v].second = tree[rc].second;
if (tree[rc].second == 1e9)
tree[v].second = tree[lc].second;
}
void update(int v, int tl, int tr, int p, int val) {
if (tl > p || tr < p)
return;
if (tl == tr) {
tree[v].first += val;
tree[v].second += val;
return;
}
int tm = (tl + tr) / 2;
update(lc, tl, tm, p, val);
update(rc, tm+1, tr, p, val);
pushup(v);
}
int query(int v, int tl, int tr, int p) {
if (tl > p || tr < p)
return 0;
if (tl == tr)
return tree[v].first;
int tm = (tl + tr) / 2;
int a = query(lc, tl, tm, p);
if (a == 0)
return query(rc, tm+1, tr, p);
return 0;
}
// find index of leftmost value in the range l->r that is non zero
int queryl(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 1e9;
if (tree[v].first == 1e9)
return 1e9;
if (tl == tr)
return tl;
int tm = (tl + tr) / 2;
int a = queryl(lc, tl, tm, l, r);
if (a == 1e9)
return queryl(rc, tm+1, tr, l, r);
return a;
}
// find index of rightmost value in the range l->r that is non zero
int queryr(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 1e9;
if (tree[v].second == 1e9)
return 1e9;
if (tl == tr)
return tl;
int tm = (tl + tr) / 2;
int a = queryr(rc, tm+1, tr, l, r);
if (a == 1e9)
return queryr(lc, tl, tm, l, r);
return a;
}
};
struct node {
ll val = 0, pd = 0;
bool up = false;
};
struct stree2 {
#define lc v*2
#define rc v*2+1
vector <node> tree;
stree2 (int sz) {
tree = vector <node> (sz*4);
}
void pushup(int v) {
tree[v].val = tree[lc].val + tree[rc].val;
}
void pushdown(int v, int tl, int tm, int tr) {
if (!tree[v].up)
return;
tree[v].up = false;
tree[lc].up = tree[rc].up = true;
tree[lc].pd += tree[v].pd;
tree[rc].pd += tree[v].pd;
tree[lc].val += (tm - tl + 1) * tree[v].pd;
tree[rc].val += (tr - tm) * tree[v].pd;
tree[v].pd = 0;
}
void update(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l || l > r)
return;
if (tl >= l && tr <= r) {
tree[v].pd++;
tree[v].up = true;
tree[v].val += (tr - tl + 1);
return;
}
int tm = (tl + tr) / 2;
pushdown(v, tl, tm, tr);
update(lc, tl, tm, l, r);
update(rc, tm+1, tr, l, r);
pushup(v);
}
int query(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return 0;
if (tl >= l && tr <= r)
return tree[v].val;
int tm = (tl + tr) / 2;
pushdown(v, tl, tm, tr);
int a = query(lc, tl, tm,l, r);
int b = query(rc, tm+1, tr,l, r);
return a + b;
}
};
int main() {
int n;
cin >> n;
vector <pair <int, int>> updates(n);
for (int i = 0; i < n; i++)
cin >> updates[i].first >> updates[i].second;
sort(updates.begin(), updates.end());
stree tree(maxn + 5);
stree2 tree2(maxn + 5);
int r = 0;
ll ans = 0;
for (int i = 0; i < n; i++) {
r = max(r, updates[i].first + 1);
int k = updates[i].second;
ll res = tree2.query(1, 0, maxn + 4, r - k-1, r-2);
ans += res;
int lb = tree.queryr(1, 0, maxn + 4, 0, r - k - 1);
int rb = r-2;
int lx = tree.queryl(1, 0, maxn + 4, r - k-1, rb);
int rx = lb + (k - (rb - lx + 1)) - 1;
if (lx == 1e9)
rx = lb + k - 1;
tree.update(1, 0, maxn + 4, lb, 1);
tree.update(1, 0, maxn + 4, rx+1, -1);
if (lx > 0) {
tree.update(1, 0, maxn + 4, lx, 1);
tree.update(1, 0, maxn + 4, rb+1, -1);
}
tree2.update(1, 0, maxn + 4, lb, rx);
if (lx > 0)
tree2.update(1, 0, maxn + 4, lx, rb);
}
cout << ans << '\n';
}
# | 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... |