Language: C++

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
int y[100005],x[100005],n,start,sum[100005],k;
struct node{
	int l,r;
	int 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("%d%d",&n,&start);
	for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
	for(int i=0;i<=n;i++){
	    sum[i]=sum[i-1]+y[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);
		cout<<s<<" "<<l<<" "<<r<<endl;
		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:59:13: error: reference to 'next' is ambiguous
   59 |             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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:74:20: error: reference to 'next' is ambiguous
   74 |                 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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:74:42: error: reference to 'next' is ambiguous
   74 |                 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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:79:30: error: reference to 'next' is ambiguous
   79 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:80:25: error: reference to 'next' is ambiguous
   80 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:80:39: error: reference to 'next' is ambiguous
   80 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:86:30: error: reference to 'next' is ambiguous
   86 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:87:25: error: reference to 'next' is ambiguous
   87 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:87:39: error: reference to 'next' is ambiguous
   87 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:88:33: error: reference to 'next' is ambiguous
   88 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc:89:35: error: reference to 'next' is ambiguous
   89 |                         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:52:5: note:                 'int next [100005]'
   52 | int next[100005],last[100005];
      |     ^~~~
a.cc: In function 'int find(int)':
a.cc:51:1: warning: control reaches end of non-void function [-Wreturn-type]
   51 | }
      | ^