prithivMLmods/Coder-Stat huggingface dataset:
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int e;
int main() {
  while (cin >> e, e) {
    int ans = 10000000;
    int m;
    bool flag = true;
    for (int i = 0; i <= 1000000 && flag; i++) {
      if (i >= e + 1) {
        break;
      }
      for (int j = 0; j <= 1000000 && flag; j++) {
        if (i + j >= e + 1 || i + j * j >= e + 1) {
          break;
        }
        for (int k = 0; k <= 1000000 && flag; k++) {
          if (i + j * j + k >= e + 1 || i + j * j + k * k >= e + 1 ||
              i + j * j + k * k * k >= e + 1) {
            break;
          }
          if (i + j * j + k * k * k == e) {
            m = i + j + k;
            ans = min(ans, m);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int e;
int main() {
  while (cin >> e, e) {
    int ans = 10000000;
    for (int z = 0; z * z * z <= e; z++) {
      for (int y = 0; y * y + z * z * z <= e; y++) {
        int x = e - y * y - z * z * z;
        if (ans > x + y + z) {
          ans = x + y + z;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int e, ans;
  while (1) {
    cin >> e;
    if (!e)
      break;
    ans = (1e9);
    for (int z = 0; z * z * z <= e; z++) {
      int Z = z * z * z;
      for (int y = 0; y * y <= e - Z; y++) {
        int Y = y * y;
        for (int x = 0; x <= e - Y - Z; x++) {
          int X = x;
          if (X + Y + Z != e)
            continue;
          ans = min(ans, x + y + z);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
  int e, ans;
  while (1) {
    cin >> e;
    if (!e)
      break;
    ans = (1e9);
    for (int z = 0; z * z * z <= e; z++) {
      int Z = z * z * z;
      for (int y = 0; y * y <= e - Z; y++) {
        int Y = y * y;
        int X = e - Y - Z;
        if (X + Y + Z != e)
          continue;
        ans = min(ans, X + y + z);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define PB push_back
#define in scanf
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
void solve() {
  int e;
  while (cin >> e, e != 0) {
    int m = 1000000000;
    int x;
    REP(z, (int)pow(e, 0.333) + 1) {
      REP(y, (int)pow(e, 0.5) + 1) {
        x = e - pow(y, 2) - pow(z, 3);
        if (x >= 0)
          m = min(x + y + z, m);
      }
    }
    cout << m << endl;
  }
}

int main() { solve(); }

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define PB push_back
#define in scanf
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
void solve() {
  int e;
  while (cin >> e, e != 0) {
    int m = 1000000000;
    int x;
    for (int z = 0; z * z * z <= e; z++) {
      for (int y = 0; y * y + z * z * z <= e; y++) {
        x = e - y * y - z * z * z;
        m = min(x + y + z, m);
      }
    }
    cout << m << endl;
  }
}

int main() { solve(); }
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int e;
  while (cin >> e, e) {
    int ans = 1000000;
    for (int z = 0; z <= e; ++z) {
      for (int y = 0; y * y + z * z * z <= e; ++y) {
        int x = e - y * y - z * z * z;
        ans = min(ans, x + y + z);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int e;
  while (cin >> e, e) {
    int ans = 1000000;
    for (int z = 0; z * z * z <= e; ++z) {
      for (int y = 0; y * y + z * z * z <= e; ++y) {
        int x = e - y * y - z * z * z;
        ans = min(ans, x + y + z);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int solve(int E) {
  int ans = 1000000;
  for (int y = 0; y <= E; ++y) {
    for (int z = 0; z <= E; ++z) {
      int x = E - y * y - z * z;
      ans = min(ans, x + y + z);
    }
  }
  return ans;
}

int main() {
  int E;
  while (cin >> E && E > 0) {
    cout << solve(E) << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int solve(int E) {
  int ans = 1000000;
  int x;
  for (int y = 0; y * y <= E; ++y) {
    for (int z = 0; z * z * z <= E; ++z) {
      x = E - y * y - z * z * z;
      if (x >= 0)
        ans = min(ans, x + y + z);
    }
  }
  return ans;
}

int main() {
  int E;
  while (cin >> E && E > 0) {
    cout << solve(E) << endl;
  }
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int E;

int main() {

  while (cin >> E && E > 0) {
    int min = 1000001;
    for (int z = 0; z < E; ++z) {
      for (int y = 0; y * y < E - z * z * z; ++y) {
        int x = E - z * z * z - y * y;
        if (x + y + z < min)
          min = x + y + z;
      }
    }
    cout << min << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int E;

int main() {

  while (cin >> E) {
    if (E == 0)
      break;
    int min = 100000001;
    for (int z = 0; z * z * z < E + 1; ++z) {
      for (int y = 0; y * y < E - z * z * z + 1; ++y) {
        int x = E - z * z * z - y * y;
        if (x + y + z < min)
          min = x + y + z;
      }
    }
    cout << min << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int e;
int main() {

  vector<int> ans;
  while (cin >> e, e) {
    int m = 1000000;
    for (int i = 0; i <= e; ++i) {
      if (i * i * i > e)
        break;
      for (int j = 0; j <= e; ++j) {
        if (i * i * i + j * j > e)
          break;
        for (int k = 0; k <= e; ++k) {
          if (i * i * i + j * j + k == e)
            m = min(m, i + j + k);
        }
      }
    }
    ans.push_back(m);
  }
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int e;
int main() {

  vector<int> ans;
  while (cin >> e, e) {
    int m = 1000000;
    for (int i = 0; i <= e; ++i) {
      if (i * i * i > e)
        break;
      for (int j = 0; j <= e; ++j) {
        if (i * i * i + j * j > e)
          break;
        if (e - i * i * i - j * j >= 0)
          m = min(m, i + j + e - i * i * i - j * j);
      }
    }
    ans.push_back(m);
  }
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int ene;
  while (cin >> ene && ene > 0) {
    int min = 1000000;
    for (int z = 0; z <= 100; z++) {
      int xy = ene - z * z * z;
      if (xy >= 0)
        for (int y = 0; y <= xy; y++) {
          int x = xy - y * y;
          if (x >= 0)
            if (x + y + z < min)
              min = x + y + z;
        }
    }
    cout << min << endl;
  }
}


Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int ene;
  while (cin >> ene && ene > 0) {
    int min = 1000000;
    for (int z = 0; z <= 100; z++) {
      int xy = ene - z * z * z;
      if (xy >= 0)
        for (int y = 0; y <= 10000; y++) {
          int x = xy - y * y;
          if (x >= 0)
            if (x + y + z < min)
              min = x + y + z;
        }
    }
    cout << min << endl;
  }
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 1000000

int main(void) {
  int e;
  vector<pii> vs;
  for (int y = 0;; y++) {
    int k = y * y;
    if (k > MAX)
      break;
    for (int z = 0;; z++) {
      int l = z * z * z;
      if (k + l > MAX)
        break;
      vs.push_back(pii(k + l, y + z));
    }
  }
  sort(ALL(vs));

  while (cin >> e, e) {
    int ans = (1 << 29);
    for (int x = 0; x <= max(e / 3 + 1, 3); x++) {
      int e_x = e - x;

      int a = 0, b = vs.size();
      while (b - a > 1) {
        int m = (a + b) / 2;
        if (vs[m].first >= e_x)
          b = m;
        else
          a = m;
      }
      if (vs[b].first == e_x)
        ans = min(ans, x + vs[b].second);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 1000000

int main(void) {
  int e;
  vector<pii> vs;
  for (int y = 0;; y++) {
    int k = y * y;
    if (k > MAX)
      break;
    for (int z = 0;; z++) {
      int l = z * z * z;
      if (k + l > MAX)
        break;
      vs.push_back(pii(k + l, y + z));
    }
  }
  sort(ALL(vs));

  while (cin >> e, e) {
    int ans = (1 << 29);
    rep(i, vs.size()) {
      int x = e - vs[i].first;
      if (x < 0)
        continue;
      ans = min(ans, x + vs[i].second);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  while (n != 0) {
    int a;
    a = 1e9;
    for (int i = 0; i < pow(n, 0.34); i++) {
      for (int j = 0; j <= pow(n, 0.5); j++) {
        int t = (n - i * i * i - j * j);
        if (t >= 0)
          a = min(i + j + t, a);
      }
    }
    cout << a << endl;
    cin >> n;
  }
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  while (n != 0) {
    int a;
    a = 1e9;
    for (int i = 0; i * i * i <= n; i++) {
      for (int j = 0; j * j + i * i * i <= n; j++) {
        int t = (n - i * i * i - j * j);
        if (t >= 0)
          a = min(i + j + t, a);
      }
    }
    cout << a << endl;
    cin >> n;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(x) for (auto it = (x).begin(); it != (x).end(); it++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

using namespace std;

int main() {
  while (1) {
    int e;
    int ans = 1e9 + 7;
    cin >> e;
    if (e == 0)
      break;

    for (int i = 0; i <= e; i++) {
      for (int j = 0; i + j * j <= e; j++) {
        for (int k = 0; i + j * j + k * k * k <= e; k++) {
          if (i + j * j + k * k * k == e)
            ans = min(ans, i + j + k);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(x) for (auto it = (x).begin(); it != (x).end(); it++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

using namespace std;

int main() {
  while (1) {
    int e;
    int ans = 1e9 + 7;
    cin >> e;
    if (e == 0)
      break;

    rep(i, 0, 1000) {
      rep(j, 0, 1000) {
        int k = e - i * i - j * j * j;
        if (k < 0)
          continue;
        ans = min(ans, i + j + k);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {

  int e;
  while (cin >> e) {
    if (e == 0)
      break;
    int ans = 10000000;
    for (int z = 0; z * z * z <= e; z++) {
      for (int y = 0; z * z * z + y <= e; y++) {
        int x = e - z * z * z - y * y;
        if (x >= 0) {
          ans = min(ans, x + y + z);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

// EOF

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {

  int e;
  while (cin >> e) {
    if (e == 0)
      break;
    int ans = 10000000;
    for (int z = 0; z * z * z <= e; z++) {
      for (int y = 0; z * z * z + y * y <= e; y++) {
        int x = e - z * z * z - y * y;
        if (x >= 0) {
          ans = min(ans, x + y + z);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

// EOF
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#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;
}

signed main() {
  while (true) {
    int e = read();
    int ans = 100000;
    int m;
    if (e == 0)
      break;
    int z, y, x;
    for (z = 1; z * z * z < e; z++) {
    }
    for (y = 1; y * y < e; y++) {
    }
    for (int i = 0; i <= z; i++) {
      for (int j = 0; j <= y; j++) {
        for (x = 0; x + j * j + i * i * i <= e; x++) {
          if (x + j * j + i * i * i == e)
            ans = min(ans, x + j + i);
        }
      }
    }

    cout << ans << endl;
  }
}

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;
}

signed main() {
  while (true) {
    int e = read();
    int ans = 100000;
    int m;
    if (e == 0)
      break;
    int z, y, x;
    for (z = 1; z * z * z < e; z++) {
    }
    for (y = 1; y * y < e; y++) {
    }
    for (int i = 0; i <= z; i++) {
      for (int j = 0; j <= y; j++) {
        if (e - i * i * i - j * j >= 0) {
          x = e - i * i * i - j * j;
          ans = min(ans, x + j + i);
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int e, m;

  while (cin >> e && e > 0) {
    m = e;
    for (int z = 0; pow(z, 3) <= e; z++) {
      for (int y = 0; pow(z, 3) + pow(y, 2) <= e; y++) {
        // int z=e-x-pow(y,2);
        int x = e - (pow(z, 3) + pow(y, 2));
        if (x + y + z < m)
          m = x + y + z;
      }
    }
    cout << m << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int e, m;

  while (cin >> e && e > 0) {
    m = e;
    for (int z = 0; pow(z, 3) <= e; z++) {
      int pz = pow(z, 3);
      for (int y = 0; pz + pow(y, 2) <= e; y++) {
        int x = e - (pz + pow(y, 2));
        if (x + y + z < m)
          m = x + y + z;
      }
    }
    cout << m << endl;
  }
}
Problem p01138 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int imos[86410] = {};
    for (int i = 0; i < n; i++) {
      int a, b, c, d, e, f;
      scanf("%d:%d:%d", a, b, c);
      scanf("%d:%d:%d", d, e, f);
      imos[a * 3600 + b * 60 + c]++;
      imos[d * 3600 + e * 60 + f]--;
    }
    int ans = imos[0];
    for (int i = 1; i <= 86400; i++) {
      imos[i] += imos[i - 1];
      ans = max(ans, imos[i]);
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int imos[86410] = {};
    for (int i = 0; i < n; i++) {
      int a, b, c, d, e, f;
      char ch;
      cin >> a >> ch >> b >> ch >> c;
      cin >> d >> ch >> e >> ch >> f;
      imos[a * 3600 + b * 60 + c]++;
      imos[d * 3600 + e * 60 + f]--;
    }
    int ans = imos[0];
    for (int i = 1; i <= 86400; i++) {
      imos[i] += imos[i - 1];
      ans = max(ans, imos[i]);
    }
    cout << ans << endl;
  }
}
Problem p01138 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;
int N;
P G[10000];
int solve() {
  int res = 0;
  int c = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (G[i].second) {
      c++;
      res = max(c, res);
    } else {
      c--;
    }
  }
  return res;
}
int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 2; j++) {
        char str[10];
        cin >> str;
        int a = (str[0] - '0') * 10 + str[1] - '0';
        int b = (str[3] - '0') * 10 + str[4] - '0';
        int c = (str[6] - '0') * 10 + str[7] - '0';
        G[i * 2 + j] = P(a * 60 * 60 + b * 60 + c, 1 - j);
      }
    }
    sort(G, G + 2 * N);
    cout << solve() << endl;
  }
}

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;
int N;
P G[20000];
int solve() {
  int res = 0;
  int c = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (G[i].second) {
      c++;
      res = max(c, res);
    } else {
      c--;
    }
  }
  return res;
}
int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 2; j++) {
        char str[10];
        cin >> str;
        int a = (str[0] - '0') * 10 + str[1] - '0';
        int b = (str[3] - '0') * 10 + str[4] - '0';
        int c = (str[6] - '0') * 10 + str[7] - '0';
        G[i * 2 + j] = P(a * 60 * 60 + b * 60 + c, 1 - j);
      }
    }
    sort(G, G + 2 * N);
    cout << solve() << endl;
  }
}
Problem p01138 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

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

int n;
int imos[86400];

void solve() {
  rep(i, n) {
    int s[3];
    int t[3];
    scanf("%d:%d:%d", &s[0], &s[1], &s[2]);
    scanf("%d:%d:%d", &t[0], &t[1], &t[2]);

    // hh:mm:ss を 秒に変換
    int S = s[2] + 60 * (s[1] + s[0] * 60);
    int T = t[2] + 60 * (t[1] + t[0] * 60);

    // 入る時間に +1
    imos[S]++;

    // 出る時間+1に -1
    imos[T + 1]--;

    // ナイーブな実装 (写経しなくて良い)
    // for(int j = S; j <= T; j++) imos[j]++;
  }

  // 累積和を計算する
  rep(i, 86400) if (i) imos[i] += imos[i - 1];

  int ans = 0;
  rep(i, 86400) ans = max(ans, imos[i]);

  cout << ans << endl;
}

int main() {
  // こう書くとn==0のときにwhileループが停止する
  while (cin >> n, n) {
    rep(i, 86400) imos[i] = 0;
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

int n;
int imos[86400];

void solve() {
  rep(i, n) {
    int s[3];
    int t[3];
    scanf("%d:%d:%d", &s[0], &s[1], &s[2]);
    scanf("%d:%d:%d", &t[0], &t[1], &t[2]);

    // hh:mm:ss を 秒に変換
    int S = s[2] + 60 * (s[1] + s[0] * 60);
    int T = t[2] + 60 * (t[1] + t[0] * 60);

    // 入る時間に +1
    imos[S]++;

    // 出る時間に -1
    imos[T]--;

    // ナイーブな実装 (写経しなくて良い)
    // for(int j = S; j <= T; j++) imos[j]++;
  }

  // 累積和を計算する
  rep(i, 86400) if (i) imos[i] += imos[i - 1];

  int ans = 0;
  rep(i, 86400) ans = max(ans, imos[i]);

  cout << ans << endl;
}

int main() {
  // こう書くとn==0のときにwhileループが停止する
  while (cin >> n, n) {
    rep(i, 86400) imos[i] = 0;
    solve();
  }
}
Problem p01138 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _                                                                      \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

const int MAX_N = 1000;

pair<int, int> p[MAX_N + 1];

int main() {
  _;
  int n, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
  while (scanf("%d", &n), n != 0) {
    REP(i, n) {
      scanf("%02d:%02d:%02d %02d:%02d:%02d", &a, &b, &c, &d, &e, &f);
      p[i] = make_pair(a * 3600 + b * 60 + c, d * 3600 + e * 60 + f);
    }
    priority_queue<int, vector<int>, greater<int>> que;
    int maxTrain = 0;
    sort(p, p + n);
    REP(i, n) {
      while (!que.empty() && que.top() <= p[i].first)
        que.pop();
      que.push(p[i].second);
      maxTrain = max(maxTrain, (int)que.size());
    }
    cout << maxTrain << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define _                                                                      \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

const int MAX_N = 10000;

pair<int, int> p[MAX_N + 1];

int main() {
  _;
  int n, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
  while (scanf("%d", &n), n != 0) {
    REP(i, n) {
      scanf("%02d:%02d:%02d %02d:%02d:%02d", &a, &b, &c, &d, &e, &f);
      p[i] = make_pair(a * 3600 + b * 60 + c, d * 3600 + e * 60 + f);
    }
    priority_queue<int, vector<int>, greater<int>> que;
    int maxTrain = 0;
    sort(p, p + n);
    REP(i, n) {
      while (!que.empty() && que.top() <= p[i].first)
        que.pop();
      que.push(p[i].second);
      maxTrain = max(maxTrain, (int)que.size());
    }
    cout << maxTrain << endl;
  }
}
Problem p01138 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

#define secondMAX (23 * 60 * 59 + 60 * 60 + 59)
using namespace std;

int main() {

  int n;
  int h, m, s;
  char gav;
  int table[secondMAX + 1];
  int max;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i <= secondMAX; i++)
      table[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> h >> gav >> m >> gav >> s;
      table[h * 60 * 60 + m * 60 + s] += 1;
      cin >> h >> gav >> m >> gav >> s;
      table[h * 60 * 60 + m * 60 + s] -= 1;
    }
    for (int i = 1; i <= secondMAX; i++)
      table[i] += table[i - 1];
    max = 0;
    for (int i = 0; i <= secondMAX; i++) {
      if (max < table[i])
        max = table[i];
    }

    cout << max << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

#define secondMAX (23 * 60 * 60 + 59 * 60 + 59)
using namespace std;

int main() {

  int n;
  int h, m, s;
  char gav;
  int table[secondMAX + 1];
  int max;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i <= secondMAX; i++)
      table[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> h >> gav >> m >> gav >> s;
      table[h * 60 * 60 + m * 60 + s] += 1;
      cin >> h >> gav >> m >> gav >> s;
      table[h * 60 * 60 + m * 60 + s] -= 1;
    }
    for (int i = 1; i <= secondMAX; i++)
      table[i] += table[i - 1];
    max = 0;
    for (int i = 0; i <= secondMAX; i++) {
      if (max < table[i])
        max = table[i];
    }

    cout << max << endl;
  }

  return 0;
}
Problem p01139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int w, h;
vector<string> ss;
bool is_white[103][103];
bool is_black[103][103];

void dfs(int x_, int y_, bool (*f_)[103], bool f2) {
  // cout<<x_<<","<<y_<<endl;
  if (f_[x_][y_] || (ss[x_][y_] != '.' && f2))
    return;
  f_[x_][y_] = true;
  rep(i, 4) {
    int x = x_ + dx[i];
    int y = y_ + dy[i];
    if (0 <= x && x < ss.size() && 0 <= y && y < ss[x].size()) {
      dfs(x, y, f_, true);
    }
  }
}

void solve() {
  rep(i, 103) rep(j, 103) is_white[i][j] = is_black[i][j] = false;
  ss.resize(w);
  rep(i, h) {
    string t;
    cin >> t;
    ss[i] = t;
  }
  int ans_b = 0, ans_w = 0;
  rep(i, ss.size()) {
    rep(j, ss[i].size()) {
      if (ss[i][j] == 'B')
        dfs(i, j, is_black, false);
      if (ss[i][j] == 'W')
        dfs(i, j, is_white, false);
    }
  }
  rep(i, ss.size()) rep(j, ss[i].size()) {
    // cout<<i<<","<<j<<":"<<is_white[i][j]<<","<<is_black[i][j]<<ss[i][j]<<endl;
    if (ss[i][j] == '.' && (is_white[i][j] ^ is_black[i][j])) {
      if (is_white[i][j])
        ans_w++;
      else
        ans_b++;
    }
  }
  cout << ans_b << " " << ans_w << endl;
  ss.clear();
}

int main() {
  while (cin >> w >> h) {
    if (w + h == 0)
      return 0;
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int w, h;
vector<string> ss;
bool is_white[103][103];
bool is_black[103][103];

void dfs(int x_, int y_, bool (*f_)[103], bool f2) {
  // cout<<x_<<","<<y_<<endl;
  if (f_[x_][y_] || (ss[x_][y_] != '.' && f2))
    return;
  f_[x_][y_] = true;
  rep(i, 4) {
    int x = x_ + dx[i];
    int y = y_ + dy[i];
    if (0 <= x && x < ss.size() && 0 <= y && y < ss[x].size()) {
      dfs(x, y, f_, true);
    }
  }
}

void solve() {
  rep(i, 103) rep(j, 103) is_white[i][j] = is_black[i][j] = false;
  ss.resize(h);
  rep(i, h) {
    string t;
    cin >> t;
    ss[i] = t;
  }
  int ans_b = 0, ans_w = 0;
  rep(i, ss.size()) {
    rep(j, ss[i].size()) {
      if (ss[i][j] == 'B')
        dfs(i, j, is_black, false);
      if (ss[i][j] == 'W')
        dfs(i, j, is_white, false);
    }
  }
  rep(i, ss.size()) rep(j, ss[i].size()) {
    // cout<<i<<","<<j<<":"<<is_white[i][j]<<","<<is_black[i][j]<<ss[i][j]<<endl;
    if (ss[i][j] == '.' && (is_white[i][j] ^ is_black[i][j])) {
      if (is_white[i][j])
        ans_w++;
      else
        ans_b++;
    }
  }
  cout << ans_b << " " << ans_w << endl;
  ss.clear();
}

int main() {
  while (cin >> w >> h) {
    if (w + h == 0)
      return 0;
    solve();
  }

  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  int w[1500], h[1500];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    scanf("%d", &w[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &h[i]);
  while (n) {
    vector<int> x;
    vector<int> y;
    for (int i = 0; i < n; i++) {
      int a = 0;
      for (int j = i; j < n; j++) {
        a += w[j];
        x[a]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int b = 0;
      for (int j = i; j < m; j++) {
        b += h[j];
        y[b]++;
      }
    }

    int ave = 0;
    for (int i = 0; i < 1500 * 1000; i++) {
      // if(y[i]!=0)cout << i << " " << y[i] << " / ";
      ave += x[i] * y[i];
    }
    cout << ave << endl;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      scanf("%d", &h[i]);
  }
  return 0;
}


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

int main() {
  int n, m;
  int w[1500], h[1500];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    scanf("%d", &w[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &h[i]);
  while (n) {
    vector<int> x(1501 * 1001);
    vector<int> y(1501 * 1001);
    for (int i = 0; i < n; i++) {
      int a = 0;
      for (int j = i; j < n; j++) {
        a += w[j];
        x[a]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int b = 0;
      for (int j = i; j < m; j++) {
        b += h[j];
        y[b]++;
      }
    }

    int ave = 0;
    for (int i = 0; i < 1500 * 1000; i++) {
      // if(y[i]!=0)cout << i << " " << y[i] << " / ";
      ave += x[i] * y[i];
    }
    cout << ave << endl;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      scanf("%d", &h[i]);
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[1600] = {};
    short num2[1600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[10600] = {};
    int num2[10600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (int i = a; i < x; i++)

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && M && N) {
    int h[10000] = {};
    int w[10000] = {};
    unsigned long long sum_h[10000] = {};
    unsigned long long sum_w[10000] = {};
    long long h_c[225000] = {};
    long long w_c[225000] = {};

    rep(i, N) {
      cin >> h[i];
      sum_h[i + 1] = sum_h[i] + h[i];
    }
    rep(i, M) {
      cin >> w[i];
      sum_w[i + 1] = sum_w[i] + w[i];
    }

    rep(i, N) rep(j, N - i) h_c[sum_h[j + i + 1] - sum_h[j]]++;
    rep(i, M) rep(j, M - i) w_c[sum_w[j + i + 1] - sum_w[j]]++;

    long long count = 0;
    rep(i, min(sum_h[N], sum_w[M]) + 1) count += h_c[i] * w_c[i];
    cout << count << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (int i = a; i < x; i++)

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && M && N) {
    int h[10000] = {};
    int w[10000] = {};
    unsigned long long sum_h[10000] = {};
    unsigned long long sum_w[10000] = {};
    long long h_c[510000] = {};
    long long w_c[500000] = {};

    rep(i, N) {
      cin >> h[i];
      sum_h[i + 1] = sum_h[i] + h[i];
    }
    rep(i, M) {
      cin >> w[i];
      sum_w[i + 1] = sum_w[i] + w[i];
    }

    rep(i, N) rep(j, N - i) h_c[sum_h[j + i + 1] - sum_h[j]]++;
    rep(i, M) rep(j, M - i) w_c[sum_w[j + i + 1] - sum_w[j]]++;

    long long count = 0;
    rep(i, min(sum_h[N], sum_w[M]) + 1) count += h_c[i] * w_c[i];
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
int main() {

  for (int n, m; cin >> n >> m, n && m;) {
    int h[1500], w[1500], a0[MAX + 10] = {}, a1[MAX + 10] = {};
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < n - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += h[j + k];
        a0[r]++;
      }
    for (int i = 1; i <= m; i++)
      for (int j = 0; j < m - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += w[j + k];
        a1[r]++;
      }
    int r = 0;
    for (int i = 1; i <= MAX; i++)
      r += a0[i] * a1[i];
    cout << r << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1500000;
int main() {

  for (int n, m; cin >> n >> m, n && m;) {
    int h[1500], w[1500], a0[MAX + 10] = {}, a1[MAX + 10] = {};
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < n - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += h[j + k];
        a0[r]++;
      }
    for (int i = 1; i <= m; i++)
      for (int j = 0; j < m - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += w[j + k];
        a1[r]++;
      }
    int r = 0;
    for (int i = 1; i <= MAX; i++)
      r += a0[i] * a1[i];
    cout << r << endl;
  }
  return 0;
}
Problem p01140 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter

while True:
    N, M = map(int, input().split())
    if N == 0 and M == 0:
        break
    H = [int(input()) for _ in range(N)]
    W = [int(input()) for _ in range(M)]
    all_H = Counter(
        sorted([sum(H[i:j]) for i in range(N) for j in range(i + 1, N + 1)])
    )
    all_W = Counter(
        sorted([sum(W[i:j]) for i in range(M) for j in range(i + 1, M + 1)])
    )
    ans = 0
    for k, v in all_H.items():
        if k in all_H:
            ans += v * all_W[k]
    print(ans)


Fixed submission:
from collections import Counter

while True:
    N, M = map(int, input().split())
    if N == 0 and M == 0:
        break
    H = [int(input()) for _ in range(N)]
    W = [int(input()) for _ in range(M)]

    all_H = []
    for i in range(N):
        s = 0
        for j in range(i, N):
            s += H[j]
            all_H.append(s)

    all_W = []
    for i in range(M):
        s = 0
        for j in range(i, M):
            s += W[j]
            all_W.append(s)

    all_H = Counter(all_H)
    all_W = Counter(all_W)
    ans = 0
    for k, v in all_H.items():
        if k in all_H:
            ans += v * all_W[k]
    print(ans)
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int w, h;
  cin >> w >> h;
  int wl[1501] = {}, hl[1501] = {};
  int wb, hb;
  int i, j, k;
  while (w != 0) {
    for (i = 0; i < w; i++) {
      cin >> wb;
      wl[i + 1] = wl[i] + wb;
    }
    for (i = 0; i < h; i++) {
      cin >> hb;
      hl[i + 1] = hl[i] + hb;
    }
    vector<int> ws, hs;

    for (i = 0; i <= w; i++) {
      for (j = i + 1; j <= w; j++) {
        ws.push_back(wl[j] - wl[i]);
      }
    }
    for (i = 0; i <= h; i++) {
      for (j = i + 1; j <= h; j++) {
        hs.push_back(hl[j] - hl[i]);
      }
    }

    sort(ws.begin(), ws.end());
    sort(hs.begin(), hs.end());
    /*
    for(i=0;i<ws.size();i++) cout << ws[i] << ",";
    cout << endl;
    for(i=0;i<hs.size();i++) cout << hs[i] << ",";
    cout << endl;
    */
    int wc = 0, hc = 0;
    int x, y;
    int o = 0;
    while (!(wc + 1 >= ws.size() && hc + 1 >= hs.size())) {

      if (ws[wc] == hs[hc]) {
        x = 0;
        y = 0;
        while (ws[wc + x] == ws[wc] && wc + x < ws.size())
          x++;
        while (hs[hc + y] == hs[hc] && hc + y < hs.size())
          y++;
        // cout << x*y << endl;
        o += x * y;
        wc += x;
        hc += y;
      } else if (ws[wc] > hs[hc]) {
        while (ws[wc] > hs[hc] && hc < hs.size())
          hc++;
      } else {
        while (ws[wc] < hs[hc] && wc < ws.size())
          wc++;
      }
      // cout << wc<<":"<<hc<<endl;
    }
    cout << o << endl;

    cin >> w >> h;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  int w, h;
  cin >> w >> h;
  int wl[1501] = {}, hl[1501] = {};
  int wb, hb;
  int i, j, k;
  while (w != 0) {
    for (i = 0; i < w; i++) {
      cin >> wb;
      wl[i + 1] = wl[i] + wb;
    }
    for (i = 0; i < h; i++) {
      cin >> hb;
      hl[i + 1] = hl[i] + hb;
    }
    vector<int> ws, hs;

    for (i = 0; i <= w; i++) {
      for (j = i + 1; j <= w; j++) {
        ws.push_back(wl[j] - wl[i]);
      }
    }
    for (i = 0; i <= h; i++) {
      for (j = i + 1; j <= h; j++) {
        hs.push_back(hl[j] - hl[i]);
      }
    }

    sort(ws.begin(), ws.end());
    sort(hs.begin(), hs.end());
    /*
    for(i=0;i<ws.size();i++) cout << ws[i] << ",";
    cout << endl;
    for(i=0;i<hs.size();i++) cout << hs[i] << ",";
    cout << endl;
    */
    int wc = 0, hc = 0;
    int x, y;
    int o = 0;
    while (!(wc >= ws.size() || hc >= hs.size())) {

      if (ws[wc] == hs[hc]) {
        x = 0;
        y = 0;
        while (ws[wc + x] == ws[wc] && wc + x < ws.size())
          x++;
        while (hs[hc + y] == hs[hc] && hc + y < hs.size())
          y++;
        // cout << x*y << endl;
        o += x * y;
        wc += x;
        hc += y;
      } else if (ws[wc] > hs[hc]) {
        while (ws[wc] > hs[hc] && hc < hs.size())
          hc++;
      } else {
        while (ws[wc] < hs[hc] && wc < ws.size())
          wc++;
      }
      // cout << wc<<":"<<hc<<endl;
    }
    cout << o << endl;

    cin >> w >> h;
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int w[1501] = {0}, h[1501] = {0};
int W[2250000] = {0}, H[2250000] = {0};

int sum(int *a, int i, int n, int size) {
  int s = 0;
  for (int j = n; j--;) {
    if (i >= size) {
      return 0;
    }
    s += a[i++];
  }
  return s;
}

int main() {
  int n, m, w_, h_;
  while (cin >> n >> m, n || m) {
    // int cnt_h[1001] = {0}
    int cnt_w[100000] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int i_ = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = 0; i + j <= n; i++) {
        H[i_++] = sum(h, i, j, n);
      }
    }
    /*for(int i=0 ; i<i_ ; i++ ){
            cnt_h[ H[i] ]++;
    }*/

    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int j_ = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 0; i + j <= m; i++) {
        W[j_++] = sum(w, i, j, m);
      }
    }
    for (int i = 0; i < j_; i++) {
      cnt_w[W[i]] += 1;
    }
    /*for(int i=0 ; i<j_ ; i++ ){
            cout << "W[" << i << "] : " << W[i] << endl;
    }*/

    int ans = 0;
    for (int i = 0; i < i_; i++) {
      // if( H[i] <= 1000 && H[i] >= 0 )
      ans += cnt_w[H[i]];
      // for(int j=0 ; j < j_ ; j++ ){
      //	if( W[j] == H[i] ) ans++;
      // }
    }
    // cout << "ans : " << ans << endl;
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int w[1501] = {0}, h[1501] = {0};
int W[2250000] = {0}, H[2250000] = {0};

int sum(int *a, int i, int n, int size) {
  int s = 0;
  for (int j = n; j--;) {
    if (i >= size) {
      return 0;
    }
    s += a[i++];
  }
  return s;
}

int main() {
  int n, m, w_, h_;
  while (cin >> n >> m, n || m) {
    // int cnt_h[1001] = {0}
    int cnt_w[1000000] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int i_ = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = 0; i + j <= n; i++) {
        H[i_++] = sum(h, i, j, n);
      }
    }
    /*for(int i=0 ; i<i_ ; i++ ){
            cnt_h[ H[i] ]++;
    }*/

    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int j_ = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 0; i + j <= m; i++) {
        W[j_++] = sum(w, i, j, m);
      }
    }
    for (int i = 0; i < j_; i++) {
      cnt_w[W[i]] += 1;
    }
    /*for(int i=0 ; i<j_ ; i++ ){
            cout << "W[" << i << "] : " << W[i] << endl;
    }*/

    int ans = 0;
    for (int i = 0; i < i_; i++) {
      // if( H[i] <= 1000 && H[i] >= 0 )
      ans += cnt_w[H[i]];
      // for(int j=0 ; j < j_ ; j++ ){
      //	if( W[j] == H[i] ) ans++;
      // }
    }
    // cout << "ans : " << ans << endl;
    cout << ans << endl;
  }
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int H, W;

  while (cin >> H >> W, H + W) {
    int h[1001] = {};
    int w[1001] = {};
    map<int, int> setH;
    map<int, int> setW;

    for (int i = 1; i <= H; i++)
      cin >> h[i];
    for (int i = 1; i <= W; i++)
      cin >> w[i];

    for (int i = 1; i <= H; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= W; i++)
      w[i] += w[i - 1];

    for (int i = 0; i < H; i++) {
      for (int j = i + 1; j <= H; j++) {
        int t = h[j] - h[i];
        if (setH.count(t))
          setH[t]++;
        else
          setH[t] = 1;
      }
    }
    for (int i = 0; i < W; i++) {
      for (int j = i + 1; j <= W; j++) {
        int t = w[j] - w[i];
        if (setW.count(t))
          setW[t]++;
        else
          setW[t] = 1;
      }
    }

    int cnt = 0;
    for (auto i : setH) {
      if (setW.count(i.first))
        cnt += setW[i.first] * i.second;
    }

    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int H, W;

  while (cin >> H >> W, H + W) {
    int h[1501] = {};
    int w[1501] = {};
    map<int, int> setH;
    map<int, int> setW;

    for (int i = 1; i <= H; i++)
      cin >> h[i];
    for (int i = 1; i <= W; i++)
      cin >> w[i];

    for (int i = 1; i <= H; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= W; i++)
      w[i] += w[i - 1];

    for (int i = 0; i < H; i++) {
      for (int j = i + 1; j <= H; j++) {
        int t = h[j] - h[i];
        if (setH.count(t))
          setH[t]++;
        else
          setH[t] = 1;
      }
    }
    for (int i = 0; i < W; i++) {
      for (int j = i + 1; j <= W; j++) {
        int t = w[j] - w[i];
        if (setW.count(t))
          setW[t]++;
        else
          setW[t] = 1;
      }
    }

    int cnt = 0;
    for (auto i : setH) {
      if (setW.count(i.first))
        cnt += setW[i.first] * i.second;
    }

    cout << cnt << endl;
  }

  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[150001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[1500001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\n", count);
  }
  return 0;
}