#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair <int , int>
#define arr3 array <int , 3>
using namespace std;
const int INF = 1e18 + 7;
const int maxn = 1e6 + 7;
struct Fenwick_Tree
{
vector <int> tree = vector <int> (maxn + 3 , 0);
void update(int pos , int val)
{
while(pos < tree.size())
{
tree[pos] += val;
pos += (pos & (-pos));
}
}
int get_prefix(int pos)
{
int sum = 0;
while(pos > 0)
{
sum += tree[pos];
pos -= (pos & (-pos));
}
return sum;
}
} bit;
int find_pos(int val)
{
int l = 1 , r = maxn , pos;
while(l <= r)
{
int mid = (l+r) >> 1;
if(bit.get_prefix(mid) < val)
{
pos = mid;
r = mid - 1;
}
else l = mid + 1;
}
return pos;
}
int n;
pii a[maxn];
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i].fi >> a[i].se;
}
sort(a+1 , a+n+1);
for(int i = 1; i <= n; i++)
{
int h = a[i].fi , k = a[i].se;
if(bit.get_prefix(h) < bit.get_prefix(h - k + 1))
{
int id1 = find_pos(bit.get_prefix(h - k + 1) + 1);
int id2 = find_pos(bit.get_prefix(h - k + 1));
bit.update(id2 , 1);
bit.update(h+1 , -1);
bit.update(id1 , 1);
bit.update(id1 + (k - (h - id2 + 1)), - 1);
}
else
{
int id1 = find_pos(bit.get_prefix(h - k + 1) + 1);
bit.update(id1 , 1);
bit.update(id1 + k , -1);
}
}
int ans = 0ll;
for(int i = 1; i < maxn; i++)
{
int cnt = bit.get_prefix(i);
ans += cnt*(cnt - 1)/2;
}
cout << ans << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
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... |