Language: C++

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
long long y[100005],x[100005],n,start,k;
struct node{
	int l,r;
	long long sum;
}a[500005];
void buildtree(){
	k=1;
	while(k<n)k*=2;
	for(int i=k*2-1;i>=k;i--){
		a[i].l=a[i].r=i-k+1;
		a[i].sum=y[i-k+1];
	}
	for(int i=k-1;i>=1;i--){
	   a[i].l=a[i*2].l;a[i].r=a[i*2+1].r;
	   a[i].sum=a[i*2].sum+a[i*2+1].sum;
	}
}
void update(int s,int t){
	while(s){
		a[s].sum-=t;
		s/=2;
	}
}
int ask(int i,int l,int r){
	if(l<=a[i].l&&r>=a[i].r)return a[i].sum;
	int mid=(a[i].l+a[i].r)/2;
	if(mid<l)return ask(i*2+1,l,r);
	else if(mid>=r)return ask(i*2,l,r);
	else {
		return ask(i*2,l,mid)+ask(i*2+1,mid+1,r);
	}
}
int find(int place){
	int l=1,r=n,mid;
	while(l<r){
		mid=(l+r)/2;
		if(x[mid]==place)return mid;
		else if(place<x[mid]){
			l=mid+1;
		}
		else r=mid-1;
	}
}
int next[100005],last[100005];
int main(){
	long long ans=0;
	scanf("%lld%lld",&n,&start);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&x[i],&y[i]);
	for(int i=0;i<=n;i++){ 
	    next[i]=i+1;last[i]=i-1;
	}
	buildtree();
	int s=1;x[n+1]=1111111111;
	for(int i=1;i<=n+1;i++){
		if(start<x[i]){
			s=i-1;break;
		}
	}
	if(s==0){
		ans=x[1]-start;s++;
	}
	for(int i=1;i<=n;i++){
		int l=0,r=0;
		if(last[s]>0)l=ask(1,1,last[s]);
		if(next[s]<(n+1))r=ask(1,next[s],n);
		if(l==0&&r==0)break;
		if(i==1)l=ask(1,1,s);
	    if(l>=r){
			last[next[s]]=last[s];
			next[last[s]]=next[s];
			ans+=(x[s]-x[last[s]]);
			if(i!=1)s=last[s];
			update(k+s-1,y[s]);
		}
		else {
			last[next[s]]=last[s];
			next[last[s]]=next[s];
			ans+=(x[next[s]]-x[s]);
			if(i!=1)s=next[s];
			update(k+s-1,y[s]);
		}
	}
	printf("%lld",ans);
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:55:13: error: reference to 'next' is ambiguous
   55 |             next[i]=i+1;last[i]=i-1;
      |             ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:70:20: error: reference to 'next' is ambiguous
   70 |                 if(next[s]<(n+1))r=ask(1,next[s],n);
      |                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:70:42: error: reference to 'next' is ambiguous
   70 |                 if(next[s]<(n+1))r=ask(1,next[s],n);
      |                                          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:74:30: error: reference to 'next' is ambiguous
   74 |                         last[next[s]]=last[s];
      |                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:75:25: error: reference to 'next' is ambiguous
   75 |                         next[last[s]]=next[s];
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:75:39: error: reference to 'next' is ambiguous
   75 |                         next[last[s]]=next[s];
      |                                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:81:30: error: reference to 'next' is ambiguous
   81 |                         last[next[s]]=last[s];
      |                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:82:25: error: reference to 'next' is ambiguous
   82 |                         next[last[s]]=next[s];
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:82:39: error: reference to 'next' is ambiguous
   82 |                         next[last[s]]=next[s];
      |                                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:83:33: error: reference to 'next' is ambiguous
   83 |                         ans+=(x[next[s]]-x[s]);
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc:84:35: error: reference to 'next' is ambiguous
   84 |                         if(i!=1)s=next[s];
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:49:5: note:                 'int next [100005]'
   49 | int next[100005],last[100005];
      |     ^~~~
a.cc: In function 'int find(int)':
a.cc:48:1: warning: control reaches end of non-void function [-Wreturn-type]
   48 | }
      | ^