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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

    vector<int> h(n + 1), w(m + 1);
    for (int i = 0; i < n; i++)
      cin >> h[i + 1];
    for (int i = 0; i < m; i++)
      cin >> w[i + 1];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];

    vector<int> hs(1500001), ws(1500001);

    for (int i1 = n; i1 >= 0; i1--) {
      for (int j1 = i1 + 1; j1 < n + 1; j1++) {
        hs[h[j1] - h[i1]]++;
      }
    }

    for (int i2 = m; i2 >= 0; i2--) {
      for (int j2 = i2 + 1; j2 < m + 1; j2++) {
        ws[w[j2] - w[i2]]++;
      }
    }

    long long ans = 0;
    for (int i = 1; i <= 1500000; i++) {
      ans += hs[i] * ws[i];
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

    vector<int> h(n + 1), w(m + 1);
    for (int i = 0; i < n; i++)
      cin >> h[i + 1];
    for (int i = 0; i < m; i++)
      cin >> w[i + 1];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];

    int cnt = 0;
    for (int i1 = n; i1 >= 0; i1--) {
      for (int j1 = i1 + 1; j1 < n + 1; j1++) {
        for (int i2 = m; i2 >= 0; i2--) {
          for (int j2 = i2 + 1; j2 < m + 1; j2++) {
            if (h[j1] - h[i1] == w[j2] - w[i2]) {
              cnt++;
            } else if (h[j1] - h[i1] < w[j2] - w[i2])
              break;
          }
        }
      }
    }

    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int main() {
  int n, m;
  int h[2000];
  int w[2000];
  while (cin >> n >> m, n) {
    REP(i, n) cin >> h[i];
    REP(i, m) cin >> w[i];
    map<int, int> ari;
    int sh[2000];
    int sw[2000];
    sh[0] = sw[0] = 0;
    FOR(i, 1, n + 1) sh[i] = sh[i - 1] + h[i - 1];
    FOR(i, 1, m + 1) sw[i] = sw[i - 1] + w[i - 1];
    REP(i, n) FOR(j, i + 1, n + 1) {
      int t = sh[j] - sh[i];
      if (ari.find(t) == ari.end())
        ari[t] = 1;
      else
        ari[t]++;
    }
    int cnt = 0;
    REP(i, m) FOR(j, i + 1, m + 1) { cnt += ari[sw[j] - sw[i]]; }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int main() {
  int n, m;
  int h[2000];
  int w[2000];
  while (cin >> n >> m, n) {
    REP(i, n) cin >> h[i];
    REP(i, m) cin >> w[i];
    multiset<int> ari;
    int sh[2000];
    int sw[2000];
    sh[0] = sw[0] = 0;
    FOR(i, 1, n + 1) sh[i] = sh[i - 1] + h[i - 1];
    FOR(i, 1, m + 1) sw[i] = sw[i - 1] + w[i - 1];
    REP(i, n) FOR(j, i + 1, n + 1) { ari.insert(sh[j] - sh[i]); }
    int cnt = 0;
    REP(i, m) FOR(j, i + 1, m + 1) { cnt += ari.count(sw[j] - sw[i]); }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int M = 1500;
ll h[M + 5], w[M + 5];

void solve(int H, int W) {
  ZERO(h);
  ZERO(w);
  FOR(i, 1, H + 1) scanf("%lld", &h[i]);
  FOR(i, 1, W + 1) scanf("%lld", &w[i]);

  rep(i, H) h[i + 1] += h[i];
  rep(i, W) w[i + 1] += w[i];

  map<ll, ll> nums;
  FOR(i, 1, W + 1) rep(j, i) nums[w[i] - w[j]]++;

  ll ans = 0;
  FOR(i, 1, H + 1) {
    rep(j, i) {
      ll height = h[i] - h[j];
      ans += nums[height];
    }
  }

  cout << ans << endl;
}

int main() {
  int H, W;
  while (cin >> H >> W, H)
    solve(H, W);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int M = 1500;
ll h[M + 5], w[M + 5];

void solve(int H, int W) {
  ZERO(h);
  ZERO(w);
  FOR(i, 1, H + 1) scanf("%lld", &h[i]);
  FOR(i, 1, W + 1) scanf("%lld", &w[i]);

  rep(i, H) h[i + 1] += h[i];
  rep(i, W) w[i + 1] += w[i];

  vector<ll> widths;
  FOR(i, 1, W + 1) rep(j, i) widths.push_back(w[i] - w[j]);

  ll ans = 0;
  FOR(i, 1, H + 1) {
    rep(j, i) {
      ll height = h[i] - h[j];
      ans += count(ALL(widths), height);
    }
  }

  cout << ans << endl;
}

int main() {
  int H, W;
  while (cin >> H >> W, H)
    solve(H, W);
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *  Square Route (Problem D, Practice Contest for Japan Domestic, 2007-06-24)
 *  by Kenji Inoue, 2007-06-28
 **/

#include <iostream>
#include <map>

using namespace std;

int main() {
  int h[1501], w[1501];

  int N, M;
  while (cin >> N >> M, (N || M)) {
    for (int i = 0; i < N; i++) {
      int t;
      cin >> t;
      h[i] = t;
    }
    for (int i = 0; i < M; i++) {
      int t;
      cin >> t;
      w[i] = t;
    }

    map<int, int> V, H;
    for (int i = 0; i < N; i++) {
      int len = 0;
      for (int j = i; j < N; j++) {
        len += h[j];
        V[len]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int len = 0;
      for (int j = i; j < M; j++) {
        len += w[j];
        H[len]++;
      }
    }

    int count = 0;
    for (map<int, int>::iterator it = V.begin(); it != V.end(); it++) {
      int key = it->first;
      count += V[key] * H[key];
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  int tate[1501], yoko[1501], n, m, h, w, i, j, k, max, s, e;
  tate[0] = yoko[0] = 0;
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;

    for (i = 1; i <= n; i++) {
      scanf("%d", &h);
      tate[i] = tate[i - 1] + h;
    }
    for (i = 1; i <= m; i++) {
      scanf("%d", &h);
      yoko[i] = yoko[i - 1] + h;
    }

    max = 0;

    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        e = i + 1;
        s = j + 1;
        //				printf("%d %d\\n",e,s);
        while (e <= n && s <= m) {
          if (tate[e] - tate[i] == yoko[s] - yoko[j]) {
            ++max;
            ++s;
            ++e;
          }
          if (tate[e] - tate[i] > yoko[s] - yoko[j])
            ++s;
          if (tate[e] - tate[i] < yoko[s] - yoko[j])
            ++e;
        }
      }
    }
    printf("%d\\n", max);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int h[1500], w[1500];
  int y[1500000], x[1500000];
  int tmp;

  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    int max;
    if (n > m)
      max = n;
    else
      max = m;

    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];

    for (int i = 0; i < max * 1000; i++) {
      y[i] = 0;
      x[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      tmp = 0;
      for (int j = i; j < n; j++) {
        tmp += h[j];
        y[tmp]++;
      }
    }

    for (int i = 0; i < m; i++) {
      tmp = 0;
      for (int j = i; j < m; j++) {
        tmp += w[j];
        x[tmp]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < max * 1000; i++)
      ans += y[i] * x[i];

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int h[1500], w[1500];

  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];

    int ans = 0;

    for (int i = 0; i < n; i++) {
      int y = 0;
      for (int j = i; j < n; j++) {
        y += h[j];
        int s = 0;
        int g = 0;
        int x = w[s];
        while (s < m) {
          if (y == x) {
            ans++;
            x -= w[s];
            if (g < m - 1) {
              g++;
              x += w[g];
            }
            s++;
          } else if (y > x) {
            if (g < m - 1) {
              g++;
              x += w[g];
            } else {
              s = m;
            }
          } else {
            x -= w[s];
            if (s == g && g < m - 1) {
              g++;
              x += w[g];
            }
            s++;
          }
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int X[1500001] = {}, Y[1500001] = {};

int main() {

  int w, h;

  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;

    int ans = 0;
    int x[1501] = {}, y[1501] = {};
    for (int i = 0; i < h; i++)
      cin >> y[i];
    for (int i = 0; i < w; i++)
      cin >> x[i];

    for (int i = 0; i < 1500001; i++) {
      X[i] = 0;
      Y[i] = 0;
    }

    for (int i = 0; i < w; i++) {
      int sum = 0;
      for (int j = i; j < w; j++) {
        sum += x[j];
        X[sum]++;
      }
    }
    for (int i = 0; i < h; i++) {
      int sum = 0;
      for (int j = i; j < h; j++) {
        sum += y[j];
        Y[sum]++;
      }
    }

    for (int i = 0; i < 1500001; i++) {
      ans += X[i] * Y[i];
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;

int main() {

  int w, h;
  int x[1502] = {}, y[1502] = {};

  while (cin >> w >> h, w) {
    for (int i = 1; i <= h; i++) {
      cin >> y[i];
      y[i] += y[i - 1];
    }
    for (int i = 1; i <= w; i++) {
      cin >> x[i];
      x[i] += x[i - 1];
    }

    map<int, int> data;
    map<int, int> data2;
    int ans = 0;
    for (int i = 1; i <= h; i++) {
      for (int i2 = i; i2 <= h; i2++) {
        int cnt = 0;
        if (data[y[i2] - y[i - 1]] != 1) {
          data[y[i2] - y[i - 1]] = 1;
          for (int j = 1; j <= w; j++) {
            for (int j2 = j; j2 <= w; j2++) {
              if (y[i2] - y[i - 1] < x[j2] - x[j - 1])
                break;
              if (y[i2] - y[i - 1] == x[j2] - x[j - 1])
                cnt++;
            }
          }
          data2[y[i2] - y[i - 1]] = cnt;
        }
        ans += data2[y[i2] - y[i - 1]];
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N, M;
  vector<int> w, h;
  vector<int> wsum, hsum;
  while (cin >> N >> M, N | M) {
    map<int, int> hm, wm;
    h.resize(N);
    hsum.resize(N + 1);
    w.resize(M);
    wsum.resize(M + 1);
    hsum[0] = wsum[0] = 0;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i != 0) {
        hsum[i + 1] = hsum[i] + h[i];
      } else {
        hsum[i + 1] = h[i];
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i != 0) {
        wsum[i + 1] = wsum[i] + w[i];
      } else {
        wsum[i + 1] = w[i];
      }
    }

    for (int i = 0; i < M + 1; i++) {
      for (int j = i + 1; j < M + 1; j++) {
        int a = wsum[j] - wsum[i];
        if (wm.find(a) == wm.end()) {
          wm[a] = 1;
        } else {
          wm[a]++;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = i + 1; j < N + 1; j++) {
        int a = hsum[j] - hsum[i];
        if (wm.find(a) != wm.end()) {
          ans += wm[a];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N, M;
  vector<int> w, h;
  vector<int> wsum, hsum;
  while (cin >> N >> M, N | M) {
    h.resize(N);
    hsum.resize(N + 1);
    w.resize(M);
    wsum.resize(M + 1);
    hsum[0] = wsum[0] = 0;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i != 0) {
        hsum[i + 1] = hsum[i] + h[i];
      } else {
        hsum[i + 1] = h[i];
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i != 0) {
        wsum[i + 1] = wsum[i] + w[i];
      } else {
        wsum[i + 1] = w[i];
      }
    }
    /*
       for(int i=0; i < N; i++){
       cout << hsum[i] << " " << endl;
       }
       for(int i=0; i < M; i++){
       cout << wsum[i] << " " << endl;
       }
     */

    // DEBUG
    // vector< pair< pair<int, int>, pair<int, int> > > ans;
    int ans = 0;
    // GUBED
    for (int hend = 1; hend < N + 1; hend++) {
      for (int hbegin = 0; hbegin < hend; hbegin++) {
        for (int wend = 1; wend < M + 1; wend++) {
          for (int wbegin = 0; wbegin < wend; wbegin++) {
            if (hsum[hend] - hsum[hbegin] == wsum[wend] - wsum[wbegin]) {
              // ans.push_back(make_pair(make_pair(hbegin, hend),
              // make_pair(wbegin, wend)));
              ans++;
            }
          }
        }
      }
    }

    // cout << ans.size() << endl;
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int hc[1125750], wc[1125750];
main() {
  int n, m;
  while (cin >> n >> m, n) {
    int h[1501] = {}, w[1501] = {};
    for (int i = 1; i <= n; i++)
      cin >> h[i];
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= m; i++)
      cin >> w[i];
    for (int i = 1; i <= m; i++)
      w[i] += w[i - 1];
    int c = 0;
    for (int i = 0; i <= n; i++)
      for (int j = i + 1; j <= n; j++)
        hc[c++] = h[j] - h[i];
    int d = 0;
    for (int i = 0; i <= m; i++)
      for (int j = i + 1; j <= m; j++)
        wc[d++] = w[j] - w[i];
    int cnt = 0;
    sort(hc, hc + c);
    sort(wc, wc + d);
    for (int i = 0; i < c; i++) {
      cnt += upper_bound(wc, wc + d, hc[i]) - lower_bound(wc, wc + d, hc[i]);
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int h[1500], w[1500];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    vector<int> sumh, sumw;
    int tmp[1500];
    tmp[0] = h[0];
    for (int i = 1; i < n; i++)
      tmp[i] = tmp[i - 1] + h[i];
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        sumh.push_back(tmp[j] - (i ? tmp[i - 1] : 0));
      }
    }
    tmp[0] = w[0];
    for (int i = 1; i < m; i++)
      tmp[i] = tmp[i - 1] + w[i];
    for (int i = 0; i < m; i++) {
      for (int j = i; j < m; j++) {
        sumw.push_back(tmp[j] - (i ? tmp[i - 1] : 0));
      }
    }
    sort(sumh.begin(), sumh.end());
    sort(sumw.begin(), sumw.end());
    int c = 0;
    int now = 0;
    int sto;
    for (int i = 0; i < sumh.size() && now < sumw.size(); i++) {
      while (sumh[i] > sumw[now])
        now++;
      sto = now;
      while (sumh[i] == sumw[now])
        now++, c++;
      now = sto;
    }
    cout << c << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
#define ll long long
using namespace std;

ll dph[1500 * 1000 + 1];
ll dpw[1500 * 1000 + 1];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    vector<ll> H(N + 1, 0), W(M + 1, 0);

    for (int i = 1; i <= 1500 * 1000; i++)
      dph[i] = dpw[i] = 0;

    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    ll ans = 0;

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        dph[H[j] - H[i]]++;
      }
    }

    for (int k = 0; k <= M; k++) {
      for (int l = k + 1; l <= M; l++) {
        dpw[W[l] - W[k]]++;
      }
    }

    ll MIN_HW = min(H[N], W[M]);

    for (int i = 1; i <= MIN_HW; i++) {
      // cout << "i = " << i << endl;
      // cout << "dph = " << dph[i] << " dpw = " << dpw[i] << endl;
      ans += dph[i] * dpw[i];
    }

    /* ????¬? TLE
    for (int i = 0;i <= N;i++) {
            for(int j = i + 1; j <= N;j++){

                    for (int k = 0; k <= M;k++) {
                            for (int l = k + 1;l <= M;l++) {
                                    if (H[j] - H[i] == W[l] - W[k])ans++;
                            }
                    }

            }
    }
    */
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    vector<ll> H(N + 1, 0), W(M + 1, 0);
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }

    vector<ll> dph(N * H[N] + 1, 0);
    vector<ll> dpw(M * W[M] + 1, 0);

    ll ans = 0;

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        dph[H[j] - H[i]]++;
      }
    }

    for (int k = 0; k <= M; k++) {
      for (int l = k + 1; l <= M; l++) {
        dpw[W[l] - W[k]]++;
      }
    }

    ll MIN_HW = min(H[N], W[M]);

    for (int i = 1; i <= MIN_HW; i++) {
      // cout << "i = " << i << endl;
      // cout << "dph = " << dph[i] << " dpw = " << dpw[i] << endl;
      ans += dph[i] * dpw[i];
    }

    /* ????¬? TLE
    for (int i = 0;i <= N;i++) {
            for(int j = i + 1; j <= N;j++){

                    for (int k = 0; k <= M;k++) {
                            for (int l = k + 1;l <= M;l++) {
                                    if (H[j] - H[i] == W[l] - W[k])ans++;
                            }
                    }

            }
    }
    */
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1), w(m + 1);
    loop(n, i) cin >> h[i + 1];
    loop(m, i) cin >> w[i + 1];

    loop(n, i) h[i + 1] += h[i];
    loop(m, i) w[i + 1] += w[i];

    vector<int> H, W;
    loop(n + 1, i) loop(n + 1, j) {
      if (i >= j)
        continue;
      H.push_back(h[j] - h[i]);
    }
    loop(m + 1, i) loop(m + 1, j) {
      if (i >= j)
        continue;
      W.push_back(w[j] - w[i]);
    }
    sort(all(W));

    int ans = 0;
    for (int v : H) {
      ans += distance(lower_bound(all(W), v), upper_bound(all(W), v));
    }

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1), w(m + 1);
    loop(n, i) cin >> h[i + 1];
    loop(m, i) cin >> w[i + 1];

    loop(n, i) h[i + 1] += h[i];
    loop(m, i) w[i + 1] += w[i];

    multiset<int> H, W;
    loop(n + 1, i) loop(n + 1, j) {
      if (i >= j)
        continue;
      H.insert(h[j] - h[i]);
    }
    loop(m + 1, i) loop(m + 1, j) {
      if (i >= j)
        continue;
      W.insert(w[j] - w[i]);
    }

    int ans = 0;
    for (int v : H) {
      ans += W.count(v);
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> h, w;
vector<int> Sh, Sw;

void init() {
  h.resize(0);
  w.resize(0);
  Sh.resize(0);
  Sw.resize(0);
}

bool input() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    w.push_back(tmp);
  }
  return true;
}

int ans() {
  int res = 0;
  for (int i = 0; i < h.size(); i++) {
    int tmp = 0;
    for (int j = i; j < h.size(); j++) {
      tmp += h[j];
      Sh.push_back(tmp);
    }
  }
  for (int i = 0; i < w.size(); i++) {
    int tmp = 0;
    for (int j = i; j < w.size(); j++) {
      tmp += w[j];
      Sw.push_back(tmp);
    }
  }

  sort(Sw.begin(), Sw.end());
  sort(Sh.begin(), Sh.end());

  int i = 0, j = 0;

  while (i < Sw.size() && j < Sh.size()) {
    if (Sw[i] < Sh[j]) {
      i++;
      continue;
    } else if (Sw[i] > Sh[j]) {
      j++;
      continue;
    }
    int s_w = 1, s_h = 1;

    while (Sw[i] == Sw[i + 1]) {
      s_w++;
      i++;
    }
    while (Sh[j] == Sh[j + 1]) {
      s_h++;
      j++;
    }
    res += s_w * s_h;
    i++;
    j++;
  }
  return res;
}

int main() {
  while (init(), input()) {
    int a = ans();
    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> h, w;
vector<int> Sh, Sw;

void init() {
  h.resize(0);
  w.resize(0);
  Sh.resize(0);
  Sw.resize(0);
}

bool input() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    w.push_back(tmp);
  }
  return true;
}

int ans() {
  int res = 0;
  for (int i = 1; i < h.size() + 1; i++) {
    for (int j = 0; j < h.size() - i + 1; j++) {
      int tmp = 0;
      for (int k = 0; k < i && j + k < h.size(); k++) {
        tmp += h[j + k];
      }
      Sh.push_back(tmp);
    }
  }
  for (int i = 1; i < w.size() + 1; i++) {
    for (int j = 0; j < w.size() - i + 1; j++) {
      int tmp = 0;
      for (int k = 0; k < i && j + k < w.size(); k++) {
        tmp += w[j + k];
      }
      Sw.push_back(tmp);
    }
  }

  sort(Sw.begin(), Sw.end());
  sort(Sh.begin(), Sh.end());

  int i = 0, j = 0;

  while (i < Sw.size() && j < Sh.size()) {
    if (Sw[i] < Sh[j]) {
      i++;
      continue;
    } else if (Sw[i] > Sh[j]) {
      j++;
      continue;
    }
    int s_w = 1, s_h = 1;

    while (Sw[i] == Sw[i + 1]) {
      s_w++;
      i++;
    }
    while (Sh[j] == Sh[j + 1]) {
      s_h++;
      j++;
    }
    res += s_w * s_h;
    i++;
    j++;
  }
  return res;
}

int main() {
  while (init(), input()) {
    int a = ans();
    cout << a << endl;
  }
}