Language: C++

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

typedef long long ll;

ll X, K;
typedef array<ll, 4> State;
map<ll, State> States;
vector<ll> R;
ll Q;

void simulate() {
  State state = {0, 0, X, X};
  ll gravity = -1;
  ll t = 0;
  for (ll i = 0; i < R.size(); ++i) {
    ll t1 = min(t + (gravity < 0 ? state[1] : X - state[3]), R[i]);
    state[gravity < 0 ? 1 : 3] += gravity * (t1 - t);
    state[gravity < 0 ? 3 : 1] += gravity * (t1 - t);
    States[t1] = state;
    t = t1;
    ll t2 = min(t + (gravity < 0 ? state[3] : X - state[1]), R[i]);
    state[gravity < 0 ? 0 : 2] -= gravity * (t2 - t);
    state[gravity < 0 ? 3 : 1] += gravity * (t2 - t);
    States[t2] = state;
    t = t2;
    States[R[i]] = state;
    t = R[i];
    gravity *= -1;
  }
}
State interpolate(ll t0, State from, ll t1, State to, ll t) {
  State res;
  for (ll i : {0, 1, 2, 3})
    res[i] = from[i] + (to[i] - from[i]) / (t1 - t0) * (t - t0);
  return res;
}
int main() {
  cin >> X >> K;
  for (ll i = 0; i < K; ++i) {
    ll r;
    cin >> r;
    R.push_back(r);
  }
  R.push_back(1e9 + 1);
  simulate();
  cin >> Q;
  for (ll i = 0; i < Q; ++i) {
    ll t, a;
    cin >> t >> a;
    auto it1 = States.upper_bound(t);
    auto it0 = prev(it1);
    auto state =
        interpolate(it0->first, it0->second, it1->first, it1->second, t);
    ll x0, y0, x1, y1;
    tie(x0, y0, x1, y1) = state;
    cout << (a <= x0 ? y0 : x1 <= a ? y1 : (a - x0) + y0) << endl;
  }
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:56:27: error: no match for 'operator=' (operand types are 'std::tuple<long long int&, long long int&, long long int&, long long int&>' and 'std::array<long long int, 4>')
   56 |     tie(x0, y0, x1, y1) = state;
      |                           ^~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/tuple:1930:9: note: candidate: 'template<class ... _UElements> std::__enable_if_t<((bool)__assignable<const _UElements& ...>()), std::tuple< <template-parameter-1-1> >&> std::tuple< <template-parameter-1-1> >::operator=(const std::tuple<_Args1 ...>&) [with _UElements = {_UElements ...}; _Elements = {long long int&, long long int&, long long int&, long long int&}]'
 1930 |         operator=(const tuple<_UElements...>& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:1930:9: note:   template argument deduction/substitution failed:
a.cc:56:27: note:   'std::array<long long int, 4>' is not derived from 'const std::tuple<_UTypes ...>'
   56 |     tie(x0, y0, x1, y1) = state;
      |                           ^~~~~
/usr/include/c++/14/tuple:1940:9: note: candidate: 'template<class ... _UElements> std::__enable_if_t<((bool)__assignable<_UElements ...>()), std::tuple< <template-parameter-1-1> >&> std::tuple< <template-parameter-1-1> >::operator=(std::tuple<_Args1 ...>&&) [with _UElements = {_UElements ...}; _Elements = {long long int&, long long int&, long long int&, long long int&}]'
 1940 |         operator=(tuple<_UElements...>&& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:1940:9: note:   template argument deduction/substitution failed:
a.cc:56:27: note:   'std::array<long long int, 4>' is not derived from 'std::tuple<_UTypes ...>'
   56 |     tie(x0, y0, x1, y1) = state;
      |                           ^~~~~
/usr/include/c++/14/tuple:1907:7: note: candidate: 'std::tuple< <template-parameter-1-1> >& std::tuple< <template-parameter-1-1> >::operator=(std::__conditional_t<((bool)__assignable<const _Elements& ...>()), const std::tuple< <template-parameter-1-1> >&, const std::__nonesuch&>) [with _Elements = {long long int&, long long int&, long long int&, long long int&}; std::__conditional_t<((bool)__assignable<const _Elements& ...>()), const std::tuple< <template-parameter-1-1> >&, const std::__nonesuch&> = const std::tuple<long long int&, long long int&, long long int&, long long int&>&]'
 1907 |       operator=(__conditional_t<__assignable<const _Elements&...>(),
      |       ^~~~~~~~
/usr/include/c++/14/tuple:1909:52: note:   no known conversion for argument 1 from 'std::array<long long int, 4>' to 'std::__conditional_t<true, const std::tuple<long long int&, long long int&, long long int&, long long int&>&, const std::__nonesuch&>' {aka 'const std::tuple<long long int&, long long int&, long long int&, long long int&>&'}
 1907 |       operator=(__conditional_t<__assignable<const _Elements&...>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1908 |                                 const tuple&,
      |                                 ~~~~~~~~~~~~~       
 1909 |                                 const __nonesuch&> __in)
      |                                 ~~~~~~~~~~~~~~~~~~~^~~~
/usr/include/c++/14/tuple:1918:7: note: candidate: 'std::tuple< <template-parameter-1-1> >& std::tuple< <template-parameter-1-1> >::operator=(std::__conditional_t<((bool)__assignable<_Elements ...>()), std::tuple< <template-parameter-1-1> >&&, std::__nonesuch&&>) [with _Elements = {long long int&, long long int&, long long int&, long long int&}; std::__conditional_t<((bool)__assignable<_Elements ...>()), std::tuple< <template-parameter-1-1> >&&, std::__nonesuch&&> = std::tuple<long long int&, long long int&, long long int&, long long int&>&&]'
 1918 |       operator=(__conditional_t<__assignable<_Elements...>(),
      |       ^~~~~~~~
/usr/include/c++/14/tuple:1920:47: note:   no known conversion for argument 1 from 'std::array<long long int, 4>' to 'std::__conditional_t<true, std::tuple<long long int&, long long int&, long long int&, long long int&>&&, std::__nonesuch&&>' {aka 'std::tuple<long long int&, long long int&, long long int&, long long int&>&&'}
 1918 |       operator=(__conditional_t<__assignable<_Elements...>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1919 |                                 tuple&&,
      |                                 ~~~~~~~~       
 1920 |                                 __nonesuch&&> __in)
      |                                 ~~~~~~~~~~~~~~^~~~