#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define size(x) (int)(x).size()
#define int long long
typedef pair <int,int> pii;
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int N = 1e5 + 5;
int cnt[N + 5], tot[N + 5];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
for ( int i = 0; i < n; i++ ){
int h, k; cin >> h >> k;
cnt[h - k + 1] += 1;
cnt[h + 1] -= 1;
tot[1] += 1, tot[h + 1] -= 1;
}
for ( int i = 1; i < N; i++ ){
cnt[i] += cnt[i - 1];
tot[i] += tot[i - 1];
}
priority_queue <ar<int,2>> q;
int ans = 0;
for ( int i = 1; i < N; i++ ){
while ( size(q) && -q.top()[0] + 2 < cnt[i] ){
auto [u, f] = q.top(); q.pop();
u *= -1;
cnt[i] -= 1, u += 1;
if ( f > 1 ){
q.push({-u, f - 1});
} else ans += u * (u - 1) / 2;
}
if ( tot[i] > cnt[i] ){
q.push({-cnt[i], tot[i] - cnt[i]});
} else ans += cnt[i] * (cnt[i] - 1) / 2;
}
while ( size(q) ){
auto [x, f] = q.top(); q.pop();
x *= -1;
ans += x * (x - 1) / 2;
}
cout << ans;
cout << '\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... |