#include<bits/stdc++.h>
using namespace std;
struct dataa{
long long lazy = 0,sum = 0;
void add(long long left,long long right,long long val){
lazy+=val;
sum+=val * (right - left + 1);
//sum += val *(right - left + 1);
}
};
struct Segment_Tree{
vector<dataa>tree;
void init(long long n){
tree.resize(2*n - 1);
}
dataa combine(dataa left,dataa right){
dataa res;
res.sum = left.sum + right.sum;
return res;
}
void push(long long node,long long left,long long right){
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (tree[node].lazy!=0){
tree[node + 1].add(left,mid,tree[node].lazy);
tree[z].add(mid + 1,right,tree[node].lazy);
tree[node].lazy = 0;
}
}
long long query(long long node,long long left,long long right,long long qleft,long long qright){
if (qright<left|| qleft > right)return 0;
if (qleft<=left && qright >=right){
return tree[node].sum;
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
return query(node + 1,left,mid,qleft,qright) + query(z,mid + 1,right,qleft,qright);
}
void update(long long node,long long left,long long right,long long uleft,long long uright,long long v){
if (left > uright || right < uleft) return;
if (uleft <= left && right <=uright){
tree[node].add(left,right,v);
return;
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (uleft<=mid){
update(node + 1,left,mid,uleft,uright,v);
}
if (uright > mid){
update(z,mid + 1,right,uleft,uright,v);
}
tree[node] = combine(tree[node + 1],tree[z]);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<pair<long long,long long>>arr(n);
vector<long long>v(100001,0);
Segment_Tree st;
st.init(100001);
int mxn = 100000;
for (int i = 0;i<n;++i){
cin>>arr[i].first>>arr[i].second;
}
sort(arr.begin(),arr.end(),[&](auto x,auto y){
return x.first < y.first;
});
long long ans = 0;
for (int i = 0;i<n;++i){
ans+=st.query(0,0,mxn,arr[i].first - arr[i].second,arr[i].first - 1);
int left = 0,right = arr[i].second - 1;
int p = -1;
int temp = st.query(0,0,mxn,arr[i].first - arr[i].second,arr[i].first - arr[i].second);
while(left<=right){
int mid = (left + right)>>1;
if (temp <= st.query(0,0,mxn,arr[i].first - arr[i].second + mid,arr[i].first - arr[i].second + mid)){
left = mid + 1;
p = mid;
}
else right = mid - 1;
}
int p2 = -1;
left = 0,right = arr[i].first - arr[i].second - 1;
while(left<=right){
int mid = (left + right)>>1;
if (temp >= st.query(0,0,mxn,arr[i].first - arr[i].second - 1 - mid,arr[i].first - arr[i].second - 1 - mid)){
p2 = mid;
left = mid + 1;
}
else right = mid - 1;
}
if (p2 == -1){
st.update(0,0,mxn,arr[i].first - arr[i].second,arr[i].first - 1,1);
continue;
}
int l = arr[i].first - arr[i].second - 1 - p2;
int r = arr[i].first - arr[i].second + 1 + p;
st.update(0,0,mxn,l,l + p,1);
st.update(0,0,mxn,r,arr[i].first - 1,1);
}
cout<<ans<<'\n';
return 0;
}
//2 2 2 1 1
//0 0 1 1 1
//3 2 2 1 1
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4160 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
10 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4436 KB |
Output is correct |
2 |
Correct |
122 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
494 ms |
6272 KB |
Output is correct |
2 |
Correct |
405 ms |
6316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
445 ms |
6676 KB |
Output is correct |
2 |
Correct |
208 ms |
6352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
593 ms |
6952 KB |
Output is correct |
2 |
Correct |
354 ms |
6852 KB |
Output is correct |