Problem p03707 (C++)

Buggy submission (status: Time Limit Exceeded):
/// supercalifragilisticexpialidocious.
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define f first
/*#define s second*/
#define PB pop_back
#define pb push_back
#define mp make_pair
#define int long long
#define sz(s) (int)s.size()
#define seper(n) setprecision(n)
#define all(v) v.begin(), v.end()
#define mem(a, b) memset(a, b, sizeof a)
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<string, int> msi;
typedef pair<int, string> pis;
typedef pair<int, pair<int, int>> piii;
const int MAXN = 2e3 + 10;
int n, m, q, ans, x, y, x2, y2, t[MAXN][MAXN], p[MAXN][MAXN];
char s[MAXN][MAXN];
int32_t main() {
  IOS cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    t[i][0] = p[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      t[i][j] = t[i][j - 1], p[i][j] += p[i][j - 1];
      if (s[j - 1][i] == '1') {
        if (j - 2 >= 0) {
          if (s[j - 2][i] == '0')
            t[i][j]++;
        } else
          t[i][j]++;
        if (i and s[j - 1][i - 1] == '1')
          p[i][j]++;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> y >> x2 >> y2, ans = 0;
    for (int j = y - 1; j <= y2 - 1; j++) {
      ans += t[j][x2] - t[j][x - 1];
      if (x - 2 >= 0 and s[x - 2][j] == '1' and s[x - 1][j] == '1')
        ans++;
      if (j and j - (y - 1))
        ans -= p[j][x2] - p[j][x - 1];
    }
    cout << ans << '\n';
  }
  return cout << '\n', 0;
}


Fixed submission:
/// supercalifragilisticexpialidocious.
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define f first
/*#define s second*/
#define PB pop_back
#define pb push_back
#define mp make_pair
/*#define int long long*/
#define sz(s) (int)s.size()
#define seper(n) setprecision(n)
#define all(v) v.begin(), v.end()
#define mem(a, b) memset(a, b, sizeof a)
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<string, int> msi;
typedef pair<int, string> pis;
typedef pair<int, pair<int, int>> piii;
const int MAXN = 2e3 + 10;
int n, m, q, ans, x, y, x2, y2, t[MAXN][MAXN], p[MAXN][MAXN];
char s[MAXN][MAXN];
int32_t main() {
  IOS cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    t[i][0] = p[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      t[i][j] = t[i][j - 1], p[i][j] += p[i][j - 1];
      if (s[j - 1][i] == '1') {
        if (j - 2 >= 0) {
          if (s[j - 2][i] == '0')
            t[i][j]++;
        } else
          t[i][j]++;
        if (i and s[j - 1][i - 1] == '1')
          p[i][j]++;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> y >> x2 >> y2, ans = 0;
    for (int j = y - 1; j <= y2 - 1; j++) {
      ans += t[j][x2] - t[j][x - 1];
      if (x - 2 >= 0 and s[x - 2][j] == '1' and s[x - 1][j] == '1')
        ans++;
      if (j and j - (y - 1))
        ans -= p[j][x2] - p[j][x - 1];
    }
    cout << ans << '\n';
  }
  return cout << '\n', 0;
}