Problem p03616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
using namespace std;
int INF = 1e18;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k, ai;
  cin >> n >> k;
  vector<int> data;
  data.push_back(0);
  for (int i = 0; i < k; i++) {
    cin >> ai;
    data.push_back(ai);
  }
  data.push_back(INF);
  int q;
  cin >> q;
  vector<pair<int, int>> qv(n);
  for (int i = 0; i < q; i++) {
    int f, s;
    cin >> f >> s;
    pair<int, int> p = make_pair(f, s);
    qv[i] = p;
  }
  int l = 0;
  int r = n;
  int uf = 1, us = 1;
  int u = 0;
  bool mins = true;
  vector<int> answers(q);
  for (int i = 1; i <= k + 1; i++) {
    while (u < q && qv[u].first < data[i]) {
      int diff = qv[u].first - data[i - 1];
      int old;
      if (r == l)
        old = l;
      else {
        if (qv[u].second < uf)
          old = l;
        else if (qv[u].second > n - us)
          old = r;
        else
          old = l + (qv[u].second - uf) + 1;
      }
      if (mins)
        old -= diff;
      else
        old += diff;
      if (old < 0)
        old = 0;
      if (old > n)
        old = n;
      answers[u] = old;
      u++;
    }
    if (i > k)
      break;
    if (mins) {
      l -= (data[i] - data[i - 1]);
      if (l < 0) {
        uf -= l;
        l = 0;
      }
      r -= (data[i] - data[i - 1]);
      if (r < 0)
        r = 0;
    } else {
      r += (data[i] - data[i - 1]);
      if (r > n) {
        us += (r - n);
        r = n;
      }
      l += (data[i] - data[i - 1]);
      if (l > n)
        l = n;
    }
    mins ^= 1;
  }
  for (int i = 0; i < q; i++) {
    cout << answers[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
using namespace std;
int INF = 1e18;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k, ai;
  cin >> n >> k;
  vector<int> data;
  data.push_back(0);
  for (int i = 0; i < k; i++) {
    cin >> ai;
    data.push_back(ai);
  }
  data.push_back(INF);
  int q;
  cin >> q;
  vector<pair<int, int>> qv(q);
  for (int i = 0; i < q; i++) {
    int f, s;
    cin >> f >> s;
    pair<int, int> p = make_pair(f, s);
    qv[i] = p;
  }
  int l = 0;
  int r = n;
  int uf = 1, us = 1;
  int u = 0;
  bool mins = true;
  vector<int> answers(q);
  for (int i = 1; i <= k + 1; i++) {
    while (u < q && qv[u].first < data[i]) {
      int diff = qv[u].first - data[i - 1];
      int old;
      if (r == l)
        old = l;
      else {
        if (qv[u].second < uf)
          old = l;
        else if (qv[u].second > n - us)
          old = r;
        else
          old = l + (qv[u].second - uf) + 1;
      }
      if (mins)
        old -= diff;
      else
        old += diff;
      if (old < 0)
        old = 0;
      if (old > n)
        old = n;
      answers[u] = old;
      u++;
    }
    if (i > k)
      break;
    if (mins) {
      l -= (data[i] - data[i - 1]);
      if (l < 0) {
        uf -= l;
        l = 0;
      }
      r -= (data[i] - data[i - 1]);
      if (r < 0)
        r = 0;
    } else {
      r += (data[i] - data[i - 1]);
      if (r > n) {
        us += (r - n);
        r = n;
      }
      l += (data[i] - data[i - 1]);
      if (l > n)
        l = n;
    }
    mins ^= 1;
  }
  for (int i = 0; i < q; i++) {
    cout << answers[i] << endl;
  }
  return 0;
}