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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem1125 : Get Many Persimmon Trees **/
int main() {
  int N, W, H;
  while (cin >> N, N) {
    cin >> W >> H;
    bool field[W][H];
    int num[W][H];

    fill(field[0], field[0] + W * H, false);
    fill(num[0], num[0] + W * H, 0);

    rep(i, N) {
      int x, y;
      cin >> x >> y;
      field[x - 1][y - 1] = true;
    }

    num[0][0] = field[0][0];

    REP(x, 1, W) { num[x][0] = num[x - 1][0] + field[x][0]; }

    REP(x, 1, H) { num[0][x] = num[0][x - 1] + field[0][x]; }

    REP(y, 1, H) {
      REP(x, 1, W) {
        num[x][y] =
            num[x - 1][y] + num[x][y - 1] + field[x][y] - num[x - 1][y - 1];
      }
    }

    int S, T;
    cin >> S >> T;
    int ans = 0;
    rep(y, H - T + 1) {
      rep(x, W - S + 1) {
        int tmp = num[x + S - 1][y + T - 1];

        if (x > 0)
          tmp -= num[x - 1][y + T - 1];
        if (y > 0)
          tmp -= num[x + S - 1][y - 1];
        if (x > 0 && y > 0)
          tmp += num[x - 1][y - 1];
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem1125 : Get Many Persimmon Trees **/
int main() {
  int N, W, H;
  while (cin >> N, N) {
    cin >> W >> H;
    bool field[W][H];
    int num[W][H];

    fill(field[0], field[0] + W * H, false);
    fill(num[0], num[0] + W * H, 0);

    rep(i, N) {
      int x, y;
      cin >> x >> y;
      field[x][y] = true;
    }

    num[0][0] = field[0][0];

    REP(x, 1, W) { num[x][0] = num[x - 1][0] + field[x][0]; }

    REP(x, 1, H) { num[0][x] = num[0][x - 1] + field[0][x]; }

    REP(y, 1, H) {
      REP(x, 1, W) {
        num[x][y] =
            num[x - 1][y] + num[x][y - 1] + field[x][y] - num[x - 1][y - 1];
      }
    }

    int S, T;
    cin >> S >> T;
    int ans = 0;
    rep(y, H - T) {
      rep(x, W - S) {
        int tmp = num[x + S - 1][y + T - 1];

        if (x > 0)
          tmp -= num[x - 1][y + T - 1];
        if (y > 0)
          tmp -= num[x + S - 1][y - 1];
        if (x > 0 && y > 0)
          tmp += num[x - 1][y - 1];
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
}