Language: C++

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 1e5 + 5;

int r[N];
signed main()   {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int S;  cin >> S;
    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)   cin >> r[i];

    int q;  cin >> q;
    int ptr = 1, val = 0;
    int Min = 0, Max = S;

    bool top = 1;

    while (q--) {
        int t;  cin >> t;
        int a;  cin >> a;

        for(; ptr <= n && r[ptr] <= t ; ++ptr, top ^= 1)    {
            int delta = r[ptr] - r[ptr - 1];

            if (Max == Min) {
                val += delta * (top ? -1 : 1);
                val = max(val,0);
                val = min(val,S);
            }
            else    {
                if (top)    {
                    int low = Min + val;

                    if (low <  delta)
                        Min += delta - low;
                    
                    val -= delta;
                    if (Max + val <= 0) {
                        val += Max;
                        Min = 0;
                        Max = 0;
                    }
                }
                else    {
                    if (Max + val + delta > S)
                        Max = S - val - delta,
                        val = S - Max;
                    else
                        val += delta;
                    
                    if (Max <= Min) {
                        val += Max;
                        Max = 0;
                        Min = 0;
                    }
                }
            }
        }
        int ans;
        if (a <= Min)   ans = Min + val;
        if (a >= Max)   ans = Max + val;
        if (Min < a && a < Max) ans = a + val;

        ans += (t - r[ptr - 1]) * (top ? -1 : 1);
        ans = max(ans,0);
        ans = min(ans,S);

        cout << ans << "\n";
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:42:26: error: no matching function for call to 'max(long long int&, int)'
   42 |                 val = max(val,0);
      |                       ~~~^~~~~~~
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: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:42:26: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   42 |                 val = max(val,0);
      |                       ~~~^~~~~~~
/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:
/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:42:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   42 |                 val = max(val,0);
      |                       ~~~^~~~~~~
a.cc:80:18: error: no matching function for call to 'max(long long int&, int)'
   80 |         ans = max(ans,0);
      |               ~~~^~~~~~~
/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:80:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   80 |         ans = max(ans,0);
      |               ~~~^~~~~~~
/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:80:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   80 |         ans = max(ans,0);
      |               ~~~^~~~~~~