#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e5 + 4;
int n;
pair<int,int> a[maxN];
ll bit[maxN];
void update(int x, int v)
{
for (; x <= n; x += x & -x) bit[x] += v;
}
ll get(int x)
{
ll res = 0;
for (; x >= 1; x -= x & -x) res += bit[x];
return res;
}
int fi(int r, int x)
{
int l = 1, res = -1;
while(l <= r)
{
int m = (l + r) / 2;
if (get(m) < x)
{
res = m;
r = m - 1;
}
else l = m + 1;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen(".INP","r",stdin);
// freopen(".OUT","w",stdout);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
{
int h = a[i].first, k = a[i].second;
int last = h - k + 1;
int val = get(last);
int p1 = fi(h, val);
int p2 = fi(h, val + 1);
if (p1 != -1)
{
update(p1, 1);
update(h + 1, -1);
k = k - (h - p1 + 1);
}
update(p2, 1);
update(p2 + k, -1);
}
ll ans = 0;
for (int i = 1; i <= 1e5; ++i)
{
ll x = get(i);
ans += x * (x - 1) / 2;
}
cout << ans;
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... |