#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define ld long double
#define pl pair<ll, ll>
#define vi vector<long long>
#define vii vector<vi>
#define vc vector<char>
#define vcc vector<vc>
#define vp vector<pl>
#define mi map<ll,ll>
#define mc map<char,int>
#define sortx(X) sort(X.begin(),X.end());
#define all(X) X.begin(),X.end()
#define allr(X) X.rbegin(),X.rend()
#define ln '\n'
#define YES {cout << "YES\n"; return;}
#define NO {cout << "NO\n"; return;}
#define MUN {cout << "-1\n"; return;}
using namespace std;
const int MODE = 1e9+7;
typedef long long item;
class SegmentTree
{
public:
void set(int l, int r, int value) {
set(0, 0, size - 1, l, r, value);
}
item getrange(int l, int r) {
return (getrange(0, 0, size - 1, l, r));
}
item get(ll k) {
return (get(0, 0, size - 1, k));
}
void build(int n) {
size = 1;
while (size < n)
size *= 2;
tree.assign(size * 2, item());
lazy.assign(size * 2, 0);
}
private:
int size;
vector<item> tree;
vector<long long> lazy;
item merge(item &a, item &b) {
item res = min(a, b);
return (res);
}
void checkLazy(int m, int lx, int rx) {
if (!lazy[m]) return;
tree[m] += lazy[m];
if (lx != rx) {
lazy[2 * m + 1] += lazy[m];
lazy[2 * m + 2] += lazy[m];
}
lazy[m] = 0;
}
void set(int m, int lx, int rx, int l, int r, int val) {
checkLazy(m, lx, rx);
if (rx < l || r < lx) return;
if (l <= lx && rx <= r)
{
lazy[m] = val;
checkLazy(m, lx, rx);
return;
}
int mid = (lx + rx) / 2;
item s1, s2;
set(m * 2 + 1, lx, mid, l, r, val);
set(m * 2 + 2, mid + 1, rx, l, r, val);
s1 = tree[m * 2 + 1], s2 = tree[m * 2 + 2];
tree[m] = merge(s1, s2);
}
item getrange(int m, int lx, int rx, int l, int r) {
checkLazy(m, lx, rx);
if (rx < l || r < lx) return INT32_MAX;
if (l <= lx && rx <= r) return (tree[m]);
int mid = (lx + rx) / 2;
item s1, s2;
s1 = getrange(m * 2 + 1, lx, mid, l, r);
s2 = getrange(m * 2 + 2, mid + 1, rx, l, r);
return merge(s1, s2);
}
item get(int m, int lx, int rx, int k) {
checkLazy(m, lx, rx);
if (tree[m] > k) return -1;
if (lx == rx) return lx;
int mid = (lx + rx) / 2;
item s1, s2;
s1 = get(m * 2 + 1, lx, mid, k);
if (s1 != -1) return s1;
return get(m * 2 + 2, mid + 1, rx, k);
}
};
void solve(int tc) {
ll n;
cin >> n;
vp X(n);
for (int i = 0; i < n; i++)
{
cin >> X[i].first >> X[i].second;
}
sortx(X);
ll at = 1;
SegmentTree sg;
sg.build(2e5);
for (auto [a, b] : X) {
a--;
ll v = sg.getrange(a-b+1, a-b+1);
ll p = sg.get(v);
ll p1 = sg.get(v-1);
if (p1 <= a && p1 != -1) {
sg.set(p1, a, 1);
b-=a-p1+1;
}
sg.set(p, p+b-1, 1);
}
ll sol = 0;
for (int i = 0; i < 2e5; i++)
{
ll re = sg.getrange(i, i)-1;
sol += re * (re + 1) / 2;
}
cout << sol << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int size = 1;
// freopen("slingshot.in", "r", stdin);
// freopen("slingshot.out", "w", stdout);
// cin >> size;
for (int i = 1; i <= size; i++) solve(i);
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... |