#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 1e5+20;
const int MAXA = 1e6;
const int INF = 1e18+10;
const int MOD = 1e9+7;
const int LOG = 32;
const ld EPS = 1e-12;
int n, ans;
struct seg {
int st[4*MAXN], laz[4*MAXN];
void bd(int id, int l,int r){
st[id] = laz[id] = 0;
if(l==r){ return; }
bd(lf,l,md); bd(rg,md+1,r);
}
void bnc(int id,int l,int r){
if(laz[id] == 0) return;
st[lf] += (md-l+1)*laz[id]; laz[lf]+=laz[id];
st[rg] += (r-md)*laz[id]; laz[rg]+=laz[id];
laz[id] = 0;
}
int que(int id, int l,int r,int x,int y){
if(x<=l && r<=y) return st[id];
if(r<x || y<l) return 0;
bnc(id,l,r);
return que(lf,l,md,x,y) + que(rg,md+1,r,x,y);
}
int upd(int id,int l,int r,int x,int y,int p){
if(x<=l && r<=y){
laz[id] += p;
return st[id] += (r-l+1) * p;
}
if(r<x || y<l) return st[id];
bnc(id,l,r);
return st[id] = upd(lf,l,md,x,y,p)+upd(rg,md+1,r,x,y,p);
}
} A;
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
vector<pii> vec;
for(int i=1; i<=n; i++){
int x, y; cin>>x>>y; vec.pb({x,y});
}
sort(vec.begin(), vec.end());
A.bd(1,1,MAXN-10);
;
int las = MAXN-9, p = 0;
for(auto [x,k] : vec){
// cout << las << "las";
if(x!=p){
las = las-(x-p); p = x;
}
// cout << las << "las2 ";
// upd k awal
int val = A.que(1,1,MAXN-10,las+k-1,las+k-1);
// cout << val << "val\n";
int l=las,r=MAXN-10,mid=0; int le = 0;
while(l<=r){
mid = md;
if(A.que(1,1,MAXN-10,mid,mid) < val) l = mid+1;
else r = mid-1, le = mid;
}
l=las,r=MAXN-10,mid=0; int ri = 0;
while(l<=r){
mid = md;
if(A.que(1,1,MAXN-10,mid,mid) > val) r = mid-1;
else l = mid+1, ri = mid;
}
int sis = k - (le-las);
ans += A.que(1,1,MAXN-10,las,le-1);
ans += A.que(1,1,MAXN-10,ri-sis+1,ri);
A.upd(1,1,MAXN-10,las,le-1,1);
A.upd(1,1,MAXN-10,ri-sis+1,ri,1);
// cout << ans << "ans\n";
}
cout << ans << '\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... |