#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
class Lazy_Segment_Tree{
private:
int n;
vi date,lazy;
void Set_Lazy(int k,int x){
date[k]+=x;
lazy[k]+=x;
}
void Push(int k){
Set_Lazy(k*2+1,lazy[k]);
Set_Lazy(k*2+2,lazy[k]);
lazy[k]=0;
}
void Fix(int k){
date[k]=min(date[k*2+1],date[k*2+2]);
}
void Add_func(int a,int b,int k,int l,int r){
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
Set_Lazy(k,1);
return;
}
Push(k);
int m=(l+r)/2;
Add_func(a,b,k*2+1,l,m);
Add_func(a,b,k*2+2,m,r);
Fix(k);
}
public:
Lazy_Segment_Tree(int n_){
n=1;
while(n<n_) n*=2;
date=lazy=vi(2*n-1);
}
void Add(int a,int b){
if(a==b) return;
Add_func(a,b,0,0,n);
}
int Query(int x){
int k=0;
while(k<n-1){
Push(k);
// if(x==-1) cout<<k<endl;
if(date[k*2+1]>x) k=2*k+2;
else k=2*k+1;
}
return k-n+1;
}
int Open(int k){
k+=n-1;
int res=date[k];
while(k>0){
k=(k-1)/2;
res+=lazy[k];
}
return res;
}
ll Res(){
for(int i=0;i<n-1;i++) Push(i);
ll t=0;
for(int i=0;i<n;i++){
ll x=date[i+n-1];
t+=x*(x-1)/2;
}
return t;
}
};
const int M=1e5;
int n;
vp a;
int main(){
Lazy_Segment_Tree st(M);
cin>>n;
a=vp(n);
for(int i=0;i<n;i++) cin>>a[i].first>>a[i].second;
sort(a.begin(),a.end());
for(auto p:a){
int h=p.first,x=p.second;
int t=st.Open(h-x),l=st.Query(t),r=min(h,st.Query(t-1));
st.Add(l,x-h+r+l);
st.Add(r,h);
}
cout<<st.Res()<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2432 KB |
Output is correct |
2 |
Correct |
3 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2432 KB |
Output is correct |
2 |
Correct |
3 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2560 KB |
Output is correct |
2 |
Correct |
3 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
2560 KB |
Output is correct |
2 |
Correct |
69 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
3372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
3832 KB |
Output is correct |
2 |
Correct |
210 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
3852 KB |
Output is correct |
2 |
Correct |
148 ms |
3960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
3960 KB |
Output is correct |
2 |
Correct |
204 ms |
3912 KB |
Output is correct |