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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> Mat;
typedef tuple<int, int, int, int> T;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

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

  int n, m, t, p;
  while (1) {
    cin >> n >> m >> t >> p;

    if (n + m + t + p == 0)
      break;
    vector<vector<int>> paper(33 * 21, vector<int>(33 * 21, 0));
    int ox = 0, oy = 0, dx = n, dy = m;
    rep(i, m) rep(j, n) paper[i][j] += 1;

    rep(_, t) {
      int d, c;
      cin >> d >> c;
      if (d == 1) {
        dx = max(dx, ox + c * 2);
        FOR(i, ox, ox + c) {
          FOR(j, oy, dy) paper[j][2 * ox + 2 * c - 1 - i] += paper[j][i];
        }
        ox += c;
      } else {
        dy = max(dy, oy + c * 2);
        FOR(i, oy, oy + c) {
          FOR(j, ox, dx) paper[2 * oy + 2 * c - 1 - i][j] += paper[i][j];
        }
        oy += c;
      }
    }

    /*
      FOR(i, oy, dy) {
        FOR(j, ox, dx) cout << paper[i][j];
        cout << endl;
      }
      */

    rep(_, p) {
      int x, y;
      cin >> x >> y;
      cout << paper[y + oy][x + ox] << "\\n";
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> Mat;
typedef tuple<int, int, int, int> T;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

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

  int n, m, t, p;
  while (1) {
    cin >> n >> m >> t >> p;

    if (n + m + t + p == 0)
      break;
    vector<vector<int>> paper((m + 1) * 2, vector<int>((n + 1) * 2, 0));
    int ox = 0, oy = 0, dx = n, dy = m;
    rep(i, m) rep(j, n) paper[i][j] += 1;

    rep(_, t) {
      int d, c;
      cin >> d >> c;
      if (d == 1) {
        dx = max(dx, ox + (c + 1) * 2);
        FOR(i, ox, ox + c) { FOR(j, oy, dy) paper[j][c + i] += paper[j][i]; }
        ox += c;
      } else {
        dy = max(dy, oy + (c + 1) * 2);
        FOR(i, oy, oy + c) { FOR(j, ox, dx) paper[c + i][j] += paper[i][j]; }
        oy += c;
      }
    }

    rep(_, p) {
      int x, y;
      cin >> x >> y;
      cout << paper[y + oy][x + ox] << endl;
    }
  }

  return 0;
}