Language: C++

#include<bits/stdc++.h>
using namespace std;

using ll=long long ;
int main(){
    ll x;
    ll k;
    cin>>x>>k;
    vector<ll> r(k+3);
    r[0]=0;
    r[k+1]=1e9+3;
    r[k+2]=1e9+10;
    for(int i=1;i<=k;i++){
        cin>>r[i];
    }
    
    int psize=sqrt(k)+1;
    
    vector<vector<ll>> pr;
    for(int i=0;i<r.size();){
        vector<ll> mp;
        for(int j=0;j<psize && i<r.size();i++,j++){
            mp.push_back(r[i]);
        }
        if(i!=r.size()-1) mp.push_back(r[i]);
        pr.push_back(mp);
    }
    vector<vector<pair<ll,ll>>> pacs(pr.size());
    vector<ll> aho(pr.size(),-1e11);
    for(int i=0;i<pr.size();i++){
        ll lb=0;
        ll ub=x+1;
        vector<pair<ll,ll>> lbs;
        lbs.push_back({0,0});
        vector<pair<ll,ll>> ubs;
        ll sum=0;
        for(int j=1;j<pr[i].size();j++){
            int sign=-(j%2);
            sum+=sign*(pr[i][j]-pr[i][j-1]);
            if(lb==ub) continue;
            if(sum<0 && lb<-sum){
                if(ub<-sum) lb=ub;
                else lb=-sum;
                lbs.push_back({lb,j});
            }
            else if(sum>0 && x-sum+1<ub){
                if(x-sum+1<lb) ub=lb;
                else ub=x-sum+1;
                ubs.push_back({ub,j});
            }
        }
        
        vector<ll> pacres(pr[i].size()+1);
        pacres[pr[i].size()]=x;
        for(int j=1;j<pr[i].size();j++){
            ll top=x;
            for(int k=j;k<pr[i].size();k++){
                top=max(0LL,top-(pr[i][k]-pr[i][k-1]));
                top=x-top;
            }   
            pacres[j]=top;
        }
        for(int j=0;j<int(lbs.size())-1;j++){
            pacs[i].push_back({lbs[j].first,pacres[lbs[i].second+1]});
        }
        if(lb!=ub){
            pacs[i].push_back({lb,-sum});
            aho[i]=lb;
        }
        for(int j=0;j<ubs.size();j++){
            pacs[i].push_back({ubs[j].first,pacres[ubs[i].second+1]});
        }
    }

    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        ll t,a;
        cin>>t>>a;
        bool istopa=(lower_bound(r.begin(),r.end(),0)-r.begin())%2;
        bool out=false;
        ll top=a;
        for(int j=0;j<pr.size();j++){
            if(t<pr[j].back()){
                for(int k=j;k<pr[i].size();k++){
                    if(pr[i][k]<t){
                        top=max(0LL,top-(t-pr[j][k-1]));
                        cout<<(istopa ? top : x-top)<<endl;
                        goto aholavel;
                    }
                    top=max(0LL,top-(pr[j][k]-pr[j][k-1]));
                    top=x-top;
                }
            }
            else{
                auto val=*lower_bound(pacs[j].begin(),pacs[j].end(),{top,-100000000000LL});;
                if(aho[j]==val.first){
                    top+=val.second;
                }
                else{
                    top=val.second;
                }
            }
        }
        aholavel:
        int xxx;
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:96:38: error: no matching function for call to 'lower_bound(std::vector<std::pair<long long int, long long int> >::iterator, std::vector<std::pair<long long int, long long int> >::iterator, <brace-enclosed initializer list>)'
   96 |                 auto val=*lower_bound(pacs[j].begin(),pacs[j].end(),{top,-100000000000LL});;
      |                           ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:1530:5: note: candidate: 'template<class _ForwardIterator, class _Tp> _ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&)'
 1530 |     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1530:5: note:   template argument deduction/substitution failed:
a.cc:96:38: note:   couldn't deduce template parameter '_Tp'
   96 |                 auto val=*lower_bound(pacs[j].begin(),pacs[j].end(),{top,-100000000000LL});;
      |                           ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:1963:5: note: candidate: 'template<class _FIter, class _Tp, class _Compare> _FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare)'
 1963 |     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1963:5: note:   candidate expects 4 arguments, 3 provided