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

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

int n, m, t, p, mm, nn;
int memo[100][100];
int d, c, x, y;

int main() {
  int i, j, a, ans;
  while (1) {
    cin >> n >> m >> t >> p;
    if (n + m + t + p == 0)
      break;
    nn = n;
    mm = m;
    ans = 0;
    for (i = 0; i < 100; ++i)
      for (j = 0; j < 100; ++j)
        memo[i][j] = 0;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        memo[i][j] = 1;
    for (a = 0; a < t; a++) {
      cin >> d >> c;
      if (d == 1) {
        for (i = 0; i < mm; i++)
          for (j = 0; j < c / 2; j++) {
            swap(memo[i][j], memo[i][c - j - 1]);
          }
        if (c <= nn / 2)
          nn -= c;
        else
          nn = c;
        for (i = 0; i < mm; i++)
          for (j = 0; j < nn; j++) {
            if (j < c)
              memo[i][j] += memo[i][j + c];
            else
              memo[i][j] = memo[i][j + c];
            memo[i][j + c] = 0;
          }
      } else {
        for (i = 0; i < c / 2; i++)
          for (j = 0; j < nn; j++) {
            swap(memo[i][j], memo[c - i - 1][j]);
          }
        if (c <= mm / 2)
          mm -= c;
        else
          mm = c;
        for (i = 0; i < mm; i++)
          for (j = 0; j < nn; j++) {
            if (i < c)
              memo[i][j] += memo[i + c][j];
            else
              memo[i][j] = memo[i + c][j];
            memo[i + c][j] = 0;
          }
      }
      /*for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
          if(i >= mm || j >= nn) memo[i][j] = 0;*/
      /*for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) cout << memo[i][j] << " ";
        cout << endl;
      }
      cout << endl;*/
      // cout << n << " " << nn << " " << m << " " << mm << endl;
    }
    for (a = 0; a < p; a++) {
      cin >> x >> y;
      cout << memo[y][x] << endl;
      // if(memo[y][x] == 96)
      // cout << n << " " << m << " " << t << " " << p << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

int n, m, t, p, mm, nn;
int memo[40][40];
int d, c, x, y;

int main() {
  int i, j, a, ans;
  while (1) {
    cin >> n >> m >> t >> p;
    if (n + m + t + p == 0)
      break;
    nn = n;
    mm = m;
    ans = 0;
    for (i = 0; i < 40; ++i)
      for (j = 0; j < 40; ++j)
        memo[i][j] = 0;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        memo[i][j] = 1;
    for (a = 0; a < t; a++) {
      cin >> d >> c;
      if (d == 1) {
        for (i = 0; i < mm; i++)
          for (j = 0; j < c / 2; j++) {
            swap(memo[i][j], memo[i][c - j - 1]);
          }
        if (nn - c > c / 2)
          nn -= c;
        else
          nn = c;
        for (i = 0; i < mm; i++)
          for (j = 0; j < nn; j++) {
            if (j < c)
              memo[i][j] += memo[i][j + c];
            else
              memo[i][j] = memo[i][j + c];
            memo[i][j + c] = 0;
          }
      } else {
        for (i = 0; i < c / 2; i++)
          for (j = 0; j < nn; j++) {
            swap(memo[i][j], memo[c - i - 1][j]);
          }
        if (mm - c > c / 2)
          mm -= c;
        else
          mm = c;
        for (i = 0; i < mm; i++)
          for (j = 0; j < nn; j++) {
            if (i < c)
              memo[i][j] += memo[i + c][j];
            else
              memo[i][j] = memo[i + c][j];
            memo[i + c][j] = 0;
          }
      }
      /*for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
          if(i >= mm || j >= nn) memo[i][j] = 0;*/
      /*for(i=0;i<m;i++){
        for(j=0;j<n;j++)cout << memo[i][j]<<" ";
        cout << endl;
        }
        cout << endl;*/
    }
    for (a = 0; a < p; a++) {
      cin >> x >> y;
      cout << memo[y][x] << endl;
    }
  }
  return 0;
}