Language: C++

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<=n;i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
#define EPS 1e-9
#define DEBUG 0
#define ll long long
#define Pll pair<ll, ll>
#define Pii pair<int, int>
 
using namespace std;
struct edge{int from, to; ll cost;};

signed main(){
    ios::sync_with_stdio(false); cin.tie(0);

    ll x, k, q, t, aa, diff;
    cin >> x >> k;
    vector<ll> r(k), a(k), b(k), c(k);
    rep(k){
        cin >> r[i];
        if(i > 0) diff = r[i] - r[i-1];
        if(i == 0){
            if(r[i] > x){
                a[i] = 0;
                b[i] = 0;
                c[i] = 0;
            }else{
                a[i] = r[i];
                b[i] = x;
                c[i] = -r[i];
            }
        }else if(i % 2 == 0){
            if(b[i-1] + c[i-1] - diff <= 0){
                a[i] = 0;
                b[i] = 0;
                c[i] = 0;
            }else{
                if(a[i-1] + c[i-1] - diff <= 0){
                    a[i] = c[i-1] + diff;
                }else{
                    a[i] = max(a[i-1], - c[i-1] + diff);
                }
                b[i] = b[i-1];
                c[i] = c[i-1] - diff;
            }
        }else{
            if(a[i-1]+c[i-1]+diff >= x){
                a[i] = 0;
                b[i] = 0;
                c[i] = x;
            }else{
                a[i] = a[i-1];
                if(b[i-1]+c[i-1]+diff >= x){
                    b[i] = - c[i-1] + x - diff;
                }else{
                    b[i] = min(b[i-1], - c[i-1] + (x - diff));
                }
                c[i] = c[i-1] + diff;
            }
        }
    }

    cin >> q;
    rep(q){
        cin >> t >> aa;
        ll ans, ii = (ll)(upper_bound(all(r), t) - r.begin()) - 1;

        if(ii == -1){
            cout << max(0, aa - t) << endl;
            continue;
        }
        //cout << a[ii-1] <<  " " << b[ii-1] << " " << c[ii-1] << endl;
        //cout << a[ii] <<  " " << b[ii] << " " << c[ii] << endl;
        if(aa <= a[ii]){
            ans = a[ii] + c[ii];
        }else if(aa >= b[ii]){
            ans = b[ii] + c[ii];
        }else{
            ans = aa + c[ii];
        }

        if(ii % 2 == 1){
            ans = max(0, ans - (t - r[ii]));
        }else{
            ans = min(x, ans + (t - r[ii]));
        }
        
        cout << ans << endl;
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:89:24: error: no matching function for call to 'max(int, long long int)'
   89 |             cout << max(0, aa - t) << endl;
      |                     ~~~^~~~~~~~~~~
In file included from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:89:24: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   89 |             cout << max(0, aa - t) << endl;
      |                     ~~~^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:89:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   89 |             cout << max(0, aa - t) << endl;
      |                     ~~~^~~~~~~~~~~
a.cc:103:22: error: no matching function for call to 'max(int, long long int)'
  103 |             ans = max(0, ans - (t - r[ii]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:103:22: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  103 |             ans = max(0, ans - (t - r[ii]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:103:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  103 |             ans = max(0, ans - (t - r[ii]));
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~