#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];
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;
}
for ( int i = 1; i < N; i++ ) cnt[i] += cnt[i - 1];
priority_queue <int> q;
for ( int i = 1; i < N; i++ ){
while ( size(q) && -q.top() + 2 < cnt[i] ){
int u = -q.top(); q.pop();
cnt[i] -= 1, u += 1;
q.push(-u);
}
q.push(-cnt[i]);
}
int ans = 0;
while ( size(q) ){
int x = -q.top(); q.pop();
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... |