Problem p03707 (C++)

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x)*1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)

#define dbg(x) cerr << (#x) << " --> " << (x) << nl;
#define Kazakhstan ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

#define Toktama ""

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
// typedef tree < pair <int, int>, null_type, less < pair <int, int> >,
// rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 2000 + 7, inf = 1e9 + 7, mod = 1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1},
          dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

int get_int() {
  char x = getchar();
  bool mns = 0;
  while (!isdigit(x))
    mns |= x == '-', x = getchar();
  int res = 0;
  while (isdigit(x))
    res = res * 10 + x - '0', x = getchar();
  if (mns)
    res = -res;
  return res;
}
void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
int mult(int x, int y) { return x * 1ll * y % mod; }
int sum(int x, int y) {
  add(x, y);
  return x;
}

int n, m, q;
int f[3][N][N];
char a[N][N];

int sum(int t, int x1, int y1, int x2, int y2) {
  if (y1 > y2)
    return 0;
  if (x1 > x2)
    return 0;
  return f[t][x2][y2] - f[t][x1 - 1][y2] - f[t][x2][y1 - 1] +
         f[t][x1 - 1][y1 - 1];
}
void solve() {
  cin >> n >> m >> q;
  rep(i, 1, n) {
    rep(j, 1, m) {
      cin >> a[i][j];
      if (a[i][j] == '1') {
        f[0][i][j] = 1;
        if (a[i][j - 1] == '1')
          f[1][i][j - 1] = 1;
        if (a[i - 1][j] == '1')
          f[2][i - 1][j] = 1;
      }
    }
  }
  rep(t, 0, 3) {
    rep(i, 1, n) rep(j, 1, m) f[t][i][j] +=
        f[t][i - 1][j] + f[t][i][j - 1] - f[t][i - 1][j - 1];
  }
  rep(i, 1, q) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int cnt = sum(0, x1, y1, x2, y2) - sum(1, x1, y1, x2, y2 - 1) -
              sum(2, x1, y1, x2 - 1, y2);
    cout << cnt << nl;
  }
}
int main() {
#ifndef Toktama
  freopen(Toktama ".in", "r", stdin);
  freopen(Toktama ".out", "w", stdout);
#endif
  Kazakhstan int t = 1;
  while (t--)
    solve();
  ioi
}


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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x)*1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)

#define dbg(x) cerr << (#x) << " --> " << (x) << nl;
#define Kazakhstan ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

#define Toktama ""

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
// typedef tree < pair <int, int>, null_type, less < pair <int, int> >,
// rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 2000 + 7, inf = 1e9 + 7, mod = 1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1},
          dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

int get_int() {
  char x = getchar();
  bool mns = 0;
  while (!isdigit(x))
    mns |= x == '-', x = getchar();
  int res = 0;
  while (isdigit(x))
    res = res * 10 + x - '0', x = getchar();
  if (mns)
    res = -res;
  return res;
}
void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
int mult(int x, int y) { return x * 1ll * y % mod; }
int sum(int x, int y) {
  add(x, y);
  return x;
}

int n, m, q;
int f[3][N][N];
char a[N][N];

int sum(int t, int x1, int y1, int x2, int y2) {
  if (y1 > y2)
    return 0;
  if (x1 > x2)
    return 0;
  return f[t][x2][y2] - f[t][x1 - 1][y2] - f[t][x2][y1 - 1] +
         f[t][x1 - 1][y1 - 1];
}
void solve() {
  cin >> n >> m >> q;
  rep(i, 1, n) {
    rep(j, 1, m) {
      cin >> a[i][j];
      if (a[i][j] == '1') {
        f[0][i][j] = 1;
        if (a[i][j - 1] == '1')
          f[1][i][j - 1] = 1;
        if (a[i - 1][j] == '1')
          f[2][i - 1][j] = 1;
      }
    }
  }
  rep(t, 0, 2) {
    rep(i, 1, n) rep(j, 1, m) f[t][i][j] +=
        f[t][i - 1][j] + f[t][i][j - 1] - f[t][i - 1][j - 1];
  }
  rep(i, 1, q) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int cnt = sum(0, x1, y1, x2, y2) - sum(1, x1, y1, x2, y2 - 1) -
              sum(2, x1, y1, x2 - 1, y2);
    cout << cnt << nl;
  }
}
int main() {
#ifndef Toktama
  freopen(Toktama ".in", "r", stdin);
  freopen(Toktama ".out", "w", stdout);
#endif
  Kazakhstan int t = 1;
  while (t--)
    solve();
  ioi
}