Problem p00459 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define fr first
#define sc second
typedef pair<int, int> P;
int n, m;
deque<P> d;
int _sum(P p) { return p.sc - p.fr + 1; }
int query(int &p, int &q, int &r) {
  int ret = 0, ans = 0;
  while (ret + _sum(d.front()) < p) {
    ret += _sum(d.front());
    d.pop_front();
  }
  if (ret != p - 1)
    d.front().fr += p - ret;
  ret = p;
  while (ret + _sum(d.front()) <= q) {
    if (d.front().sc <= r)
      ans += _sum(d.front());
    else if (d.front().fr <= r)
      ans += _sum(P(d.front().fr, r));
    ret += _sum(d.front());
    d.pop_front();
  }
  if (!d.empty() && ret != q) {
    P p = P(d.front().fr, d.front().fr + q - ret - 1);
    if (p.sc <= r)
      ans += _sum(P(p.fr, p.sc));
    else if (p.fr <= r)
      ans += _sum(P(p.fr, r));
  }
  return ans;
}
void shuffle(int *xy) {
  deque<P> tmp[2];
  int ret = 0;
  for (int i = 0; i < 2; i++) {
    while (ret + _sum(d.front()) <= xy[i]) {
      tmp[i].push_back(d.front());
      ret += _sum(d.front());
      d.pop_front();
    }
    if (ret != xy[i]) {
      tmp[i].push_back(P(d.front().fr, d.front().fr + xy[i] - ret - 1));
      d.front().fr = d.front().fr + xy[i] - ret;
      ret = xy[i];
    }
  }
  d.insert(d.end(), tmp[1].begin(), tmp[1].end());
  d.insert(d.end(), tmp[0].begin(), tmp[0].end());
}
int main() {
  while (cin >> n, n) {
    cin >> m;
    d.push_back(P(1, n));
    int p, q, r;
    cin >> p >> q >> r;
    while (m--) {
      int xy[2];
      cin >> xy[0] >> xy[1];
      shuffle(xy);
    }
    cout << query(--p, q, r) << endl;
    d.clear();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define fr first
#define sc second
typedef pair<int, int> P;
int n, m;
deque<P> d;
int _sum(P p) { return p.sc - p.fr + 1; }
int query(int &p, int &q, int &r) {
  int ret = 0, ans = 0;
  while (_sum(d.front()) < p) {
    ret += _sum(d.front());
    d.pop_front();
  }
  if (ret != p - 1)
    d.front().fr = d.front().fr + p - ret;
  ret = p;
  while (ret + _sum(d.front()) <= q) {
    if (d.front().sc <= r)
      ans += _sum(d.front());
    else if (d.front().fr <= r)
      ans += _sum(P(d.front().fr, r));
    ret += _sum(d.front());
    d.pop_front();
  }
  if (ret != q) {
    P p = P(d.front().fr, d.front().fr + q - ret - 1);
    if (p.sc <= r)
      ans += _sum(P(p.fr, p.sc));
    else if (p.fr <= r)
      ans += _sum(P(p.fr, r));
  }
  return ans;
}
void shuffle(int *xy) {
  deque<P> tmp[2];
  int ret = 0;
  for (int i = 0; i < 2; i++) {
    while (ret + _sum(d.front()) <= xy[i]) {
      tmp[i].push_back(d.front());
      ret += _sum(d.front());
      d.pop_front();
    }
    if (ret != xy[i]) {
      tmp[i].push_back(P(d.front().fr, d.front().fr + xy[i] - ret - 1));
      d.front().fr = d.front().fr + xy[i] - ret;
      ret = xy[i];
    }
  }
  d.insert(d.end(), tmp[1].begin(), tmp[1].end());
  d.insert(d.end(), tmp[0].begin(), tmp[0].end());
}
int main() {
  while (cin >> n, n) {
    cin >> m;
    d.push_back(P(1, n));
    int p, q, r;
    cin >> p >> q >> r;
    while (m--) {
      int xy[2];
      cin >> xy[0] >> xy[1];
      shuffle(xy);
    }
    cout << query(--p, q, r) << endl;
    d.clear();
  }
}