Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  while (cin >> N >> M, N) {
    VI h(N + 1, 0), w(M + 1, 0);
    REP(i, N) {
      int x;
      cin >> x;
      h[i + 1] = h[i] + x;
    }
    REP(i, M) {
      int x;
      cin >> x;
      w[i + 1] = w[i] + x;
    }
    LL ans = 0;
    map<int, int> szx, szy;
    for (int i = 0; i <= M; ++i)
      for (int j = 0; j < i; ++j)
        szx[w[i] - w[j]]++;

    for (int i = 0; i <= N; ++i)
      for (int j = 0; j < i; ++j) {
        ans += szx[h[i] - h[j]];
      }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  while (cin >> N >> M, N) {
    VI h(N + 1, 0), w(M + 1, 0);
    REP(i, N) {
      int x;
      cin >> x;
      h[i + 1] = h[i] + x;
    }
    REP(i, M) {
      int x;
      cin >> x;
      w[i + 1] = w[i] + x;
    }
    int ans = 0;
    if (N < M) {
      for (int i = 0; i <= N; ++i)
        for (int j = 0; j < i; ++j) {
          int sz = h[i] - h[j], s = 0, e = 0;
          for (; e <= M;) {
            int tmp = w[e] - w[s];
            if (tmp == sz) {
              ++ans;
              ++e, ++s;
            } else if (tmp < sz) {
              ++e;
            } else {
              ++s;
            }
          }
        }
    } else {
      for (int i = 0; i <= M; ++i)
        for (int j = 0; j < i; ++j) {
          int sz = w[i] - w[j], s = 0, e = 0;
          for (; e <= N;) {
            int tmp = h[e] - h[s];
            if (tmp == sz) {
              ++ans;
              ++e, ++s;
            } else if (tmp < sz) {
              ++e;
            } else {
              ++s;
            }
          }
        }
    }
    cout << ans << endl;
  }

  return 0;
}