#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5+5;
const long long MAXM = 1e5;
pair<long long,long long> arr[MAXN];
pair<long long,long long> seg[8*MAXN];
long long lazy[8*MAXN];
void push(long long curr,long long l,long long r){
if(!lazy[curr]){
return;
}
seg[curr].first+=lazy[curr];
seg[curr].second+=lazy[curr];
if(l!=r){
lazy[2*curr]+=lazy[curr];
lazy[2*curr+1]+=lazy[curr];
}
lazy[curr] = 0;
}
long long atpos(long long curr,long long l,long long r,long long pos){
if(lazy[curr]){
push(curr,l,r);
}
if(l==r){
return seg[curr].first;
}
long long mid = (l+r)/2;
push(2*curr+1,mid+1,r);
push(2*curr,l,mid);
if(pos<=mid){
return atpos(2*curr,l,mid,pos);
}else{
return atpos(2*curr+1,mid+1,r,pos);
}
}
long long firstpos(long long curr,long long l,long long r,long long val){
if(lazy[curr]){
push(curr,l,r);
}
if(l==r){
return l;
}
long long mid = (l+r)/2;
push(2*curr+1,mid+1,r);
push(2*curr,l,mid);
if(seg[2*curr].first>=val && seg[2*curr].second<=val){
return firstpos(2*curr,l,mid,val);
}else{
return firstpos(2*curr+1,mid+1,r,val);
}
}
long long lastpos(long long curr,long long l,long long r,long long val){
if(lazy[curr]){
push(curr,l,r);
}
if(l==r){
return l;
}
long long mid =(l+r)/2;
push(2*curr+1,mid+1,r);
push(2*curr,l,mid);
if(seg[2*curr+1].first>=val && seg[2*curr+1].second<=val){
return lastpos(2*curr+1,mid+1,r,val);
}else{
return lastpos(2*curr,l,mid,val);
}
}
void update(long long curr,long long l,long long r,long long tl,long long tr){
if(l>r){
return;
}
if(lazy[curr]){
push(curr,l,r);
}
if(r<tl||l>tr){
return;
}
if(l>=tl && r<=tr){
//cout<<l<<" "<<r<<" "<<tl<<" "<<tr<<endl;
seg[curr].first++;
seg[curr].second++;
//cout<<curr<<" "<<seg[curr].first<<endl;
if(l!=r){
lazy[2*curr]++;
lazy[2*curr+1]++;
}
return;
}
long long mid =(l+r)/2;
update(2*curr,l,mid,tl,tr);
update(2*curr+1,mid+1,r,tl,tr);
seg[curr].first = max(seg[2*curr].first,seg[2*curr+1].first);
seg[curr].second = min(seg[2*curr].second,seg[2*curr+1].second);
}
int main(){
long long n;
cin>>n;
for(long long i=1;i<=n;i++){
cin>>arr[i].first>>arr[i].second;
}
sort(arr+1,arr+n+1);
for(long long i=1;i<=n;i++){
long long h = arr[i].first;
long long k = arr[i].second;
long long last = h-k+1;
long long val = atpos(1,1,MAXM,last);
//cout<<atpos(1,1,MAXM,1)<<endl;
//cout<<h<<" "<<k<<" "<<last<<" "<<val<<endl;
long long l =firstpos(1,1,MAXM,val);
long long r = lastpos(1,1,MAXM,val);
//cout<<val<<" "<<l<<" "<<r<<endl;
//cout<<atpos(1,1,MAXM,1)<<" "<<firstpos(1,1,MAXM,0)<<endl;
long long remaining = k;
if(r+1<=h){
update(1,1,MAXM,r+1,h);
//cout<<h<<" "<<k<<" "<<r+1<<" "<<h<<endl;
remaining = k-(h-(r+1)+1);
}
if(remaining>0){
// cout<<h<<" "<<k<<" "<<l<<" "<<l+remaining-1<<endl;
update(1,1,MAXM,l,l+remaining-1);
}
}
long long ans = 0;
for(long long i=1;i<=1e5;i++){
long long hold =atpos(1,1,MAXM,i);
ans+=(hold)*(hold-1)/2;
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
376 KB |
Output is correct |
2 |
Correct |
16 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
376 KB |
Output is correct |
2 |
Correct |
16 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
376 KB |
Output is correct |
2 |
Correct |
16 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
476 KB |
Output is correct |
2 |
Correct |
17 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
632 KB |
Output is correct |
2 |
Correct |
23 ms |
6520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2296 KB |
Output is correct |
2 |
Correct |
84 ms |
1584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
4268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
7716 KB |
Output is correct |
2 |
Correct |
208 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
253 ms |
7928 KB |
Output is correct |
2 |
Correct |
160 ms |
7008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
300 ms |
7984 KB |
Output is correct |
2 |
Correct |
233 ms |
4952 KB |
Output is correct |