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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
long long l[10000], r[10000], x1, x2, y1, y2, m, n, t, p, d[10000], c[10000],
    a[300][300], b[300][300], x, y;
int main() {
  while (1) {
    scanf("%lld%lld%lld%lld", &n, &m, &t, &p);
    if (n == 0 && m == 0 && t == 0 && p == 0)
      break;
    x1 = 32;
    y1 = 32;
    x2 = 31 + m;
    y2 = 31 + n;
    memset(a, 0, sizeof(a));
    for (int i = 32; i <= m + 31; i++)
      for (int j = 32; j <= n + 31; j++)
        a[i][j] = 1;
    for (int k = 1; k <= t; k++) {
      scanf("%lld%lld", &d[k], &c[k]);
      if (d[k] == 2) {
        for (int i = x2 - c[k] + 1; i <= x2; i++)
          for (int j = 32; j <= y2; j++) {
            a[2 * (x2 - c[k]) + 1 - i][j] += a[i][j];
            a[i][j] = 0;
          }
        x1 = min(x1, x2 - 2 * c[k] + 1);
        x2 = x2 - c[k];
        x = 31;
        for (int i = x1; i <= x2; i++) {
          x++;
          y = 31;
          for (int j = y1; j <= y2; j++)
            b[x][++y] = a[i][j];
        }
        memset(a, 0, sizeof(a));
        for (int i = 32; i <= x; i++)
          for (int j = 32; j <= y; j++)
            a[i][j] = b[i][j];
        x1 = 32;
        y1 = 32;
        x2 = x;
        y2 = y;
      } else if (d[k] == 1) {
        c[k] += 31;
        for (int i = 32; i <= x2; i++)
          for (int j = 32; j <= c[k]; j++) {
            a[i][2 * c[k] + 1 - j] += a[i][j];
            a[i][j] = 0;
          }
        y2 = max(y2, 2 * c[k] - 31);
        y1 = c[k] + 1;
        x = 31;
        for (int i = x1; i <= x2; i++) {
          x++;
          y = 31;
          for (int j = y1; j <= y2; j++)
            b[x][++y] = a[i][j];
        }
        memset(a, 0, sizeof(a));
        for (int i = 32; i <= x; i++)
          for (int j = 32; j <= y; j++)
            a[i][j] = b[i][j];
        x1 = 32;
        y1 = 32;
        x2 = x;
        y2 = y;
      }
    }
    for (int k = 1; k <= p; k++) {
      scanf("%lld%lld", &l[k], &r[k]);
      l[k] += 32;
      printf("%lld\\n", a[x2 - r[k]][l[k]]);
    }
  }
  return (0);
}


Error: 0

Fixed submission:

#include <cstdio>
const int maxn = 33;
int g[maxn][maxn];
int n = 0, m = 0, t = 0, p = 0;
int d = 0, c = 0;
int x = 0, y = 0;
int dx = 0, dy = 0;
int main() {
  // freopen("C:\\\\Users\\\\atara\\\\Desktop\\\\in.txt","r",stdin);
  while (scanf("%d%d%d%d", &n, &m, &t, &p) == 4 && (n + m + t + p) != 0) {
    dx = 0;
    dy = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) {
        g[i][j] = 1;
      }
    for (int k = 0; k < t; ++k) {
      scanf("%d%d", &d, &c);
      if (d == 1) {
        if (c > n / 2)
          c = n - c;
        dy += c;
        for (int i = dx; i < m; ++i)
          for (int j = dy; j < dy + c; ++j)
            g[i][j] = g[i][j] + g[i][j - c];
      } else {
        if (c > m / 2)
          c = m - c;
        dx += c;
        for (int i = dx; i < dx + c; ++i)
          for (int j = dy; j < n; ++j)
            g[i][j] = g[i][j] + g[i - c][j];
      }
      /* printf("%d %d\\n",dx,dy);
      for(int i=m-1;i>=0;--i)
       {

            for(int j=0;j<n;++j)
               printf("%d ",g[i][j]);
            printf("\\n");
       }*/
    }
    for (int k = 0; k < p; ++k) {
      scanf("%d%d", &x, &y);
      printf("%d\\n", g[dx + y][dy + x]);
    }
  }
  return 0;
}