alexjercan/bugnet huggingface dataset:
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m) {
  int h[2000], w[2000];

  rep(i, n) { cin >> h[i]; }
  rep(i, m) { cin >> w[i]; }

  map<int, int> a, b;

  rep(i, n) {
    int sum = 0;
    FOR(j, i, n) {
      sum += h[j];
      a[sum]++;
    }
  }
  rep(i, m) {
    int sum = 0;
    FOR(j, i, m) {
      sum += w[j];
      b[sum]++;
    }
  }

  int ans = 0;

  for (auto itr = a.begin(); itr != a.end(); itr++) {
    ans += itr->second * b[itr->first];
  }

  cout << ans << endl;
}

int main() {
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;

    search(n, m);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m) {
  int h[2000], w[2000];

  rep(i, n) { cin >> h[i]; }
  rep(i, m) { cin >> w[i]; }

  map<int, int> a, b;

  rep(i, n) {
    int sum = 0;
    FOR(j, i, n) {
      sum += h[j];
      a[sum]++;
    }
  }
  rep(i, m) {
    int sum = 0;
    FOR(j, i, m) {
      sum += w[j];
      b[sum]++;
    }
  }

  int ans = 0;

  rep(i, 1e6) { ans += a[i] * b[i]; }

  cout << ans << endl;
}

int main() {
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;

    search(n, m);
  }

  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n && n) {
    int m;
    cin >> m;
    int h[n], w[m];
    int p[n + 1],
        q[m + 1]; // h[0]+...+h[i]??????????????????????¨????????????????
    p[0] = 0;
    q[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      p[i + 1] = p[i] + h[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> w[j];
      q[j + 1] = q[j] + w[j];
    }
    int t = 0;
    int x[(n + 1) * (m + 1) + 1];
    x[(n + 1) * (m + 1)] = 0;
    for (int k = 0; k < n + 1; k++) {
      for (int l = 0; l < m + 1; l++) {
        x[t] = p[k] + q[l];
        t = t + 1;
      }
    }
    sort(x, x + (n + 1) * (m + 1));
    int c = 1;
    int d = 0;
    for (int a = 1; a < (n + 1) * (m + 1) + 1; a++) {
      if (x[a] == x[a - 1]) {
        c = c + 1;
      } else {
        d = d + c * (c - 1) / 2;
        c = 1;
      }
    }
    cout << d << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n && n) {
    int m;
    cin >> m;
    int h[n], w[m];
    int p[n + 1],
        q[m + 1]; // h[0]+...+h[i]??????????????????????¨????????????????
    p[0] = 0;
    q[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      p[i + 1] = p[i] + h[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> w[j];
      q[j + 1] = q[j] + w[j];
    }
    int t = 0;
    for (int a = 0; a < n; a++) {
      for (int b = a + 1; b < n + 1; b++) {
        for (int c = 0; c < m; c++) {
          for (int d = d + 1; d < m + 1; d++) {
            if (p[a] - p[b] == q[c] - q[d]) {
              t = t + 1;
            }
          }
        }
      }
    }
    cout << t << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
#define MOD 1000000007
using namespace std;
signed main() {
  int N, M;
  while (cin >> N >> M, N + M) {
    vector<int> h(N), w(M);
    int totalH = 0, totalW = 0;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      totalH += h[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      totalW += w[i];
    }

    vector<int> height(totalH + 1, 0), width(totalW + 1, 0);
    for (int i = 0; i < N; i++) {
      int k = h[i];
      height[k]++;
      for (int j = i + 1; j < N; j++) {
        k += h[j];
        height[k]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int k = w[i];
      width[k]++;
      for (int j = i + 1; j < M; j++) {
        k += w[j];
        width[k]++;
      }
    }

    int ans = 0;
    for (int i = 1; i <= min(totalH, totalW); i++) {
      ans += height[i] * width[i];
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while (cin >> N >> M, N + M != 0) {
    vector<int> h(N), w(M);
    for (int i = 0; i < N; i++)
      cin >> h[i];
    for (int i = 0; i < M; i++)
      cin >> w[i];

    // 累積和チャレンジ
    vector<int> sum_h(N + 1, 0), sum_w(M + 1, 0);
    for (int i = 0; i < N; i++) {
      sum_h[i + 1] = sum_h[i] + h[i];
    }
    for (int i = 0; i < M; i++) {
      sum_w[i + 1] = sum_w[i] + w[i];
    }

    // N<=1500,M<=1500 ってこれ計算量やばい
    // 累積和ー＞全探索むりそう
    // 堂々たるTLE
    int ans = 0;
    for (int i = 0; i < N; i++)
      for (int j = i + 1; j < N + 1; j++)
        for (int k = 0; k < M; k++)
          for (int l = k + 1; l < M + 1; l++)
            if (sum_h[j] - sum_h[i] == sum_w[l] - sum_w[k])
              ans++;
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <map>
#include <stdio.h>
#include <vector>

auto solve(int N, int M) -> int {
  using namespace std;
  vector<int> h(N);
  vector<int> w(M);

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

  map<int, int> eh;
  map<int, int> ew;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i; j < N; j++) {
      sum += h[j];
      if (eh.find(sum) != ew.end())
        eh[sum]++;
      else
        eh[sum] = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i; j < M; j++) {
      sum += w[j];
      if (ew.find(sum) != ew.end())
        ew[sum]++;
      else
        ew[sum] = 1;
    }
  }

  int ans = 0;
  for (auto p : eh) {
    if (ew.find(p.first) != ew.end())
      ans += p.second * ew[p.first];
  }

  return ans;
}

auto main() -> int {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;
    printf("%d\\n", solve(n, m));
  }
}

Error: MLE

Fixed submission:

#include <map>
#include <stdio.h>
#include <vector>

auto solve(int N, int M) -> int {
  using namespace std;
  vector<int> h(N);
  vector<int> w(M);

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

  map<int, int> &eh = *(new map<int, int>());
  map<int, int> &ew = *(new map<int, int>());
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i; j < N; j++) {
      sum += h[j];
      if (eh.find(sum) != ew.end())
        eh[sum]++;
      else
        eh[sum] = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i; j < M; j++) {
      sum += w[j];
      if (ew.find(sum) != ew.end())
        ew[sum]++;
      else
        ew[sum] = 1;
    }
  }

  int ans = 0;
  for (auto p : eh) {
    if (ew.find(p.first) != ew.end())
      ans += p.second * ew[p.first];
  }

  return ans;
}

auto main() -> int {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;
    printf("%d\\n", solve(n, m));
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 1600;

int h, w;
int A[N];
int B[N];

void init() {}

bool input() {
  cin >> h >> w;
  if (h == 0)
    return 0;
  rep(i, h) cin >> A[i];
  rep(i, w) cin >> B[i];
  return true;
}

int solve() {
  int C[N] = {0};
  int D[N] = {0};
  rep(i, h) { C[i + 1] = C[i] + A[i]; }
  rep(i, w) { D[i + 1] = D[i] + B[i]; }

  map<int, int> sum;
  rep(i, h) {
    reps(j, i, h) {
      int s = C[j + 1] - C[i];
      sum[s]++;
      // printf("s %d  %d\\n",s,sum[s]);
    }
  }

  int ans = 0;
  rep(i, w) {
    reps(j, i, w) {
      int s = D[j + 1] - D[i];
      ans += sum[s];
      // printf("g %d  %d\\n",s,sum[s]);
    }
  }

  return ans;
}
int main() {
  while (init(), input())
    printf("%d\\n", solve());
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 100;

int h, w;
int A[N];
int B[N];

void init() {}

bool input() {
  cin >> h >> w;
  if (h == 0)
    return 0;
  rep(i, h) cin >> A[i];
  rep(i, w) cin >> B[i];
  return true;
}

int solve() {
  int C[N] = {0};
  int D[N] = {0};
  rep(i, h) { C[i + 1] = C[i] + A[i]; }
  rep(i, w) { D[i + 1] = D[i] + B[i]; }

  map<int, int> sum;
  rep(i, h) {
    reps(j, i, h) {
      int s = C[j + 1] - C[i];
      sum[s]++;
      // printf("s %d  %d\\n",s,sum[s]);
    }
  }

  int ans = 0;
  rep(i, w) {
    reps(j, i, w) {
      int s = D[j + 1] - D[i];
      ans += sum[s];
      // printf("g %d  %d\\n",s,sum[s]);
    }
  }

  return ans;
}
int main() {
  while (init(), input())
    printf("%d\\n", solve());
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>

using namespace std;

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int H[N], W[M];
    for (int i = 0; i < N; i++) {
      cin >> H[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> W[i];
    }

    set<int> v1, v2;
    map<int, int> m1, m2;
    for (int i = 0; i < N; i++) {
      int bfr = 0;
      for (int j = i; j < N; j++) {
        int temp;
        temp = bfr + H[j];
        bfr = temp;
        v1.insert(temp);
        m1[temp]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int bfr = 0;
      for (int j = i; j < M; j++) {
        int temp;
        temp = bfr + W[j];
        bfr = temp;
        v2.insert(temp);
        m2[temp]++;
      }
    }

    int cnt = 0;
    set<int>::iterator it1 = v1.begin();
    while (it1 != v1.end()) {
      set<int>::iterator it2;
      it2 = v2.find(*it1);
      if (it2 != v2.end())
        cnt += m1[*it1] * m2[*it2];
      it1++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>

using namespace std;

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int H[N], W[M];
    for (int i = 0; i < N; i++) {
      cin >> H[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> W[i];
    }

    set<int> v1, v2;
    map<int, int> m1, m2;
    for (int i = 0; i < N; i++) {
      int bfr = 0;
      for (int j = i; j < N; j++) {
        int temp;
        temp = bfr + H[j];
        bfr = temp;
        v1.insert(temp);
        m1[temp]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int bfr = 0;
      for (int j = i; j < M; j++) {
        int temp;
        temp = bfr + W[j];
        bfr = temp;
        v2.insert(temp);
        m2[temp]++;
      }
    }

    int cnt = 0;
    set<int>::iterator it1 = v1.begin();
    while (it1 != v1.end()) {
      set<int>::iterator it2 = v2.begin();
      while (it2 != v2.end()) {
        if (*it1 == *it2)
          cnt += m1[*it1] * m2[*it2];
        it2++;
      }
      it1++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF (1e9 + 7)

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m and n + m) {
    vector<int> nsroad(n + 1);
    vector<int> weroad(m + 1);
    nsroad[0] = 0;
    weroad[0] = 0;
    REP(i, n) {
      int tmp;
      cin >> tmp;
      nsroad[i + 1] = nsroad[i] + tmp;
    }
    REP(i, m) {
      int tmp;
      cin >> tmp;
      weroad[i + 1] = weroad[i] + tmp;
    }
    int res = 0;
    map<int, int> counts;
    REP(i, nsroad.size()) REP(j, i) {
      int count = 0;
      int width = nsroad[i] - nsroad[j];
      if (counts.find(width) != counts.end()) {
        res += counts[width];
        continue;
      }
      int fr = 0;
      int to = 0;
      while (fr < weroad.size()) {
        if (to >= weroad.size())
          break;
        if (weroad[to] - weroad[fr] == width) {
          count++;
          to++;
        }
        if (weroad[to] - weroad[fr] > width)
          fr++;
        if (weroad[to] - weroad[fr] < width)
          to++;
      }
      counts[width] = count;
      res += count;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF (1e9 + 7)

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m and n + m) {
    vector<int> nsroad(n + 1);
    vector<int> weroad(m + 1);
    nsroad[0] = 0;
    weroad[0] = 0;
    REP(i, n) {
      int tmp;
      cin >> tmp;
      nsroad[i + 1] = nsroad[i] + tmp;
    }
    REP(i, m) {
      int tmp;
      cin >> tmp;
      weroad[i + 1] = weroad[i] + tmp;
    }
    int res = 0;
    REP(i, nsroad.size()) REP(j, i) {
      int width = nsroad[i] - nsroad[j];
      int fr = 0;
      int to = 0;
      while (fr < weroad.size()) {
        if (to >= weroad.size())
          break;
        if (weroad[to] - weroad[fr] == width) {
          res++;
          to++;
        }
        if (weroad[to] - weroad[fr] > width)
          fr++;
        if (weroad[to] - weroad[fr] < width)
          to++;
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main(void) {
  int i, j;
  int n, m;
  while (cin >> n >> m, n + m) {
    vi h(n + 1), w(m + 1);
    h[0] = w[0] = 0;
    rep(i, n) cin >> h[i + 1];
    rep(i, m) cin >> w[i + 1];
    rep(i, n) h[i + 1] += h[i];
    rep(i, m) w[i + 1] += w[i];
    vi v1, v2;
    rep(i, n) loop(j, i + 1, n + 1) v1.push_back(h[j] - h[i]);
    rep(i, m) loop(j, i + 1, m + 1) v2.push_back(w[j] - w[i]);
    /*
            rep(i,v1.size()) cout << v1[i] << endl;
            cout << endl;
            rep(i,v2.size()) cout << v2[i] << endl;
    */
    sort(all(v1));
    sort(all(v2));
    int ans = 0;
    rep(i, v1.size()) {
      int t = v1[i];
      ans += upper_bound(all(v2), t) - lower_bound(all(v2), t);
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

#define ll long long int

using namespace std;
typedef pair<int, int> P;

string str;

int main() {
  int n, m;
  int h[1505];
  int w[1505];
  int i, j, k, l;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int sum = 0;
    for (i = 0; i < n; i++)
      cin >> h[i];
    for (i = 0; i < m; i++)
      cin >> w[i];

    for (i = 0; i < n; i++) {
      for (j = i + 1; j < n + 1; j++) {
        for (k = 0; k < m; k++) {
          for (l = k + 1; l < m + 1; l++) {
            int tate = 0, yoko = 0;
            for (int x = i; x < j; x++)
              tate += h[x];
            for (int x = k; x < l; x++)
              yoko += w[x];
            if (tate == yoko)
              sum++;
          }
        }
      }
    }
    cout << sum << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); i++)
#define DUMMY 0

using namespace std;

void make_map(vector<long> &sum, map<long, int> &map) {
  for (int i = 1; i < (int)sum.size(); i++) {
    for (int j = 0; i + j < (int)sum.size(); j++) {
      if (map.count(sum[i + j] - sum[j]) == 0) {
        map.insert(make_pair(sum[i + j] - sum[j], 1));
      } else {
        map[sum[i + j] - sum[j]]++;
      }
    }
  }
  return;
}

int main(void) {
  int N, M;
  while (cin >> N >> M, N > 0) {
    map<long, int> h_map;
    map<long, int> w_map;
    vector<long> h_sum;
    vector<long> w_sum;
    long result = 0;
    // hの累積和配列を作成
    h_sum.push_back(0);
    rep(i, N) {
      int h;
      cin >> h;
      h_sum.push_back(h_sum.back() + h);
    }
    // h_mapの構成
    make_map(h_sum, h_map);
    // wの累積和配列を作成
    w_sum.push_back(0);
    rep(i, M) {
      int w;
      cin >> w;
      w_sum.push_back(w_sum.back() + w);
    }
    // w_mapの構成
    make_map(w_sum, w_map);

    // h_mapのイテレータを回して，同じキー値をもつw_mapの要素数だけresultを加算
    for (auto itr = h_map.begin(); itr != h_map.end(); itr++) {
      result += (*itr).second * w_map[(*itr).first];
    }

    // 出力
    cout << result << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); i++)
#define DUMMY 0

using namespace std;

void make_map(vector<long> &sum, unordered_multimap<long, int> &map) {
  for (int i = 1; i < (int)sum.size(); i++) {
    for (int j = 0; i + j < (int)sum.size(); j++) {
      map.insert(make_pair(sum[i + j] - sum[j], DUMMY));
    }
  }
  return;
}

int main(void) {
  int N, M;
  while (cin >> N >> M, N > 0) {
    unordered_multimap<long, int> h_map;
    unordered_multimap<long, int> w_map;
    vector<long> h_sum;
    vector<long> w_sum;
    long result = 0;
    // hの累積和配列を作成
    h_sum.push_back(0);
    rep(i, N) {
      int h;
      cin >> h;
      h_sum.push_back(h_sum.back() + h);
    }
    // h_mapの構成
    make_map(h_sum, h_map);
    // wの累積和配列を作成
    w_sum.push_back(0);
    rep(i, M) {
      int w;
      cin >> w;
      w_sum.push_back(w_sum.back() + w);
    }
    // w_mapの構成
    make_map(w_sum, w_map);

    // h_mapのイテレータを回して，同じキー値をもつw_mapの要素数だけresultを加算
    for (auto itr = h_map.begin(); itr != h_map.end(); itr++) {
      result += w_map.count((*itr).first);
    }

    // 出力
    cout << result << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int w[10000000], h[1500];
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> h[i];
    int wcount = 0;
    for (int start = 0; start < n; start++) {
      int count = 0;
      for (int len = start; len < n; len++) {
        count += h[len];
        w[wcount] = count;
        wcount++;
      }
    }
    sort(w, w + wcount);
    for (int i = 0; i < m; i++)
      cin >> h[i];
    int key, ans = 0;
    for (int start = 0; start < m; start++) {
      int count = 0;
      for (int len = start; len < m; len++) {
        count += h[len];
        key = count;
        int left = 0, right = wcount;
        int mid;
        while (true) {
          if (left >= right)
            break;
          mid = (left + right) / 2;
          if (w[mid] == key) {
            ans++;
            int q = mid - 1;
            while (true) {
              if (q < 0)
                break;
              if (w[q] == w[mid])
                ans++;
              else
                break;
              q--;
            }
            q = mid + 1;
            while (true) {
              if (q == wcount)
                break;
              if (w[q] == w[mid])
                ans++;
              else
                break;
              q++;
            }
            break;
          }
          if (key > w[mid])
            left = mid + 1;
          if (key < w[mid])
            right = mid;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int w[2][8390000], h[1500];
int main() {
  int n[2];
  int numsum = 0;
  while (true) {
    cin >> n[0] >> n[1];
    if (n[0] == 0 && n[1] == 0)
      break;
    for (int f = 0; f < 2; f++) {
      for (int i = 0; i < n[f]; i++)
        cin >> h[i];
      int wcount = 0;
      for (int start = 0; start < n[f]; start++) {
        int count = 0;
        for (int len = start; len < n[f]; len++) {
          count += h[len];
          w[f][wcount] = count;
          wcount++;
        }
      }
      numsum += wcount - 1;
      w[f][wcount] = 10000000;
      sort(w[f], w[f] + wcount);
    }
    int ans = 0;
    int top[2] = {0};
    for (int i = 0; i < numsum; i++) {
      if (w[0][top[0]] < w[1][top[1]])
        top[0]++;
      if (w[0][top[0]] > w[1][top[1]])
        top[1]++;
      if (w[0][top[0]] == w[1][top[1]]) {
        ans++;
        top[0]++;
      }
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using namespace std;

int y[1501], x[1501];
int main() {
  int N, M;
  while (cin >> N >> M && (N > 0 && M > 0)) {
    for (int i = 1, s = 0; i <= N; ++i) {
      int h;
      cin >> h;
      s += h;
      y[i] = s;
    }
    for (int i = 1, s = 0; i <= M; ++i) {
      int w;
      cin >> w;
      s += w;
      x[i] = s;
    }

    map<int, int> hmap, wmap;
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j <= N; ++j) {
        hmap[y[j] - y[i]]++;
      }
    }
    for (int i = 0; i < M; ++i) {
      for (int j = i + 1; j <= M; ++j) {
        wmap[x[j] - x[i]]++;
      }
    }

    long num = 0;
    for (const auto &p : hmap) {
      num += wmap[p.first] * p.second;
    }
    cout << num << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int y[1501], x[1501];
int main() {
  int N, M;
  while (cin >> N >> M && (N > 0 && M > 0)) {
    for (int i = 1, s = 0; i <= N; ++i) {
      int h;
      cin >> h;
      s += h;
      y[i] = s;
    }
    for (int i = 1, s = 0; i <= M; ++i) {
      int w;
      cin >> w;
      s += w;
      x[i] = s;
    }

    long num = 0;
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++j) {
        for (int k = i + 1; k <= N; ++k) {
          for (int l = j + 1; l <= M; ++l) {
            if (x[l] - x[j] == y[k] - y[i])
              num++;
          }
        }
      }
    }
    cout << num << endl;
  }
}