Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) >> 1)
#define all(x) x.begin(), x.end()
#define dbgs(x) cerr << (#x) << " --> " << (x) << " "
#define dbg(x)                                                                 \
  cerr << (#x) << " --> " << (x) << endl;                                      \
  getchar()
#define pw(x) (1 << (x))
#define inf 2005000
#define MOD 1000000007
#define N 2005
#define M 1000005
#define LOG 20
#define KOK 4000000
using namespace std;

int n, m, q, a, b, c, d;
char s[N][N];
int pren[N][N], pred1[N][N], pred2[N][N];

int main() {

  // freopen("input.txt","r",stdin);

  scanf("%d %d %d", &n, &m, &q);

  for (int i = 1; i <= n; i++) {

    scanf("%s", s[i] + 1);
  }

  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= m; j++) {

      pren[i][j] = pren[i][j - 1] + pren[i - 1][j] - pren[i - 1][j - 1] +
                   (s[i][j] == '1');
      pred1[i][j] = pred1[i][j - 1] + pred1[i - 1][j] - pred1[i - 1][j - 1] +
                    (s[i][j] == '1' && s[i - 1][j] == '1');
      pred2[i][j] = pred2[i][j - 1] + pred2[i - 1][j] - pred2[i - 1][j - 1] +
                    (s[i][j] == '1' && s[i][j - 1] == '1');
    }
  }

  for (int i = 1; i <= q; i++) {

    scanf("%d %d %d %d", &a, &b, &c, &d);

    int ted = pred2[c][d] - pred2[a - 1][d] - pred2[c][b] + pred2[a - 1][b] +
              pred1[c][d] - pred1[a][d] - pred1[c][b - 1] + pred1[a][b - 1];
    int tn = pren[c][d] - pren[a - 1][d] - pren[c][b - 1] + pren[a - 1][b - 1];

    //		printf("ted %d tn %d\n",ted,tn);

    assert(ted <= tn - 1);

    printf("%d\n", tn - ted);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) >> 1)
#define all(x) x.begin(), x.end()
#define dbgs(x) cerr << (#x) << " --> " << (x) << " "
#define dbg(x)                                                                 \
  cerr << (#x) << " --> " << (x) << endl;                                      \
  getchar()
#define pw(x) (1 << (x))
#define inf 2005000
#define MOD 1000000007
#define N 2005
#define M 1000005
#define LOG 20
#define KOK 4000000
using namespace std;

int n, m, q, a, b, c, d;
char s[N][N];
int pren[N][N], pred1[N][N], pred2[N][N];

int main() {

  // freopen("input.txt","r",stdin);

  scanf("%d %d %d", &n, &m, &q);

  for (int i = 1; i <= n; i++) {

    scanf("%s", s[i] + 1);
  }

  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= m; j++) {

      pren[i][j] = pren[i][j - 1] + pren[i - 1][j] - pren[i - 1][j - 1] +
                   (s[i][j] == '1');
      pred1[i][j] = pred1[i][j - 1] + pred1[i - 1][j] - pred1[i - 1][j - 1] +
                    (s[i][j] == '1' && s[i - 1][j] == '1');
      pred2[i][j] = pred2[i][j - 1] + pred2[i - 1][j] - pred2[i - 1][j - 1] +
                    (s[i][j] == '1' && s[i][j - 1] == '1');
    }
  }

  for (int i = 1; i <= q; i++) {

    scanf("%d %d %d %d", &a, &b, &c, &d);

    int ted = pred2[c][d] - pred2[a - 1][d] - pred2[c][b] + pred2[a - 1][b] +
              pred1[c][d] - pred1[a][d] - pred1[c][b - 1] + pred1[a][b - 1];
    int tn = pren[c][d] - pren[a - 1][d] - pren[c][b - 1] + pren[a - 1][b - 1];

    //		printf("ted %d tn %d\n",ted,tn);

    // assert(ted<=tn-1);

    printf("%d\n", tn - ted);
  }
}