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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define maxn 50005
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define eps 1e-6
const double pi = acos(-1.0);
typedef long long ll;
using namespace std;

int n, w;
int len[maxn], sum[maxn];
bool dp[maxn];

bool isok(int mid) {
  int i, j, last = 0;
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  if (sum[n] + n - 1 <= w)
    return true;
  for (i = 0; i < n - 1; i++) {
    if (!dp[i])
      continue;
    for (j = max(i + 2, last); j <= n; j++) {
      if (w < sum[j] - sum[i] + j - i - 1)
        break;
      if (w > sum[j] - sum[i] + ll(j - i - 1) * mid)
        continue;
      last = j;
      dp[j] = 1;
      if (sum[n] - sum[j] + n - j - 1 <= w)
        return true;
    }
  }
  return false;
}
void solve() {
  int i, j, le = 1, ri = w, mid, ans;
  while (le <= ri) {
    mid = (le + ri) >> 1;
    if (isok(mid)) {
      ans = mid;
      ri = mid - 1;
    } else
      le = mid + 1;
  }
  printf("%d\\n", ans);
}
int main() {
  int i, j;
  while (~scanf("%d%d", &w, &n)) {
    if (w == 0 && n == 0)
      break;
    sum[0] = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &len[i]);
      sum[i] = sum[i - 1] + len[i];
    }
    solve();
  }
  return 0;
}
/*
11 4
4 2 1 3
5 7
1 1 1 2 2 1 2
11 7
3 1 3 1 3 3 4
100 3
30 30 39
30 3
2 5 3
0 0
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define maxn 50005
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define eps 1e-6
const double pi = acos(-1.0);
typedef long long ll;
using namespace std;

int n, w;
int len[maxn], sum[maxn];
bool dp[maxn];

bool isok(int mid) {
  int i, j, last = 0;
  bool flg;
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  if (sum[n] + n - 1 <= w)
    return true;
  for (i = 0; i < n - 1; i++) {
    if (!dp[i])
      continue;
    flg = 0;
    for (j = max(i + 2, last); j <= n; j++) {
      if (w < sum[j] - sum[i] + j - i - 1)
        break;
      if (w > sum[j] - sum[i] + ll(j - i - 1) * mid)
        continue;
      if (flg == 0)
        flg = 1, last = j;
      dp[j] = 1;
      if (sum[n] - sum[j] + n - j - 1 <= w)
        return true;
    }
  }
  return false;
}
void solve() {
  int i, j, le = 1, ri = w, mid, ans;
  while (le <= ri) {
    mid = (le + ri) >> 1;
    if (isok(mid)) {
      ans = mid;
      ri = mid - 1;
    } else
      le = mid + 1;
  }
  printf("%d\\n", ans);
}
int main() {
  int i, j;
  while (~scanf("%d%d", &w, &n)) {
    if (w == 0 && n == 0)
      break;
    sum[0] = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &len[i]);
      sum[i] = sum[i - 1] + len[i];
    }
    solve();
  }
  return 0;
}
/*
11 4
4 2 1 3
5 7
1 1 1 2 2 1 2
11 7
3 1 3 1 3 3 4
100 3
30 30 39
30 3
2 5 3
0 0
*/
Problem p00912 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAXN = 50005;

int W, N;
int x[MAXN];
bool dp[MAXN];

bool test(int w) {
  int a, b;
  int sumA, numA, sumB, numB;
  fill(dp, dp + MAXN, false);
  dp[0] = true;
  a = b = 0;
  sumA = numA = sumB = numB = 0;

  for (int i = 0; i < N; ++i) {
    for (; a < N && W - sumA > w * (numA - 1); ++numA, sumA += x[a++])
      ;
    for (; b < N && W - sumB >= (numB - 1); ++numB, sumB += x[b++])
      ;
    if (!(W - sumB >= (numB - 1))) {
      sumB -= x[--b];
      --numB;
    }

    if (dp[i]) {
      for (int j = a; j <= b; ++j) {
        dp[j] = true;
      }
      a = b;
      sumA = sumB;
      numA = numB;
    }

    sumA -= x[i];
    sumB -= x[i];
    numA -= 1;
    numB -= 1;
  }
  return dp[N];
}

int main() {
  while (cin >> W >> N && (W | N)) {
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    int l, r;
    l = 1;
    r = W + 1;
    while (r - l) {
      int m = (l + r) / 2;
      if (test(m)) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    cout << l << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int MAXW = 80008;
const int MAXN = 50005;

int W, N;
int x[MAXN];
bool dp[MAXN];

bool test(int w) {
  int a, b;
  int sumA, numA, sumB, numB;
  fill(dp, dp + MAXN, false);
  dp[0] = true;
  a = b = 0;
  sumA = numA = sumB = numB = 0;
  // cout << "w=" << w << endl;
  for (int i = 0; i < N; ++i) {
    for (; a < N && W - sumA > w * (numA - 1); ++numA, sumA += x[a++])
      ;
    for (; b < N && W - sumB >= (numB - 1); ++numB, sumB += x[b++])
      ;
    if (!(W - sumB >= numB)) {
      sumB -= x[--b];
      --numB;
    }

    if (dp[i]) {
      for (int j = a; j <= b; ++j) {
        dp[j] = true;
      }
    }

    sumA -= x[i];
    sumB -= x[i];
    numA -= 1;
    numB -= 1;
  }
  return dp[N];
}

int main() {
  while (cin >> W >> N && (W | N)) {
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    int l, r;
    l = 1;
    r = W + 1;
    while (r - l) {
      int m = (l + r) / 2;
      if (test(m)) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    cout << l << endl;
  }
  return 0;
}
Problem p00912 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  for (int W, N; cin >> W >> N, W | N;) {
    int x[52345];
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    int l = 0, h = 1e9;
    while (h - l > 1) {
      int m = (h + l) / 2;
      vector<pair<int, int>> v[52345];
      bool p[52345] = {};
      v[0].emplace_back(0, 1);
      for (int i = 0; i < N; i++) {
        v[i + 1].reserve(v[i].size());
        bool f = false;
        for (auto e : v[i]) {
          p[i] |= e.first + x[i] <= W;
          int nl = e.first + x[i] + 1;
          if (nl < W) {
            int nh = min(W, e.second + x[i] + m);
            if (!v[i + 1].empty() && v[i + 1].back().second >= nl) {
              v[i + 1].back().second = nh;
            } else {
              v[i + 1].emplace_back(nl, nh);
            }
          }
          f |= e.first <= W - x[i] && W - x[i] < e.second;
        }
        if (f) {
          v[i + 1].insert(v[i + 1].begin(), make_pair(0, 1));
        }
      }
      (!p[N - 1] ? l : h) = m;
    }
    cout << h << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  for (int W, N; cin >> W >> N, W | N;) {
    int x[52345];
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    int l = 0, h = 1e9;
    while (h - l > 1) {
      int m = (h + l) / 2;
      vector<pair<int, int>> v[52345];
      bool p[52345] = {};
      v[0].emplace_back(0, 1);
      for (int i = 0; i < N; i++) {
        bool f = false;
        for (auto e : v[i]) {
          p[i] |= e.first + x[i] <= W;
          int nl = e.first + x[i] + 1;
          if (nl < W) {
            int nh = min(W, e.second + x[i] + m);
            if (!v[i + 1].empty() && v[i + 1].back().second >= nl) {
              v[i + 1].back().second = nh;
            } else {
              v[i + 1].emplace_back(nl, nh);
            }
          }
          f |= e.first <= W - x[i] && W - x[i] < e.second;
        }
        if (f) {
          v[i + 1].insert(v[i + 1].begin(), make_pair(0, 1));
        }
      }
      (!p[N - 1] ? l : h) = m;
    }
    cout << h << endl;
  }
}
Problem p00912 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct SegmentTree {
  vector<int> seg;
  int sz;

  SegmentTree(int n) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz - 1, INF);
  }

  int rmq(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (INF);
    if (a <= l && r <= b)
      return (seg[k]);
    return (min(rmq(a, b, 2 * k + 1, l, (l + r) >> 1),
                rmq(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  int rmq(int a, int b) { return (rmq(a, b, 0, 0, sz)); }

  void update(int k, int x) {
    k += sz - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      seg[k] = min(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }
};

int main() {
  int W, N, x[50001] = {};

  while (scanf("%d %d", &W, &N), W) {
    for (int i = 1; i <= N; i++) {
      scanf("%d", x + i);
      x[i] += x[i - 1];
    }

    auto getSpace = [&](int l, int r) {
      if (l + 1 >= r) {
        cout << l << " " << r << endl;

        puts("foo");
        return (114514);
      }
      if (x[r] - x[l] + r - l - 1 > W)
        return (INF);
      return ((W + x[l] + r - x[r] - l - 2) / (r - l - 1));
    };

    int low = 1, high = W;
    while (high - low > 0) {
      int limit = (low + high) >> 1;

      int left = 0, right = 0;
      SegmentTree dp(N);
      dp.update(0, limit);
      for (int i = 2; i < N; i++) {
        while (i - left >= 2 && getSpace(left, i) == INF)
          ++left;
        while (i - right >= 2 &&
               (getSpace(right, i) == INF || getSpace(right, i) <= limit))
          ++right;
        dp.update(i, dp.rmq(left, right));
      }
      int tail = N - 2;
      bool flag = dp.rmq(N - 1, N) <= limit;
      while (tail >= 0 && getSpace(tail, N) != INF) {
        flag |= dp.rmq(tail, tail + 1) <= limit;
        --tail;
      }
      if (flag)
        high = limit;
      else
        low = limit + 1;
    }
    printf("%d\\n", low);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int W, N, x[50000] = {};
  int dp[50000];

  while (scanf("%d %d", &W, &N), W) {
    for (int i = 1; i <= N; i++) {
      scanf("%d", x + i);
      x[i] += x[i - 1];
    }

    auto getSpace = [&](int l, int r) {
      if (x[r] - x[l] + r - l - 1 > W)
        return (INF);
      return ((W + x[l] + r - x[r] - l - 2) / (r - l - 1));
    };

    fill_n(dp, N + 1, INF);
    dp[0] = 0;
    for (int i = 1; i < N; i++) {
      for (int j = i - 2; j >= 0; j--) {
        int vv = getSpace(j, i);
        if (vv == INF)
          break;
        if (dp[j] > dp[i])
          break;
        dp[i] = min(dp[i], max(dp[j], getSpace(j, i)));
      }
    }

    int ret = dp[N - 1], tail = N - 2;
    while (tail >= 0 && getSpace(tail, N) != INF) {
      ret = min(ret, dp[tail]);
      --tail;
    }
    printf("%d\\n", max(1, ret));
  }
}
Problem p00914 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int x[100][1000][150], a, b, c, i, j, k, l, sum;
int main() {
  while (true) {
    sum = 0;
    memset(x, 0, sizeof(x));
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    x[0][0][0] = 1;
    for (i = 1; i <= b; i++) {
      for (j = 0; j <= c; j++) {
        for (l = 0; l < a; l++) {
          for (k = l + 1; k <= a; k++) {
            x[i][j + k][k] += x[i - 1][j][l];
          }
        }
      }
    }
    for (i = 0; i <= a; i++) {
      sum += x[b][c][i];
    }
    cout << sum << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int x[100][1000][250], a, b, c, i, j, k, l, sum;
int main() {
  while (true) {
    sum = 0;
    memset(x, 0, sizeof(x));
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    x[0][0][0] = 1;
    for (i = 1; i <= b; i++) {
      for (j = 0; j <= c; j++) {
        for (l = 0; l < a; l++) {
          for (k = l + 1; k <= a; k++) {
            x[i][j + k][k] += x[i - 1][j][l];
          }
        }
      }
    }
    for (i = 0; i <= a; i++) {
      sum += x[b][c][i];
    }
    cout << sum << endl;
  }
}
Problem p00914 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    int n, k, s;
    cin >> n >> k >> s;
    if (n == 0 && k == 0 && s == 0)
      break;
    int cnt = 0;
    for (int i = 0; i < 1 << n; i++) {
      int c = 0, ss = 0, ii = i;
      while (ii != 0) {
        int x = ii & -ii;
        ii ^= x;
        ss += __builtin_ffs(x);
        c++;
      }
      if (c == k && s == ss)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    int n, k, s;
    cin >> n >> k >> s;
    if (n == 0 && k == 0 && s == 0)
      break;
    int cnt = 0;
    for (int i = 0; i < 1 << n; i++) {
      int c = 0, ss = 0;
      while (i != 0) {
        int x = i & -i;
        i ^= x;
        ss += __builtin_ffs(x);
        c++;
      }
      if (c == k && s == ss)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00914 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k, s, i, c, res;
  while (1) {
    scanf("%d", &n);
    scanf("%d", &k);
    scanf("%d", &s);
    if (!n && !k && !s) {
      break;
    }
    int t[n], cd[n];
    for (i = 0; i < n; i++) {
      cd[i] = i + 1;
      t[i] = 0;
    }
    for (i = 0; i < k; i++) {
      t[n - i - 1] = 1;
    }
    res = 0;
    do {
      c = 0;
      for (i = 0; i < n; i++) {
        c += (t[i] * cd[i]);
      }
      if (c == s) {
        res++;
      }
    } while (next_permutation(t, t + n));
    printf("%d\\n", res);
  }
  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

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

int main() {
  int n, k, s, i, c, res;
  while (1) {
    scanf("%d", &n);
    scanf("%d", &k);
    scanf("%d", &s);
    if (!n && !k && !s) {
      break;
    }
    vector<int> t(n, 0);
    int cd[n];
    for (i = 0; i < n; i++) {
      cd[i] = i + 1;
    }
    for (i = 0; i < k; i++) {
      t[n - i - 1] = 1;
    }
    res = 0;
    do {
      c = 0;
      for (i = 0; i < n; i++) {
        c += (t[i] * cd[i]);
      }
      if (c == s) {
        res++;
      }
    } while (next_permutation(t.begin(), t.end()));
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00914 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n, s, t;
  int num;
  while (scanf("%d%d%d", &n, &s, &t) && (n + s + t)) {
    num = 0;
    for (int i = 1; i <= (1 << n); i++) {
      int sum = 0, res = 0;
      for (int j = 1; j <= n; j++) {
        if (i & (1 << (j - 1))) {
          res++;
          sum += j;
        }
      }
      if (res == s && sum == t)
        num++;
    }
    cout << num << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct NODE {
  int x, v;
  void rd() {
    char str[5];
    scanf("%s", str);
    str[0] == 'R' ? v = 1 : v = -1;
    scanf("%d", &x);
  }
  void work() { x += v; }
} ant[33];

int main() {
  // freopen("1.txt","r",stdin);
  int n, m;
  while (cin >> n >> m, n || m) {
    for (int i = 1; i <= n; i++)
      ant[i].rd();

    int cnt = 0, ti = 0;
    int ans1, ans2;

    while (cnt < n) {
      ti++;
      for (int i = 1; i <= n; i++)
        ant[i].work();
      for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
          if (ant[i].x == ant[j].x)
            swap(ant[i].v, ant[j].v);
      for (int i = 1; i <= n; i++)
        if (ant[i].x == m) {
          ans1 = i, ans2 = ti, cnt++;
        }
      for (int i = 1; i <= n; i++)
        if (ant[i].x == 0) {
          ans1 = i, ans2 = ti, cnt++;
        }
    }

    cout << ans2 << ' ' << ans1 << endl;
  }
  return 0;
}
Problem p00915 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int num_ant, length;

  while (1) {
    std::cin >> num_ant >> length;

    if (num_ant == 0 && length == 0)
      break;

    int direction[num_ant];
    int position[num_ant];
    char temp;
    int tag;

    // input
    for (int i = 0; i < num_ant; i++) {
      std::cin >> temp >> position[i];
      if (temp == 'R')
        direction[i] = 1;
      else
        direction[i] = -1;
    }

    // simulate
    bool goal[110] = {0};
    bool flag = false;
    int t = 0;

    while (!flag) {
      t++;
      int conflict[110] = {0};
      int point[110] = {0};
      flag = true;

      for (int j = 0; j < num_ant; j++) {
        if (goal[j] == false) {
          position[j] += direction[j];

          if (conflict[position[j]] != 0) {
            direction[j] *= -1;
            direction[point[position[j]]] *= -1;
          } else {
            conflict[position[j]]++;
            point[position[j]] = j;
          }

          if (position[j] == 0 || position[j] == length) {
            goal[j] = true;
            if (position[j] == 0) {
              tag = j;
            } else if (position[j] == length && conflict[0] == 0) {
              tag = j;
            }
          }
        }
      }

      // confirming
      for (int k = 0; k < num_ant; k++) {
        if (goal[k] != true)
          flag = false;
      }
    }

    std::cout << t << " " << tag + 1 << std::endl;
  }

  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int num_ant, length;

  while (1) {
    std::cin >> num_ant >> length;

    if (num_ant == 0 && length == 0)
      break;

    char temp;
    int position[num_ant];
    int direction[num_ant];

    // input
    for (int i = 0; i < num_ant; i++) {
      std::cin >> temp >> position[i];
      if (temp == 'R')
        direction[i] = 1;
      else
        direction[i] = -1;
    }

    // simulation
    int t = 0;
    int tag;
    bool flag = false;
    while (!flag) {
      t++;
      int dummy[220] = {0};
      // int conflict[110] = {0};
      int *conflict = &dummy[110];
      // int point[110] = {0};
      int *point = &dummy[110];
      flag = true;

      for (int i = 0; i < num_ant; i++) {
        // renew position
        position[i] += direction[i];

        // ditecting conflict
        if (conflict[position[i]] != 0) {
          direction[i] *= -1;
          direction[point[position[i]]] *= -1;
        } else if (0 < position[i] && position[i] < length) {
          conflict[position[i]]++;
          point[conflict[position[i]]] = i;
        }
      }

      // confirming
      for (int j = 0; j < num_ant; j++) {
        if (0 < position[j] && position[j] < length)
          flag = false;
      }

      if (flag) {
        for (int k = 0; k < num_ant; k++) {
          if (position[k] == 0 || position[k] == length) {
            tag = k;
            break;
          }
        }
      }
    }

    std::cout << t << " " << tag + 1 << std::endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

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

#define MP make_pair

int main() {

  int N, L;
  while (cin >> N >> L && (N | L)) {
    vector<pair<int, int>> v;
    rep(i, N) {
      char d;
      int p;
      cin >> d >> p;
      v.push_back(MP(d == 'R' ? +1 : -1, p));
    }

    int time = 0;
    int ans = -1;
    int remain = N;
    while (remain > 0) {
      int ch[2200] = {};
      int *check = ch + 1100;
      rep(i, N) {
        v[i].second += v[i].first;
        check[v[i].second]++;
      }

      rep(i, N) {
        if (check[v[i].second] == 2) {
          v[i].first *= -1;
        }
      }

      bool f = 0;
      rep(i, N) {
        if (v[i].second == 0) {
          remain--;
          f = 1;
          ans = i + 1;
        }
        if (v[i].second == L) {
          remain--;
          if (!f) {
            ans = i + 1;
          }
        }
      }
      time++;
    }
    cout << time << " " << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

#define MP make_pair

int main() {

  int N, L;
  while (cin >> N >> L && (N | L)) {
    vector<pair<int, int>> v;
    rep(i, N) {
      char d;
      int p;
      cin >> d >> p;
      v.push_back(MP(d == 'R' ? +1 : -1, p));
    }

    int time = 0;
    int ans = -1;
    int remain = N;
    while (remain > 0) {
      int ch[2 * L + 2];
      memset(ch, 0, sizeof ch);
      int *check = ch + L + 1;
      rep(i, N) {
        v[i].second += v[i].first;
        check[v[i].second]++;
      }

      rep(i, N) {
        if (check[v[i].second] == 2) {
          v[i].first *= -1;
        }
      }

      bool f = 0;
      rep(i, N) {
        if (v[i].second == 0) {
          remain--;
          f = 1;
          ans = i + 1;
        }
        if (v[i].second == L) {
          remain--;
          if (!f) {
            ans = i + 1;
          }
        }
      }
      time++;
    }
    cout << time << " " << ans << endl;
  }
}
Problem p00916 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, x[51][4], a[300][300];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
map<int, int> z[2];
void asu(int p) {
  int cnt = 1;
  vector<int> v;
  r(i, n) r(j, 4) if (j % 2 == p) v.push_back(x[i][j]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  r(i, v.size()) z[p][v[i]] = cnt, cnt += 2;
}
void make_G() {
  r(i, n) {
    for (int j = z[0][x[i][0]]; j <= z[0][x[i][2]]; j++)
      a[z[1][x[i][1]]][j] = 1, a[z[1][x[i][3]]][j] = 1;
    for (int j = z[1][x[i][1]]; j <= z[1][x[i][3]]; j++)
      a[j][z[0][x[i][0]]] = 1, a[j][z[0][x[i][2]]] = 1;
  }
}
void bfs(int y, int x) {
  a[y][x] = 1;
  queue<pair<int, int>> q;
  q.push(make_pair(y, x));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    y = p.first, x = p.second;
    r(i, 4) {
      int yy = y + dy[i], xx = x + dx[i];
      if (yy < 0 || xx < 0 || yy >= 300 || xx >= 300)
        continue;
      if (!a[yy][xx]) {
        a[yy][xx] = 1;
        q.push(make_pair(yy, xx));
      }
    }
  }
}
int main() {
  while (cin >> n, n) {
    int ans = 0;
    memset(a, 0, sizeof(a));
    r(i, 2) z[i].clear();
    r(i, n) {
      r(j, 4) cin >> x[i][j];
      if (x[i][0] > x[i][2])
        swap(x[i][0], x[i][2]);
      if (x[i][1] > x[i][3])
        swap(x[i][1], x[i][3]);
    }
    r(i, 2) asu(i);
    make_G();
    r(i, 300) r(j, 300) if (!a[i][j]) ans++, bfs(i, j);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, x[51][4], a[120][120];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
map<int, int> z[2];
void asu(int p) {
  int cnt = 1;
  vector<int> v;
  r(i, n) r(j, 4) if (j % 2 == p) v.push_back(x[i][j]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  r(i, v.size()) z[p][v[i]] = cnt, cnt += 2;
}
void make_G() {
  r(i, n) {
    for (int j = z[0][x[i][0]]; j <= z[0][x[i][2]]; j++)
      a[z[1][x[i][1]]][j] = 1, a[z[1][x[i][3]]][j] = 1;
    for (int j = z[1][x[i][1]]; j <= z[1][x[i][3]]; j++)
      a[j][z[0][x[i][0]]] = 1, a[j][z[0][x[i][2]]] = 1;
  }
}
void dfs(int y, int x) {
  a[y][x] = 1;
  r(i, 4) {
    int yy = y + dy[i], xx = x + dx[i];
    if (yy < 0 || xx < 0 || yy >= 120 || xx >= 120)
      continue;
    if (!a[yy][xx])
      dfs(yy, xx);
  }
}
int main() {
  while (cin >> n, n) {
    int ans = 0;
    memset(a, 0, sizeof(a));
    r(i, 2) z[i].clear();
    r(i, n) {
      r(j, 4) cin >> x[i][j];
      if (x[i][0] > x[i][2])
        swap(x[i][0], x[i][2]);
      if (x[i][1] > x[i][3])
        swap(x[i][1], x[i][3]);
    }
    r(i, 2) asu(i);
    make_G();
    r(i, 120) r(j, 120) if (!a[i][j]) ans++, dfs(i, j);
    cout << ans << endl;
  }
}