Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int vsum[1001][1001];
int esum[1001][1001][4];

int getVSum(int x1, int y1, int x2, int y2) {
  return vsum[x2 + 1][y2 + 1] - vsum[x1][y2 + 1] - vsum[x2 + 1][y1] +
         vsum[x1][y1];
}

int getESum(int x1, int y1, int x2, int y2, int dir) {
  return esum[x2 + 1][y2 + 1][dir] - esum[x1][y2 + 1][dir] -
         esum[x2 + 1][y1][dir] + esum[x1][y1][dir];
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M, Q;
  cin >> N >> M >> Q;

  auto inside = [&](int y, int x) {
    if (0 <= y && y < N && 0 <= x && x < M)
      return true;
    return false;
  };

  vector<string> f(N);
  rep(i, N) cin >> f[i];
  rep(i, N) rep(j, M) {
    if (f[i][j] == '1') {
      vsum[i + 1][j + 1] = 1;
      rep(k, 4) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (!inside(ni, nj))
          continue;
        if (f[ni][nj] == '1')
          esum[i + 1][j + 1][k]++;
      }
    }
  }
  rep(i, N) rep(j, M + 1) {
    vsum[i + 1][j] += vsum[i][j];
    rep(k, 4) { esum[i + 1][j][k] += esum[i][j][k]; }
  }
  rep(j, M) rep(i, N + 1) {
    vsum[i][j + 1] += vsum[i][j];
    rep(k, 4) { esum[i][j + 1][k] += esum[i][j][k]; }
  }

  while (Q--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    int V = getVSum(x1, y1, x2, y2);
    int E = 0;
    E += getESum(x1, y1, x2 - 1, y2, 0);
    E += getESum(x1, y1, x2, y2 - 1, 1);
    E += getESum(x1 + 1, y1, x2, y2, 2);
    E += getESum(x1, y1 + 1, x2, y2, 3);
    E /= 2;
    cout << V - E << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int vsum[2001][2001];
int esum[2001][2001][4];

int getVSum(int x1, int y1, int x2, int y2) {
  return vsum[x2 + 1][y2 + 1] - vsum[x1][y2 + 1] - vsum[x2 + 1][y1] +
         vsum[x1][y1];
}

int getESum(int x1, int y1, int x2, int y2, int dir) {
  return esum[x2 + 1][y2 + 1][dir] - esum[x1][y2 + 1][dir] -
         esum[x2 + 1][y1][dir] + esum[x1][y1][dir];
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M, Q;
  cin >> N >> M >> Q;

  auto inside = [&](int y, int x) {
    if (0 <= y && y < N && 0 <= x && x < M)
      return true;
    return false;
  };

  vector<string> f(N);
  rep(i, N) cin >> f[i];
  rep(i, N) rep(j, M) {
    if (f[i][j] == '1') {
      vsum[i + 1][j + 1] = 1;
      rep(k, 4) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (!inside(ni, nj))
          continue;
        if (f[ni][nj] == '1')
          esum[i + 1][j + 1][k]++;
      }
    }
  }
  rep(i, N) rep(j, M + 1) {
    vsum[i + 1][j] += vsum[i][j];
    rep(k, 4) { esum[i + 1][j][k] += esum[i][j][k]; }
  }
  rep(j, M) rep(i, N + 1) {
    vsum[i][j + 1] += vsum[i][j];
    rep(k, 4) { esum[i][j + 1][k] += esum[i][j][k]; }
  }

  while (Q--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    int V = getVSum(x1, y1, x2, y2);
    int E = 0;
    E += getESum(x1, y1, x2 - 1, y2, 0);
    E += getESum(x1, y1, x2, y2 - 1, 1);
    E += getESum(x1 + 1, y1, x2, y2, 2);
    E += getESum(x1, y1 + 1, x2, y2, 3);
    E /= 2;
    cout << V - E << "\n";
  }
  return 0;
}