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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vi h(n);
    REP(i, n) { cin >> h[i]; }
    vi w(m);
    REP(i, m) { cin >> w[i]; }

    vi all_width;
    REP(i, m) {
      int sum = 0;
      FOR(j, i, m) {
        sum += w[j];
        all_width.push_back(sum);
      }
    }
    sort(ALL(all_width));

    int ans = 0;
    REP(i, n) {
      int total_height = 0;
      FOR(j, i, n) {
        total_height += h[j];
        vi::iterator lb = lower_bound(ALL(all_width), total_height);
        if (*lb == total_height) {
          ans += upper_bound(ALL(all_width), total_height) - lb;
        }
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vi h(n);
    REP(i, n) { cin >> h[i]; }
    vi w(m);
    REP(i, m) { cin >> w[i]; }
    int ans = 0;
    REP(i, n) {
      REP(j, m) {
        int k = i, l = j;
        int height = h[k], width = w[l];
        while (k < n && l < m) {
          bool down = false, right = false;
          if (height == width) {
            ans++;
            down = true;
            right = true;
          } else if (height < width) {
            down = true;
          } else {
            right = true;
          }
          if (down) {
            k++;
            if (k == n)
              break;
            height += h[k];
          }
          if (right) {
            l++;
            if (l == m)
              break;
            width += w[l];
          }
        }
      }
    }
    cout << ans << endl;
  }
}