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


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define dump(x) cout << " " << #x << " = " << (x) << endl;
typedef long long ll;
typedef complex<int> P;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dmax = 1500 * 1000 + 10; //
int a[dmax] = {0};                 //

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;

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

  vector<int> W, H;
  for (int i = 0; i < n; i++) {
    int x = w[i];
    W.pb(x);
    for (int j = i + 1; j < n; j++) {
      x += w[j];
      W.pb(x);
    }
  }
  for (int i = 0; i < m; i++) {
    int x = h[i];
    H.pb(x);
    for (int j = i + 1; j < m; j++) {
      x += h[j];
      H.pb(x);
    }
  }

  int ans = 0;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < W.size(); i++) {
    a[W[i]]++;
  }
  for (int j = 0; j < H.size(); j++) {
    ans += a[H[j]];
  }
  cout << ans << endl;

  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define dump(x) cout << " " << #x << " = " << (x) << endl;
typedef long long ll;
typedef complex<int> P;
const double EPS = 1e-10;
const double PI = acos(-1.0);

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;

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

  vector<int> W, H;
  for (int i = 0; i < n; i++) {
    int x = w[i];
    W.pb(x);
    for (int j = i + 1; j < n; j++) {
      x += w[j];
      W.pb(x);
    }
  }
  for (int i = 0; i < m; i++) {
    int x = h[i];
    H.pb(x);
    for (int j = i + 1; j < m; j++) {
      x += h[j];
      H.pb(x);
    }
  }

  int ans = 0;
  for (int i = 0; i < W.size(); i++) {
    for (int j = 0; j < H.size(); j++) {
      if (W[i] == H[j])
        ans++;
    }
  }
  cout << ans << endl;

  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

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

#include <iostream>
#include <map>
using namespace std;
int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    int h[n + 1];
    map<int, int> hash;

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

      if (i != 0) {
        h[i] += h[i - 1];
        hash[h[i]]++;
        //  cerr<<"{SS}"<<hash[h[i]]<<" "<<h[i]<<endl;
        for (int ii = i - 1; ii >= 0; ii--) {
          {
            hash[h[i] - h[ii]]++;
            //        cerr<<"{N}"<<hash[h[i]-h[ii]]<<" "<<h[i]-h[ii]<<endl;
          }
        }
      } else {
        hash[h[i]]++;
        //      cerr<<"{S}"<<hash[h[i]]<<" "<<h[i]<<endl;
      }
    }
    int ans = 0;
    int w[m + 1];

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

      if (i != 0) {
        w[i] += w[i - 1];
        ans += hash[w[i]];
        for (int ii = i - 1; ii >= 0; ii--) {
          ans += hash[w[i] - w[ii]];
        }
      } else {
        ans += hash[w[i]];
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    int h[n];
    int w[m];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // 左上固定の尺取
        int ii = i, jj = j;
        int isum = h[i];
        int jsum = w[j];
        while (ii < n && jj < m) {
          if (isum == jsum) {
            cnt++;
            //                      break;
            ii++;
            jj++;

            if (jj < m)
              jsum += w[j];
            if (ii < n)
              isum += h[j];
          }
          if (isum > jsum) {
            jj++;
            if (jj < m)
              jsum += w[j];
          }
          if (isum < jsum) {
            ii++;
            if (ii < n)
              isum += h[j];
          }
        }
      }
    }

    cout << cnt << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define K 1000000
using namespace std;
int h[K], w[K], n, m, i, j, p[1500], k;
int main() {
  while (1) {
    for (i = 1; i < K; i++)
      h[i] = 0, w[i] = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> p[i];
      for (j = i, k = 0; j >= 0; j--, k += p[j])
        h[p[i] + k]++;
    }
    for (i = 0; i < m; i++) {
      cin >> p[i];
      for (j = i, k = 0; j >= 0; j--, k += p[j])
        w[p[i] + k]++;
    }
    n = 0;
    for (i = 1; i < K; i++)
      n += h[i] * w[i];
    cout << n << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int h[1001] = {}, w[1001] = {}, n, m, i, j, p[1501];
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> p[i], h[p[i]]++;
      for (j = 0; j < i; j++)
        h[p[i] + p[j]]++;
    }
    for (i = 0; i < m; i++) {
      cin >> p[i], w[p[i]]++;
      for (j = 0; j < i; j++)
        w[p[i] + p[j]]++;
    }
    n = 0;
    for (i = 1; i <= 1000; i++)
      n += h[i] * w[i];
    cout << n << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> P;
int main() {
  while (true) {
    int N, M, ans = 0;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int h[N + 1], w[M + 1];
    h[0] = 0;
    w[0] = 0;
    int value;
    for (int i = 1; i <= N; i++) {
      cin >> value;
      h[i] = h[i - 1] + value;
    }
    for (int i = 1; i <= M; i++) {
      cin >> value;
      w[i] = w[i - 1] + value;
    }
    int val[1500000] = {};
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j <= N; j++) {
        val[abs(h[j] - h[i])]++;
      }
    }
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j <= M; j++) {
        if (abs(w[j] - w[i]) != 0)
          ans += val[abs(w[j] - w[i])];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;
int main() {
  while (true) {
    int N, M, ans = 0;
    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];
    }
    for (int sh = 0; sh < N; sh++) {
      for (int sw = 0; sw < M; sw++) {
        int hval = h[sh], wval = w[sw];
        int i = sh, j = sw;
        while (i < N && j < M) {
          if (hval == wval) {
            ans++;
            hval += h[++i];
          } else if (hval < wval) {
            hval += h[++i];
          } else {
            wval += w[++j];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int a, b;
  int i, j, k, l;
  int h_now = 0, w_now = 0;
  int h[1505], w[1505];
  int haba_h[1500000] = {};
  int haba_w[1500000] = {};
  int counter = 0;
  while (1) {
    for (i = 0; i < 1500000; i++) {
      haba_h[i] = 0;
      haba_w[i] = 0;
    }
    counter = 0;
    w_now = 0;
    h_now = 0;
    h[0] = 0;
    w[0] = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    //????????§?¨?
    for (i = 1; i <= n; i++) {
      cin >> a;

      h_now = h_now + a;
      h[i] = h_now;
    }

    // w?????§?¨?
    for (i = 1; i <= m; i++) {
      cin >> b;

      w_now = w_now + b;
      w[i] = w_now;
    }

    //?????????????????????????????????
    for (i = 0; i < n; i++) {
      for (j = i; j <= n; j++) {
        haba_h[h[j] - h[i]]++;
      }
    }

    for (i = 0; i < m; i++) {
      for (j = i; j <= m; j++) {
        haba_w[w[j] - w[i]]++;
      }
    }

    for (i = 1; i < 1500000; i++) {
      counter = counter + haba_h[i] * haba_w[i];
    }
    cout << counter << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int a, b;
  int i, j, k, l;
  int h_now = 0, w_now = 0;
  int h[1505], w[1505];
  int haba_h[150000] = {};
  int haba_w[150000] = {};
  int counter = 0;
  while (1) {
    for (i = 0; i < 150000; i++) {
      haba_h[i] = 0;
      haba_w[i] = 0;
    }
    counter = 0;
    w_now = 0;
    h_now = 0;
    h[0] = 0;
    w[0] = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    //????????§?¨?
    for (i = 1; i <= n; i++) {
      cin >> a;

      h_now = h_now + a;
      h[i] = h_now;
    }

    // w?????§?¨?
    for (i = 1; i <= m; i++) {
      cin >> b;

      w_now = w_now + b;
      w[i] = w_now;
    }

    //?????????????????????????????????
    for (i = 0; i < n; i++) {
      for (j = i; j <= n; j++) {
        haba_h[h[j] - h[i]]++;
      }
    }

    for (i = 0; i < m; i++) {
      for (j = i; j <= m; j++) {
        haba_w[w[j] - w[i]]++;
      }
    }

    for (i = 1; i < 1502; i++) {
      counter = counter + haba_h[i] * haba_w[i];
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;
typedef long long ll;

int memo[1500000];

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

    vector<int> H(n), W(m);
    rep(i, n) cin >> H[i];
    rep(i, m) cin >> W[i];

    vector<int> v;
    rep(i, n) {
      int sum = 0;
      REP(j, i, n) {
        sum += H[j];
        v.push_back(sum);
      }
    }

    vector<int> t;
    rep(i, m) {
      int sum = 0;
      REP(j, i, m) {
        sum += W[j];
        t.push_back(sum);
      }
    }

    memset(memo, 0, sizeof(memo));
    rep(i, t.size()) { memo[t[i]]++; }

    int ans = 0;
    rep(i, v.size()) { ans += memo[v[i]]; }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;
typedef long long ll;

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

    vector<int> H(n), W(m);
    rep(i, n) cin >> H[i];
    rep(i, m) cin >> W[i];

    vector<int> v;
    rep(i, n) {
      int sum = 0;
      REP(j, i, n) {
        sum += H[j];
        v.push_back(sum);
      }
    }

    vector<int> t;
    rep(i, m) {
      int sum = 0;
      REP(j, i, m) {
        sum += W[j];
        t.push_back(sum);
      }
    }

    int ans = 0;
    rep(i, v.size()) {
      rep(j, t.size()) {
        if (v[i] == t[j])
          ans++;
      }
    }

    cout << ans << endl;
  }

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

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

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h;
    vector<int> w;
    vector<int> a(1500000, 0);
    vector<int> o(1500000, 0);
    int tmp;

    for (int i = 0; i < n; ++i) {
      cin >> tmp;
      h.push_back(tmp);
      int x = 0;
      for (int j = i; j >= 0; --j) {
        x += h[j];
        ++a[x - 1];
      }
    }

    for (int i = 0; i < m; ++i) {
      cin >> tmp;
      w.push_back(tmp);
      int x = 0;
      for (int j = i; j >= 0; --j) {
        x += w[j];
        ++o[x - 1];
      }
    }

    int mi = min(accumulate(h.begin(), h.end(), 0),
                 accumulate(w.begin(), w.end(), 0));
    int ans = 0;
    for (int i = 0; i < mi; ++i) {
      ans += a[i] * o[i];
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h;
    vector<int> w;
    vector<int> a(1500, 0);
    vector<int> o(1500, 0);
    int tmp;

    for (int i = 0; i < n; ++i) {
      cin >> tmp;
      h.push_back(tmp);
      int x = 0;
      for (int j = i; j >= 0; --j) {
        x += h[j];
        ++a[x - 1];
      }
    }

    for (int i = 0; i < m; ++i) {
      cin >> tmp;
      w.push_back(tmp);
      int x = 0;
      for (int j = i; j >= 0; --j) {
        x += w[j];
        ++o[x - 1];
      }
    }

    // int mi =
    // min(accumulate(h.begin(),h.end(),0),accumulate(w.begin(),w.end(),0));
    int ans = 0;
    for (int i = 0; i < 1500; ++i) {
      ans += a[i] * o[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main(void) {
  int n, m;
  int i1, i2;
  int sum;
  int c;
  map<int, int>::iterator i;

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

    vector<int> h(n);
    vector<int> w(m);
    map<int, int> hh, ww;
    for (i1 = 0; i1 < n; i1++)
      cin >> h[i1];
    for (i1 = 0; i1 < m; i1++)
      cin >> w[i1];

    for (i1 = 1; i1 <= n; i1++) {
      sum = 0;
      for (i2 = 0; i2 < i1; i2++)
        sum += h[i2];
      hh[sum]++;
      for (i2; i2 < n; i2++) {
        sum += h[i2];
        sum -= h[i2 - i1];
        hh[sum]++;
      }
    }

    for (i1 = 1; i1 <= m; i1++) {
      sum = 0;
      for (i2 = 0; i2 < i1; i2++)
        sum += w[i2];
      ww[sum]++;
      for (i2; i2 < m; i2++) {
        sum += w[i2];
        sum -= w[i2 - i1];
        ww[sum]++;
      }
    }

    c = 0;
    for (map<int, int>::iterator it = hh.begin(); it != hh.end(); it++) {
      // cout<<it->first<<" "<<it->second<<endl;
      i = ww.find(it->first);
      if (i != hh.end())
        c += (it->second * i->second);
    }
    // cout<<endl;

    /*for(map<int,int>::iterator it=ww.begin();it!=ww.end();it++){
            cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;*/

    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define pb push_back

using namespace std;

int main(void) {
  int n, m;
  int i1, i2;
  int sum;
  int c;

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

    vector<int> h(n);
    vector<int> w(m);
    vector<int> hh, ww;
    for (i1 = 0; i1 < n; i1++) {
      cin >> h[i1];
      hh.pb(h[i1]);
    }
    for (i1 = 0; i1 < m; i1++) {
      cin >> w[i1];
      ww.pb(w[i1]);
    }

    for (i1 = 2; i1 <= n; i1++) {
      sum = 0;
      for (i2 = 0; i2 < i1; i2++)
        sum += h[i2];
      hh.pb(sum);
      for (i2; i2 < n; i2++) {
        sum += h[i2];
        sum -= h[i2 - i1];
        hh.pb(sum);
      }
    }

    for (i1 = 2; i1 <= n; i1++) {
      sum = 0;
      for (i2 = 0; i2 < i1; i2++)
        sum += w[i2];
      ww.pb(sum);
      for (i2; i2 < n; i2++) {
        sum += w[i2];
        sum -= w[i2 - i1];
        ww.pb(sum);
      }
    }

    c = 0;
    for (vector<int>::iterator it = hh.begin(); it != hh.end(); it++) {
      // cout<<(*it)<<" ";
      c += (static_cast<int>(count(ww.begin(), ww.end(), (*it))));
    }
    // cout<<endl;

    /*for(vector<int>::iterator it=ww.begin();it!=ww.end();it++){
            cout<<(*it)<<" ";
    }
    cout<<endl;*/

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

#include <algorithm>
#include <iostream>
#include <vector>
#define N1 1501
#define N2 1000
using namespace std;
int dh[N1 * N2], dw[N1 * N2];
int main() {
  int N, M, h[N1], w[N1];
  vector<int> H, W;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i > 0)
        h[i] += h[i - 1];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i > 0)
        w[i] += w[i - 1];
    }
    for (int i = 0; i < N1 * N2; i++)
      dh[i] = dw[i] = 0;
    int l = 1;
    while (l != N + 1) {
      for (int i = l - 1; i < N; i++) {
        dh[h[i] - h[i - l]]++;
      }
      l++;
    }
    l = 1;
    while (l != M + 1) {
      for (int i = l - 1; i < M; i++) {
        dw[w[i] - w[i - l]]++;
      }
      l++;
    } /*
     sort(H.begin(),H.end());
     sort(W.begin(),W.end());
     for(int i=0;i<N1;i++){
       for(int j=0;j<N2;j++){
         dh[i][j]=dw[i][j]=0;
       }
     }
     H.clear();
     W.clear();
     */
    int sum = 0;
    for (int i = 0; i < N1 * N2; i++) {
      if (dh[i] != 0 && dw[i] != 0)
        sum += dh[i] * dw[i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define N1 1500
using namespace std;
int main() {
  int N, M, h[N1], w[N1];
  vector<int> H, W;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      if (i > 0)
        h[i] += h[i - 1];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      if (i > 0)
        w[i] += w[i - 1];
    }
    int l = 1;
    while (l != N + 1) {
      for (int i = l - 1; i < N; i++) {
        H.push_back(h[i] - h[i - l]);
      }
      l++;
    }
    l = 1;
    while (l != M + 1) {
      for (int i = l - 1; i < M; i++) {
        W.push_back(w[i] - w[i - l]);
      }
      l++;
    }
    sort(H.begin(), H.end());
    sort(W.begin(), W.end());
    int hcnt = 0, wcnt = 0, sum = 0, flag, k = 0;
    for (int i = 0; i < H.size(); i++) {
      hcnt = 1;
      while (H[i] == H[i + 1]) {
        hcnt++;
        i++;
      }
      wcnt = 0;
      flag = 0;
      for (int j = k; j < W.size(); j++) {
        if (H[i] == W[j]) {
          flag = 1;
          wcnt++;
          k = j + 1;
        } else if (flag == 1)
          break;
      }
      sum += hcnt * wcnt;
    }
    H.clear();
    W.clear();
    cout << sum << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

int combi(int n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> x, y;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      h += y[i];
      y.push_back(h);
    }

    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      w += x[i];
      x.push_back(w);
    }
    // y=x+b → y-x=bとなる
    map<int, int> cnt;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        int diff = y[i] - x[j];
        cnt[diff]++;
      }
    }

    // 全探索
    int ans = 0;
    for (int i = -x[m]; i < y[n] + 1; i++) {
      ans += combi(cnt[i]);
    }

    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

using namespace std;

int combi(int n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> x, y;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      h += y[i];
      y.push_back(h);
    }

    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      w += x[i];
      y.push_back(w);
    }
    // y=x+b → y-x=bとなる
    map<int, int> cnt;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        int diff = y[i] - x[j];
        cnt[diff]++;
      }
    }

    // 全探索
    int ans = 0;
    for (int i = -x[m]; i < y[n] + 1; i++) {
      ans += combi(cnt[i]);
    }

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

#include <iostream>
using namespace std;

const int MAX_N = 1500, MAX_M = 1500, MAX_W = 1000;
int roadV[MAX_N], roadH[MAX_M];
int widthV[MAX_N * MAX_W + 1];
int widthH[MAX_M * MAX_W + 1];

inline int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, i, j, sumV, sumH, cnt, minWidth;

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

    sumV = 0;
    sumH = 0;
    for (i = 0; i < n; i++) {
      cin >> roadV[i];
      sumV += roadV[i];
    }
    for (i = 0; i < m; i++) {
      cin >> roadH[i];
      sumH += roadH[i];
    }
    minWidth = min(sumV, sumH);

    for (i = 1; i <= minWidth; i++) {
      widthV[i] = 0;
    }
    for (i = 1; i <= minWidth; i++) {
      widthH[i] = 0;
    }

    for (i = 0; i < n; i++) {
      sumV = 0;
      for (j = i; j < n; j++) {
        sumV += roadV[j];
        widthV[sumV]++;
      }
    }

    for (i = 0; i < m; i++) {
      sumH = 0;
      for (j = i; j < m; j++) {
        sumH += roadH[j];
        widthH[sumH]++;
      }
    }

    cnt = 0;
    for (i = 1; i <= minWidth; i++) {
      cnt += widthV[i] * widthH[i];
    }

    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

const int MAX_N = 1500, MAX_M = 1500, MAX_W = 1000;
int roadV[MAX_N], roadH[MAX_M];
int widthV[MAX_N * MAX_W + 1];
int widthH[MAX_M * MAX_W + 1];

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, i, j, sumV, sumH, cnt;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> roadV[i];
    for (i = 0; i < n; i++)
      cin >> roadH[i];

    for (i = 1; i <= n * 1000; i++) {
      widthV[i] = 0;
      widthH[i] = 0;
    }

    for (i = 0; i < n - 1; i++) {
      sumV = 0;
      sumH = 0;
      for (j = i; j < n; j++) {
        sumV += roadV[i];
        sumH += roadH[i];

        widthV[sumV]++;
        widthH[sumH]++;
      }
    }

    cnt = 0;
    for (i = 1; i <= n * 1000; i++) {
      cnt += widthV[i] * widthH[i];
    }

    cout << cnt << endl;
  }
  return 0;
}