不是很难,就是写着有点费劲。
#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef pair<int,int>P;
const int inf = 0x7f7f7f7f;
const int N = 1e5+10;
const ll mod = 2012;
const double PI = 3.14;
int read(){
char ch=getchar();int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int random(int n){return(ll)rand()*rand()%n;}
ll x[N],h[N];
ll tree1[N<<5],tree2[N<<5];
ll lazy1[N<<5],lazy2[N<<5];
ll x1,x2;
void pushup(int rt){
if(tree1[2*rt] > tree1[2*rt+1]){
tree1[rt] = tree1[2*rt];
lazy1[rt] = lazy1[2*rt];
}
if(tree1[2*rt] < tree1[2*rt+1]){
tree1[rt] = tree1[2*rt+1];
lazy1[rt] = lazy1[2*rt+1];
}
if(tree1[2*rt] == tree1[2*rt+1]){
tree1[rt] = tree1[2*rt];
lazy1[rt] = lazy1[2*rt+1];
}
if(tree2[2*rt] > tree2[2*rt+1]){
tree2[rt] = tree2[2*rt];
lazy2[rt] = lazy2[2*rt];
}
if(tree2[2*rt] < tree2[2*rt+1]){
tree2[rt] = tree2[2*rt+1];
lazy2[rt] = lazy2[2*rt+1];
}
if(tree2[2*rt] == tree2[2*rt+1]){
tree2[rt] = tree2[2*rt];
lazy2[rt] = lazy2[2*rt];
}
}
void build(int rt,int left,int right){
if(left == right){
tree1[rt] = h[left]+x[left];
lazy1[rt] = x[left];
tree2[rt] = h[left]-x[left];
lazy2[rt] = x[left];
return;
}
int mid = (left+right)/2;
build(2*rt,left,mid);
build(2*rt+1,mid+1,right);
pushup(rt);
}
void update(int rt,int left,int right,int k){
if(left == right){
tree1[rt] = tree2[rt] = -1e18;
return;
}
int mid = (left+right)/2;
if(k <= mid) update(2*rt,left,mid,k);
else update(2*rt+1,mid+1,right,k);
pushup(rt);
}
ll querytree1(int rt,int left,int right,int l,int r){
if(left >= l && right <= r){
return tree1[rt];
}
ll cnt = -1e18;
int mid = (left+right)/2;
if(l <= mid) cnt = max(cnt,querytree1(2*rt,left,mid,l,r));
if(r > mid) cnt = max(cnt,querytree1(2*rt+1,mid+1,right,l,r));
return cnt;
}
ll querytree2(int rt,int left,int right,int l,int r){
if(left >= l && right <= r){
return tree2[rt];
}
ll cnt = -1e18;
int mid = (left+right)/2;
if(l <= mid) cnt = max(cnt,querytree2(2*rt,left,mid,l,r));
if(r > mid) cnt = max(cnt,querytree2(2*rt+1,mid+1,right,l,r));
return cnt;
}
void querylazy1(int rt,int left,int right,int l,int r,ll t){
if(left >= l && right <= r){
if(tree1[rt] == t){
x1 = max(x1,lazy1[rt]);
}
return;
}
int mid = (left+right)/2;
if(l <= mid) querylazy1(2*rt,left,mid,l,r,t);
if(r > mid) querylazy1(2*rt+1,mid+1,right,l,r,t);
}
void querylazy2(int rt,int left,int right,int l,int r,ll t){
if(left >= l && right <= r){
if(tree2[rt] == t){
x2 = min(x2,lazy2[rt]);
}
return;
}
int mid = (left+right)/2;
if(l <= mid) querylazy2(2*rt,left,mid,l,r,t);
if(r > mid) querylazy2(2*rt+1,mid+1,right,l,r,t);
}
int main(){
/*
/and((unsigned)time(0));
//freopen("out.txt","w",stdout);
//freopen("in.txt","r",stdin);\
*/
int n = read(),root = read();
for(int i = 1;i <= n;i++){
x[i] = read();h[i] = read();
}
build(1,1,n);
ll ans = 0;
for(int i = 1;i < n;i++){
ll ma1 = -1e18,ma2 = -1e18;
x1 = -1e18;x2 = 1e18;
if(root-1 >= 1){
ma1 = querytree1(1,1,n,1,root-1);
querylazy1(1,1,n,1,root-1,ma1);
ma1 -= x[root];
}
if(root+1 <= n){
ma2 = querytree2(1,1,n,root+1,n);
querylazy2(1,1,n,root+1,n,ma2);
ma2 += x[root];
}
update(1,1,n,root);
if(ma1 > ma2){
ans += abs(x[root]-x1);
root = lower_bound(x+1,x+1+n,x1)-x;
// cout<<root<<endl;
}
if(ma1 < ma2){
ans += abs(x[root]-x2);
root = lower_bound(x+1,x+1+n,x2)-x;
}
if(ma1 == ma2){
if(abs(x[root]-x1) <= abs(x[root]-x2)){
ans += abs(x[root]-x1);
root = lower_bound(x+1,x+1+n,x1)-x;
}else {
ans += abs(x[root]-x2);
root = lower_bound(x+1,x+1+n,x2)-x;
}
}
// cout<<root<<endl;
}
cout<<ans<<endl;
return 0;
}