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

////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

//// MACRO ////
#define countof(a) (sizeof(a) / sizeof(a[0]))

#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define pos(c, i) c.being() + (i)
#define allof(c) c.begin(), c.end()
#define aallof(a) a, countof(a)
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
#define apartof(a, i, n) a + (i), a + (i) + (n)
#define long long long

#define EPS 1e-9
#define INF (1L << 30)
#define LINF (1LL << 60)

#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON_T(t) bool (*)(const t &, const t &)
#define COMPARISON(t, a, b, exp)                                               \\
  [&](const t &a, const t &b) -> bool { return exp; }
#define CONVERTER(TSrc, t, TDest, exp)                                         \\
  [&](const TSrc &t) -> TDest { return exp; }

inline int sign(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
inline bool inRange(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}

//// i/o helper ////

struct _Reader {
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  bool f;
  _Writer() : f(false) {}
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? " " : "") << rhs;
    f = true;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  _Reader(), __VA_ARGS__
#define WRITE(...)                                                             \\
  _Writer(), __VA_ARGS__;                                                      \\
  cout << endl

template <class T> struct vevector : public vector<vector<T>> {
  vevector(int n = 0, int m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
  vevevector(int n = 0, int m = 0, int l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
  vevevevector(int n = 0, int m = 0, int l = 0, int k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vevector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, pair<T1, T2> &p) {
  out << p.first << p.second;
  return out;
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &v) {
  ostringstream ss;
  for (auto x : v)
    ss << x << ' ';
  auto s = ss.str();
  out << s.substr(0, s.length() - 1) << endl;
  return out;
}

//// start up ////
void solve();
inline void readfrom(string filename) { freopen(filename.c_str(), "r", stdin); }
inline void writeto(string filename) { freopen(filename.c_str(), "w", stdout); }
int main() {
  solve();
  return 0;
}

////////////////////
/// template end ///
////////////////////

inline int setbit(int n) { return (1 << n) - 1; }
inline bool contains(int x, int min, int max) {
  return x >= min && x < max;
} // max含まない

void solve() {
  int testcases = INF;
  REP(testcase, testcases) {
    READ(int, N, M);
    if (!(N | M)) {
      break;
    }

    auto hs = read<int>(N);
    hs.insert(hs.begin(), 0);
    partial_sum(allof(hs), hs.begin());
    N++;
    auto ws = read<int>(M);
    ws.insert(ws.begin(), 0);
    partial_sum(allof(ws), ws.begin());
    M++;

    auto a = vector<int>();
    REP(i, N) FOR(j, i + 1, N) { a.push_back(hs[j] - hs[i]); }
    auto b = vector<int>();
    REP(i, M) FOR(j, i + 1, M) { b.push_back(ws[j] - ws[i]); }

    sort(allof(a));
    sort(allof(b));

    int i = 0, j = 0, r = 0;
    while (i < a.size() && j < b.size()) {
      int x = i;
      int y = j;
      if (a[i] == b[j]) {
        while (i < a.size() && a[i] == a[x])
          i++;
        while (j < b.size() && b[j] == b[y])
          j++;
        r += (i - x) * (j - y);
      } else if (a[i] < b[j]) {
        while (i < a.size() && a[i] == a[x])
          i++;
      } else {
        while (j < b.size() && b[j] == b[y])
          j++;
      }
    }
    WRITE(r);
  }
}

Error: TLE

Fixed submission:

////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

//// MACRO ////
#define countof(a) (sizeof(a) / sizeof(a[0]))

#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define pos(c, i) c.being() + (i)
#define allof(c) c.begin(), c.end()
#define aallof(a) a, countof(a)
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
#define apartof(a, i, n) a + (i), a + (i) + (n)
#define long long long

#define EPS 1e-9
#define INF (1L << 30)
#define LINF (1LL << 60)

#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON_T(t) bool (*)(const t &, const t &)
#define COMPARISON(t, a, b, exp)                                               \\
  [&](const t &a, const t &b) -> bool { return exp; }
#define CONVERTER(TSrc, t, TDest, exp)                                         \\
  [&](const TSrc &t) -> TDest { return exp; }

inline int sign(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
inline bool inRange(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}

//// i/o helper ////

struct _Reader {
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  bool f;
  _Writer() : f(false) {}
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? " " : "") << rhs;
    f = true;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  _Reader(), __VA_ARGS__
#define WRITE(...)                                                             \\
  _Writer(), __VA_ARGS__;                                                      \\
  cout << endl

template <class T> struct vevector : public vector<vector<T>> {
  vevector(int n = 0, int m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
  vevevector(int n = 0, int m = 0, int l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
  vevevevector(int n = 0, int m = 0, int l = 0, int k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vevector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, pair<T1, T2> &p) {
  out << p.first << p.second;
  return out;
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &v) {
  ostringstream ss;
  for (auto x : v)
    ss << x << ' ';
  auto s = ss.str();
  out << s.substr(0, s.length() - 1) << endl;
  return out;
}

//// start up ////
void solve();
inline void readfrom(string filename) { freopen(filename.c_str(), "r", stdin); }
inline void writeto(string filename) { freopen(filename.c_str(), "w", stdout); }
int main() {
  solve();
  return 0;
}

////////////////////
/// template end ///
////////////////////

inline int setbit(int n) { return (1 << n) - 1; }
inline bool contains(int x, int min, int max) {
  return x >= min && x < max;
} // max含まない

void solve() {
  int testcases = INF;
  REP(testcase, testcases) {
    READ(int, N, M);
    if (!(N | M)) {
      break;
    }

    auto hs = read<int>(N);
    hs.insert(hs.begin(), 0);
    partial_sum(allof(hs), hs.begin());
    N++;
    auto ws = read<int>(M);
    ws.insert(ws.begin(), 0);
    partial_sum(allof(ws), ws.begin());
    M++;

    auto a = vector<int>();
    REP(i, N) FOR(j, i + 1, N) { a.push_back(hs[j] - hs[i]); }
    auto b = vector<int>();
    REP(i, M) FOR(j, i + 1, M) { b.push_back(ws[j] - ws[i]); }

    sort(allof(a));
    sort(allof(b));

    int i = 0, j = 0, r = 0;
    while (i < a.size() && j < b.size()) {
      int x = i;
      int y = j;
      if (a[i] == b[j]) {
        while (i < a.size() && a[i] == a[x])
          x++;
        while (j < b.size() && b[j] == b[y])
          y++;
        r += (i - x) * (j - y);
      } else if (a[i] < b[j]) {
        while (i < a.size() && a[i] == a[x])
          i++;
      } else {
        while (j < b.size() && b[j] == b[y])
          j++;
      }
    }
    WRITE(r);
  }
}