Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

int N, M;
int h[2048];
int w[2048];

void load() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < (int)N; ++i) {
    scanf("%d", &h[i]);
  }
  for (int i = 0; i < (int)M; ++i) {
    scanf("%d", &w[i]);
  }
}

class Solve_ {
public:
  long operator()() {
    init();
    sub_h();
    sub_w();
    for (int i = 0; i <= N; ++i) {
      for (int j = i + 1; j <= N; ++j) {
        counth[sumh[j] - sumh[i]] += 1;
      }
    }
    for (int i = 0; i <= M; ++i) {
      for (int j = i + 1; j <= M; ++j) {
        countw[sumw[j] - sumw[i]] += 1;
      }
    }
    long res = 0;
    for (int i = 1; i < 2048 * 1024; ++i) {
      res += counth[i] * countw[i];
    }
    return res;
  }
  void init() {
    for (long &I : countw)
      I = 0;
    for (long &I : counth)
      I = 0;
  }
  void sub_h() {
    sumh[0] = 0;
    for (int i = 0; i < N; ++i) {
      sumh[i + 1] = h[i];
    }
    long t = 0;
    for (int i = 0; i <= N; ++i) {
      t += sumh[i];
      sumh[i] = t;
    }
  }
  void sub_w() {
    sumw[0] = 0;
    for (int i = 0; i < M; ++i) {
      sumw[i + 1] = w[i];
    }
    long t = 0;
    for (int i = 0; i <= M; ++i) {
      t += sumw[i];
      sumw[i] = t;
    }
  }

private:
  long countw[2048 * 1024];
  long counth[2048 * 1024];
  long sumw[2048];
  long sumh[2048];
};
Solve_ Solve;

int main() {
  for (;;) {
    load();
    if (N == 0 and M == 0)
      break;
    long res = Solve();
    printf("%ld\\n", res);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

int N, M;
int h[2048];
int w[2048];

void load() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < (int)N; ++i) {
    scanf("%d", &h[i]);
  }
  for (int i = 0; i < (int)M; ++i) {
    scanf("%d", &w[i]);
  }
}

class Solve_ {
public:
  long operator()() {
    init();
    sub_h();
    sub_w();
    for (int i = 0; i <= N; ++i) {
      for (int j = i + 1; j <= N; ++j) {
        counth[sumh[j] - sumh[i]] += 1;
      }
    }
    for (int i = 0; i <= M; ++i) {
      for (int j = i + 1; j <= M; ++j) {
        countw[sumw[j] - sumw[i]] += 1;
      }
    }
    long res = 0;
    for (int i = 1; i < 2048; ++i) {
      res += counth[i] * countw[i];
    }
    return res;
  }
  void init() {
    for (long &I : countw)
      I = 0;
    for (long &I : counth)
      I = 0;
  }
  void sub_h() {
    sumh[0] = 0;
    for (int i = 0; i < N; ++i) {
      sumh[i + 1] = h[i];
    }
    long t = 0;
    for (int i = 0; i <= N; ++i) {
      t += sumh[i];
      sumh[i] = t;
    }
  }
  void sub_w() {
    sumw[0] = 0;
    for (int i = 0; i < M; ++i) {
      sumw[i + 1] = w[i];
    }
    long t = 0;
    for (int i = 0; i <= M; ++i) {
      t += sumw[i];
      sumw[i] = t;
    }
  }

private:
  long countw[2048];
  long counth[2048];
  long sumw[2048];
  long sumh[2048];
};
Solve_ Solve;

int main() {
  for (;;) {
    load();
    if (N == 0 and M == 0)
      break;
    long res = Solve();
    printf("%ld\\n", res);
  }

  return 0;
}