本文为线段树做法
(听说可以tarjan缩点+拓扑?
感觉差不多。。而且这样看起来方便很多
找到左端点的过程可以看作
点 -> 区间内lowerbound最小的点 -> lowerbound -> 区间内lowerbound最小的点 -> lowerbound -> ......
所以直接维护每个点lowerbound,线段树维护下就好啦
右端点同理
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 5e5 + 5;
const long long P = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
long long pos[N], rad[N];
int L[N], R[N];
long long ans;
struct Seg{
int lm[N << 2], rm[N << 2];
void update(int rt){
lm[rt] = min(lm[rt << 1], lm[rt << 1 | 1]);
rm[rt] = max(rm[rt << 1], rm[rt << 1 | 1]);
}
void build(int l, int r, int rt){
if(l == r){
lm[rt] = L[l], rm[rt] = R[l];
return ;
}
int mid = l + ((r - l) >> 1);
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
update(rt);
}
void qry(int l, int r, int x, int y, int& nx, int& ny, int rt){
if(l == x && r == y){
nx = min(lm[rt], nx); ny = max(rm[rt], ny); return ;
}
int mid = l + ((r - l) >> 1);
if(x <= mid) qry(l, mid, x, min(y, mid), nx, ny, rt << 1);
if(y > mid) qry(mid + 1, r, max(x, mid + 1), y, nx, ny, rt << 1 | 1);
}
}seg;
inline int l_lim(int x){
return lower_bound(pos + 1, pos + x, pos[x] - rad[x]) - pos;
}
inline int r_lim(int x){
return upper_bound(pos + x + 1, pos + n + 1, pos[x] + rad[x]) - pos - 1;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%lld%lld", &pos[i], &rad[i]);
}
for(int i = 1; i <= n; ++i){
L[i] = l_lim(i);
R[i] = r_lim(i);
}
seg.build(1, n, 1);
ans = 0;
int x, y, nx, ny;
for(int i = 1; i <= n; ++i){
x = y = nx = ny = i;
do{
x = nx, y = ny;
nx = inf, ny = -inf; seg.qry(1, n, x, y, nx, ny, 1);
}while((nx ^ x) | (ny ^ y));
ans = (ans + 1ll * i * (y - x + 1) % P) % P;
//这里原来忘乘1ll了爆了long long
}
printf("%lld
", ans);
return 0;
}
/*
单纯维护两边并更新不可行 可能会有折向引爆
*/