Problem p03707 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

long long int INF = 1e18;
long long int mod = 1000000007;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(15) << x << endl;
#define pu(x) cout << x;
#define re(i, n)                                                               \
  for (i = 0; i < n; i++)                                                      \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // abcのbed乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}
ll gcd(ll number1, ll number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}

ll i, j, k, ii, jj, r, l;
ll n, m, num, sum, ans;
ll a, b, c, d, e, g, h, w;
ll x[500005], y[500005], z[500005];
ll dp[5005][5005];
ll migi[5005][5005];
ll sita[5005][5005];
char s[5005][5005];

int main() {
  cin >> h >> w >> k;
  for (i = 1; i <= h; i++) {
    cin >> s[i];
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (s[i][j - 1] == '1')
        dp[i][j] = 1;
    }
  }

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w - 1; j++) {
      if (dp[i][j] == 1 && dp[i][j + 1] == 1) {
        migi[i][j] = 1;
      }
    }
  }
  for (i = 1; i <= h - 1; i++) {
    for (j = 1; j <= w; j++) {
      if (dp[i][j] == 1 && dp[i + 1][j] == 1) {
        sita[i][j] = 1;
      }
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      dp[i][j] += dp[i - 1][j];
      migi[i][j] += migi[i - 1][j];
      sita[i][j] += sita[i - 1][j];
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      dp[i][j] += dp[i][j - 1];
      migi[i][j] += migi[i][j - 1];
      sita[i][j] += sita[i][j - 1];
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      // pe(sita[i][j]);
    }
    // el;
  }
  for (i = 0; i < k; i++) {
    ll bx, by, ax, ay;
    cin >> bx >> by >> ax >> ay;
    num = dp[ax][ay] - dp[bx - 1][ay] - dp[ax][by - 1] + dp[bx - 1][by - 1];
    // pe(num);
    if (by != ay)
      num -= migi[ax][ay - 1] - migi[bx - 1][ay - 1] - migi[ax][by - 1] +
             migi[bx - 1][by - 1];
    if (bx != ax)
      num -= sita[ax - 1][ay] - sita[bx - 1][ay] - sita[ax - 1][by - 1] +
             sita[bx - 1][by - 1];
    p(num);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

long long int INF = 1e18;
long long int mod = 1000000007;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(15) << x << endl;
#define pu(x) cout << x;
#define re(i, n)                                                               \
  for (i = 0; i < n; i++)                                                      \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // abcのbed乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}
ll gcd(ll number1, ll number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}

ll i, j, k, ii, jj, r, l;
ll n, m, num, sum, ans;
ll a, b, c, d, e, g, h, w;
ll x[500005], y[500005], z[500005];
ll dp[2005][2005];
ll migi[2005][2005];
ll sita[2005][2005];
char s[2005][2005];

int main() {
  cin >> h >> w >> k;
  for (i = 1; i <= h; i++) {
    cin >> s[i];
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (s[i][j - 1] == '1')
        dp[i][j] = 1;
    }
  }

  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w - 1; j++) {
      if (dp[i][j] == 1 && dp[i][j + 1] == 1) {
        migi[i][j] = 1;
      }
    }
  }
  for (i = 1; i <= h - 1; i++) {
    for (j = 1; j <= w; j++) {
      if (dp[i][j] == 1 && dp[i + 1][j] == 1) {
        sita[i][j] = 1;
      }
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      dp[i][j] += dp[i - 1][j];
      migi[i][j] += migi[i - 1][j];
      sita[i][j] += sita[i - 1][j];
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      dp[i][j] += dp[i][j - 1];
      migi[i][j] += migi[i][j - 1];
      sita[i][j] += sita[i][j - 1];
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      // pe(sita[i][j]);
    }
    // el;
  }
  for (i = 0; i < k; i++) {
    ll bx, by, ax, ay;
    cin >> bx >> by >> ax >> ay;
    num = dp[ax][ay] - dp[bx - 1][ay] - dp[ax][by - 1] + dp[bx - 1][by - 1];
    // pe(num);
    if (by != ay)
      num -= migi[ax][ay - 1] - migi[bx - 1][ay - 1] - migi[ax][by - 1] +
             migi[bx - 1][by - 1];
    if (bx != ax)
      num -= sita[ax - 1][ay] - sita[bx - 1][ay] - sita[ax - 1][by - 1] +
             sita[bx - 1][by - 1];
    p(num);
  }
  return 0;
}