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

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

using namespace std;

void solve() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> Vech(N);
    vector<int> Vecw(M);
    map<int, int> Map;
    for (int i = 0; i < N; ++i) {
      cin >> Vech[i];
    }
    for (int i = 0; i < M; ++i) {
      cin >> Vecw[i];
    }
    for (int i = 0; i < M; ++i) {
      int w = Vecw[i];
      Map[w]++;
      for (int j = i + 1; j < M; ++j) {
        w += Vecw[j];
        Map[w]++;
      }
    }

    int count = 0;
    for (int i = 0; i < N; ++i) {
      int h = Vech[i];
      count += Map[h];
      for (int j = i + 1; j < N; ++j) {
        h += Vech[j];
        count += Map[h];
      }
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

void solve() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> Vech(N);
    vector<int> Vecw(M);
    vector<int> width;
    for (int i = 0; i < N; ++i) {
      cin >> Vech[i];
    }
    for (int i = 0; i < M; ++i) {
      cin >> Vecw[i];
    }
    for (int i = 0; i < M; ++i) {
      int w = Vecw[i];
      width.push_back(w);
      for (int j = i + 1; j < M; ++j) {
        w += Vecw[j];
        width.push_back(w);
      }
    }

    int count = 0;
    int wsize = width.size();
    for (int a = 0; a < wsize; ++a) {
      for (int i = 0; i < N; ++i) {
        int h = Vech[i];
        if (h == width[a]) {
          count++;
          continue;
        } else if (h > width[a]) {
          continue;
        }
        for (int j = i + 1; j < N; ++j) {
          h += Vech[j];
          if (h == width[a]) {
            count++;
            break;
          } else if (h > width[a]) {
            break;
          }
        }
      }
    }
    cout << count << endl;
  }
}

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

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

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

    for (auto &i : x) {
      cin >> i;
    }

    for (auto &i : y) {
      cin >> i;
    }

    vector<int> dis_x;
    for (int i = 0; i < n; i++) {
      int dis = 0;
      for (int j = i; j < n; j++) {
        dis += x[j];
        dis_x.push_back(dis);
      }
    }

    vector<int> dis_y;
    for (int i = 0; i < m; i++) {
      int dis = 0;
      for (int j = i; j < m; j++) {
        dis += y[j];
        dis_y.push_back(dis);
      }
    }

    sort(dis_x.begin(), dis_x.end());
    sort(dis_y.begin(), dis_y.end());

    int res = 0;
    for (int i = 0; i < dis_x.size(); i++) {
      int count_x = 1;
      while (dis_x[i + count_x] == dis_x[i])
        count_x++;

      auto up = upper_bound(dis_y.begin(), dis_y.end(), dis_x[i]);
      auto lo = lower_bound(dis_y.begin(), dis_y.end(), dis_x[i]);
      if (up != dis_y.end() || lo != dis_y.end())
        res += count_x * (up - lo);

      i += count_x - 1;
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

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

    for (auto &i : x) {
      cin >> i;
    }

    for (auto &i : y) {
      cin >> i;
    }

    vector<int> dis_x;
    for (int i = 0; i < n; i++) {
      int dis = x[i];
      dis_x.push_back(dis);
      if (i + 1 == n)
        continue;
      for (int j = i + 1; j < n; j++) {
        dis += x[j];
        dis_x.push_back(dis);
      }
    }

    vector<int> dis_y;
    for (int i = 0; i < m; i++) {
      int dis = y[i];
      dis_y.push_back(dis);
      if (i + 1 == m)
        continue;
      for (int j = i + 1; j < m; j++) {
        dis += y[j];
        dis_y.push_back(dis);
      }
    }

    int res = 0;
    for (int i = 0; i < dis_x.size(); i++) {
      for (int j = 0; j < dis_y.size(); j++) {
        if (dis_x[i] == dis_y[j]) {
          res++;
        }
      }
    }

    cout << res << endl;
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const LL MOD = 1000000007;

template <typename T> void dump(T a, int n) {
  for (int i = 0; i < n; i++)
    cout << a[i] << (i == n - 1 ? '\\n' : ' ');
}

template <typename T> void dump(T a) { dump<T>(a, a.size()); }

constexpr int LIM = 1000 * 1500;

int main(void) {
  for (int N, M; cin >> N >> M, N;) {
    VI hs(N), ws(M);
    for (auto &h : hs) {
      cin >> h;
    }
    for (auto &w : ws) {
      cin >> w;
    }

    VI hsum(LIM + 1), wsum(LIM + 1);

    for (int i = 0; i < N; i++) {
      int sum = 0;

      for (int j = i; j < N; j++) {
        sum += hs[j];
        hsum[sum]++;
      }
    }

    for (int i = 0; i < M; i++) {
      int sum = 0;

      for (int j = i; j < M; j++) {
        sum += ws[j];
        wsum[sum]++;
      }
    }

    int cnt = 0;
    for (int i = 0; i <= LIM; i++) {
      cnt += hsum[i] * wsum[i];
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const LL MOD = 1000000007;

template <typename T> void dump(T a, int n) {
  for (int i = 0; i < n; i++)
    cout << a[i] << (i == n - 1 ? '\\n' : ' ');
}

template <typename T> void dump(T a) { dump<T>(a, a.size()); }

int main(void) {
  for (int N, M; cin >> N >> M, N;) {
    VI hs(N), ws(M);
    for (auto &h : hs) {
      cin >> h;
    }
    for (auto &w : ws) {
      cin >> w;
    }

    VI sum(M + 1);
    partial_sum(ws.begin(), ws.end(), sum.begin() + 1);

    int cnt = 0;
    for (int y1 = 0; y1 < N; y1++) {
      int h = 0;
      for (int y2 = y1; y2 < N; y2++) {
        h += hs[y2];

        int x = 0;
        for (int x1 = 0; x1 < M; x1++) {
          bool found = binary_search(sum.begin(), sum.end(), h + x);
          cnt += found;

          x += ws[x1];
        }
      }
    }

    cout << cnt << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int main() {
  while (1) {
    int n = 0, m = 0;
    int h[1550] = {}, w[1550] = {};
    int l[1500000] = {};
    cin >> n >> m;
    if (n == 0) {
      break;
    }
    int t = 0;
    FOR(i, 1, n + 1) {
      cin >> h[i];
      t += h[i];
      h[i] = t;
    }
    int s = 0;
    FOR(i, 1, m + 1) {
      cin >> w[i];
      s += w[i];
      w[i] = s;
    }
    sort(h, &h[n]);
    sort(w, &w[m]);
    ull sum = 0;
    rep(i, n) {
      FOR(j, i + 1, n + 1) { l[h[j] - h[i]] += 1; }
    }
    rep(i, m) {
      FOR(j, i + 1, m + 1) { sum += (ull)l[w[j] - w[i]]; }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int N, M, count = 0, j, k;
    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 (j = 0; j < N; ++j) {
      int wsum = 0;
      int hsum = 0;
      for (int i = j; i < N; ++i) {
        hsum += h[i];
        for (k = 0; k < M; ++k) {
          wsum = 0;
          for (int i = k; i < M; ++i) {
            wsum += w[i];
            if (wsum == hsum)
              ++count;
            if (wsum > hsum)
              break;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>
using namespace std;

int num[2000 * 1500];

int main() {
  int N, M;
  while (cin >> N >> M && N != 0) {
    memset(num, 0, sizeof(num));
    int h[1501], w[1501];
    h[0] = w[0] = 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];
    }

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

    int ans = 0;
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        ans += num[w[j] - w[i]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int num[2000 * 1500];

int main() {
  int N, M;
  while (cin >> N >> M && N != 0) {
    memset(num, 0, sizeof(num));
    int h[1501], w[1501];
    h[0] = w[0] = 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];
    }

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

    int ans = 0;
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        ans += num[w[i] - w[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error


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

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, !(N == 0 && M == 0)) {
    long long h[N + 1], w[M + 1];
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= N; i++) {
      int k;
      cin >> k;
      h[i] = (h[i - 1] + k);
    }
    for (int i = 1; i <= M; i++) {
      int l;
      cin >> l;
      w[i] = (w[i - 1] + l);
    }
    vector<long long> H((N + 1) * (N + 1)), W((M + 1) * (M + 1));
    for (int i = 0; i < H.size(); i++) {
      H[i] = 1000000007;
    }
    for (int i = 0; i < W.size(); i++) {
      W[i] = 1000000007;
    }
    int p = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j <= N; j++) {
        if (h[i] < h[j]) {
          H[p] = h[j] - h[i];
          //      cout << H[p]<<endl;
          p++;
        }
      }
    }
    // cout << "^^^^";
    sort(H.begin(), H.end());
    int q = 0;
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j <= M; j++) {
        if (w[i] < w[j]) {
          W[q] = w[j] - w[i];
          //   cout << W[q]<<endl;
          q++;
        }
      }
    }
    // cout << "----";
    sort(W.begin(), W.end());
    for (int i = 0; i < N; i++) {
      // cout << W[i]<<",";
    }
    // cout <<endl;
    for (int i = 0; i < M; i++) {
      // cout << H[i];
    }
    // cout <<endl;
    long long ans = 0;
    int flag = 0;
    for (int i = 0; i < p; i++) {
      for (int j = flag; W[j] <= H[i] && j < q; j++) {
        if (W[j] == H[i]) {
          ans++;
        }
        if (W[j] < H[i]) {
          flag++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, !(N == 0 && M == 0)) {
    long long h[N + 1], w[M + 1];
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= N; i++) {
      int k;
      cin >> k;
      h[i] = (h[i - 1] + k);
    }
    for (int i = 1; i <= M; i++) {
      int l;
      cin >> l;
      w[i] = (w[i - 1] + l);
    }
    long long H[(N + 1) * (N + 1)], W[(M + 1) * (M + 1)];
    int p = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j <= N; j++) {
        if (h[i] < h[j]) {
          H[p] = h[j] - h[i];
          p++;
        }
      }
    }
    sort(H, H + p);
    int q = 0;
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j <= M; j++) {
        if (w[i] < w[j]) {
          W[q] = w[j] - w[i];
          q++;
        }
      }
    }
    sort(W, W + q);
    long long ans = 0;
    int flag = 0;
    for (int i = 0; i < p; i++) {
      for (int j = flag; W[j] <= H[i] && j < q; j++) {
        if (W[j] == H[i]) {
          ans++;
        }
        if (W[j] < H[i]) {
          flag++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)

int read() {
  int i;
  scanf("%lld", &i);
  return i;
}
int h[1500 + 1], w[1500 + 1];
signed main() {
  bool flag = false;
  while (true) {
    int N = read(), M = read();
    if (N == 0 && M == 0)
      break;
    int maxh = 0, maxw = 0;
    REP(i, N) {
      h[i] = read();
      maxh += h[i];
    }
    REP(i, M) {
      w[i] = read();
      maxw += w[i];
    }

    int MIN = min(maxh, maxw);

    int cntH[maxh + 1], cntW[maxw + 1];

    REP(i, MIN + 1) {
      cntH[i] = 0;
      cntW[i] = 0;
    }

    int length;
    REP(i, N) {
      length = 0;
      FOR(j, i, N) {
        length += h[j];
        cntH[length]++;
      }
    }

    REP(i, M) {
      length = 0;
      FOR(j, i, M) {
        length += w[j];
        cntW[length]++;
      }
    }
    int ans = 0;
    FOR(i, 1, MIN + 1) ans += cntH[i] * cntW[i];

    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

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

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)

int read() {
  int i;
  scanf("%lld", &i);
  return i;
}
int h[1500 + 1], w[1500 + 1];
signed main() {
  bool flag = false;
  while (true) {
    int N = read(), M = read();
    if (N == 0 && M == 0)
      break;
    if (flag)
      cout << endl;
    flag = true;
    int cntH[1000 * 1500 + 1], cntW[1000 * 1500 + 1];
    int maxh = 0, maxw = 0;
    REP(i, N) {
      h[i] = read();
      maxh += h[i];
    }
    REP(i, M) {
      w[i] = read();
      maxw += w[i];
    }

    int MIN = min(maxh, maxw);

    REP(i, MIN + 1) {
      cntH[i] = 0;
      cntW[i] = 0;
    }

    int length;
    REP(i, N) {
      length = 0;
      FOR(j, i, N) {
        length += h[j];
        cntH[length]++;
      }
    }

    REP(i, M) {
      length = 0;
      FOR(j, i, M) {
        length += w[j];
        cntW[length]++;
      }
    }
    int ans = 0;
    FOR(i, 1, MIN + 1) ans += cntH[i] * cntW[i];

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

#define MAX 1000000
int const MOD = 1e9 + 7;
int main(void) {
  int N, M;
  while (cin >> N >> M, N || M) {
    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];

    int sh[1510] = {0}, sw[1510] = {0};
    for (int i = 0; i < N; ++i) {
      sh[i + 1] = sh[i] + h[i];
    }
    for (int i = 0; i < M; ++i) {
      sw[i + 1] = sw[i] + w[i];
    }
    map<ll, int> nh, nw;
    for (int i = 0; i < N + 1; ++i) {
      for (int j = 0; j < i; ++j) {
        nh[sh[i] - sh[j]]++;
      }
    }
    for (int i = 0; i < M + 1; ++i) {
      for (int j = 0; j < i; ++j) {
        nw[sw[i] - sw[j]]++;
      }
    }
    ll sum = 0;
    for (auto itr = nh.begin(); itr != nh.end(); ++itr) {
      sum += (*itr).second * nw[(*itr).first];
    }
    cout << sum << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1167
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

#define MAX 1000000
int const MOD = 1e9 + 7;
int main(void) {
  int N, M;
  while (cin >> N >> M, N || M) {
    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];

    int sh[1510] = {0}, sw[1510] = {0};
    for (int i = 0; i < N; ++i) {
      sh[i + 1] = sh[i] + h[i];
    }
    for (int i = 0; i < M; ++i) {
      sw[i + 1] = sw[i] + w[i];
    }
    map<int, int> nh, nw;
    for (int i = 0; i < N + 1; ++i) {
      for (int j = 0; j < i; ++j) {
        nh[sh[i] - sh[j]]++;
      }
    }
    for (int i = 0; i < M + 1; ++i) {
      for (int j = 0; j < i; ++j) {
        nw[sw[i] - sw[j]]++;
      }
    }
    ll sum = 0;
    for (int i = 1; i <= 1500 * 1000; ++i) {
      sum += nh[i] * nw[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int h[1510];
    int w[1510];
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> w[j];
    }
    int sq = 0;
    vector<vector<int>> dp(n, vector<int>(n, 0));
    vector<vector<int>> dp2(m, vector<int>(m, 0));
    vector<int> retu(1500010, 0);
    dp[0][0] = h[0];
    dp2[0][0] = w[0];
    retu[h[0]]++;
    for (int i = 1; i < n; i++) {
      dp[0][i] = dp[0][i - 1] + h[i];
      retu[dp[0][i]]++;
    }
    for (int i = 1; i < n; i++) {
      for (int j = i; j < n; j++) {
        dp[i][j] = dp[i][j - 1] + h[j];
        retu[dp[i][j]]++;
      }
    }
    if (retu[w[0]] > 0) {
      sq += retu[w[0]];
    }
    for (int i = 1; i < m; i++) {
      dp2[0][i] = dp2[0][i - 1] + w[i];
      if (retu[dp2[0][i]] > 0) {
        sq += retu[dp2[0][i]];
      }
    }
    for (int i = 1; i < m; i++) {
      for (int j = i; j < m; j++) {
        dp2[i][j] = dp2[i][j - 1] + w[j];
        if (retu[dp2[i][j]] > 0) {
          sq += retu[dp2[i][j]];
        }
      }
    }
    cout << sq << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int h[1510];
    int w[1510];
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> w[j];
    }
    int sq = 0;
    vector<vector<int>> dp(n, vector<int>(n, 0));
    vector<vector<int>> dp2(m, vector<int>(m, 0));
    dp[0][0] = h[0];
    dp2[0][0] = w[0];
    for (int i = 1; i < n; i++) {
      dp[0][i] = dp[0][i - 1] + h[i];
    }
    for (int i = 1; i < n; i++) {
      for (int j = i; j < n; j++) {
        dp[i][j] = dp[i][j - 1] + h[j];
      }
    }
    for (int i = 1; i < m; i++) {
      dp2[0][i] = dp2[0][i - 1] + w[i];
    }
    for (int i = 1; i < m; i++) {
      for (int j = i; j < m; j++) {
        dp2[i][j] = dp2[i][j - 1] + w[j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        for (int k = 0; k < m; k++) {
          for (int l = k; l < m; l++) {
            if (dp[i][j] == dp2[k][l]) {
              sq++;
            }
          }
        }
      }
    }
    cout << sq << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int MAX = 1500001;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N);
    vector<int> w(M);

    rep(i, N) { cin >> h[i]; }

    rep(i, M) { cin >> w[i]; }

    vector<int> vertical(MAX, 0);
    rep(i, h.size()) {
      int tmp = 0;
      REP(j, i, h.size()) {
        tmp += h[j];
        vertical[tmp]++;
      }
    }

    vector<int> horizontal(MAX, 0);
    rep(i, w.size()) {
      int tmp = 0;
      REP(j, i, w.size()) {
        tmp += w[j];
        horizontal[tmp]++;
      }
    }

    int ans = 0;
    rep(i, MAX) { ans += vertical[i] * horizontal[i]; }

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

Error: TLE

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N);
    vector<int> w(M);

    rep(i, N) { cin >> h[i]; }

    rep(i, M) { cin >> w[i]; }

    vector<int> vertical;
    rep(i, h.size()) {
      int tmp = 0;
      REP(j, i, h.size()) {
        tmp += h[j];
        vertical.push_back(tmp);
      }
    }

    vector<int> horizontal;
    rep(i, w.size()) {
      int tmp = 0;
      REP(j, i, w.size()) {
        tmp += w[j];
        horizontal.push_back(tmp);
      }
    }

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

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