Problem p01110 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MA_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m, t, p;
  cin >> n >> m >> t >> p;
  while (n) {
    int a[m * t][n * t];
    int x = 0, y = 0, w = n, h = m;
    rep(i, m) rep(j, n) a[i][j] = 1;
    while (t--) {
      int d, c;
      cin >> d >> c;
      if (d == 1) {
        Rep(i, y, h) {
          rep(j, c) { a[i][x + 2 * c - j - 1] += a[i][x + j]; }
        }
        w = max(w, x + 2 * c);
        x += c;
      } else {
        Rep(i, x, w) {
          rep(j, c) { a[y + 2 * c - j - 1][i] += a[y + j][i]; }
        }
        h = max(h, y + 2 * c);
        y += c;
      }
    }
    while (p--) {
      int r, t;
      cin >> r >> t;
      cout << a[y + t][x + r] << endl;
    }
    cin >> n >> m >> t >> p;
  }
}


Fixed submission:
#define _USE_MA_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a, 0, sizeof(a))
#define inf (1ll << 60)
#define Yes(f) cout << (f ? "Yes" : "No") << endl
#define yes(f) cout << (f ? "yes" : "no") << endl
#define YES(f) cout << (f ? "YES" : "NO") << endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m, t, p;
  cin >> n >> m >> t >> p;
  while (n) {
    int a[1000][1000] = {};
    int x = 0, y = 0, w = n, h = m;
    rep(i, m) rep(j, n) a[i][j] = 1;
    while (t--) {
      int d, c;
      cin >> d >> c;
      if (d == 1) {
        Rep(i, y, h) {
          rep(j, c) { a[i][x + 2 * c - j - 1] += a[i][x + j]; }
        }
        w = max(w, x + 2 * c);
        x += c;
      } else {
        Rep(i, x, w) {
          rep(j, c) { a[y + 2 * c - j - 1][i] += a[y + j][i]; }
        }
        h = max(h, y + 2 * c);
        y += c;
      }
    }
    while (p--) {
      int r, t;
      cin >> r >> t;
      cout << a[y + t][x + r] << endl;
    }
    cin >> n >> m >> t >> p;
  }
}