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

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

int N;
int weight[100010];
int belong[100010];
list<int> has[100010];
int merge(int a, int b, int w) {
  if (has[a].size() < has[b].size()) {
    swap(a, b);
    w *= -1;
  }
  int A = belong[a];
  int B = belong[b];
  int diff = w + weight[a] - weight[b];
  for (auto i : has[B]) {
    weight[i] += diff;
    belong[i] = A;
    has[A].push_back(i);
  }
  has[B].clear();
}
int main() {
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    belong[i] = i, has[i].push_back(i);
  while (Q--) {
    string in;
    cin >> in;
    int a, b, c;
    if (in == "COMPARE") {
      cin >> a >> b;
      --a, --b;
      if (belong[a] == belong[b]) {
        cout << weight[b] - weight[a] << endl;
      } else {
        cout << "WARNING" << endl;
      }
    } else {
      cin >> a >> b >> c;
      --a, --b;
      if (belong[a] != belong[b]) {
        merge(a, b, c);
      }
      weight[a] += c;
      weight[b] += c;
    }
  }
}

Error: MLE

Fixed submission:

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

int N;
int weight[100010];
int belong[100010];
vector<int> has[100010];
int merge(int a, int b, int w) {
  if (has[a].size() < has[b].size()) {
    swap(a, b);
    w *= -1;
  }
  int A = belong[a];
  int B = belong[b];
  int diff = w + weight[a] - weight[b];
  for (int i = 0; i < has[B].size(); i++) {
    weight[has[B][i]] += diff;
    belong[has[B][i]] = A;
    has[A].push_back(has[B][i]);
  }
  has[B].clear();
}
int main() {
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    belong[i] = i, has[i].push_back(i);
  while (Q--) {
    string in;
    cin >> in;
    int a, b, c;
    if (in == "COMPARE") {
      cin >> a >> b;
      --a, --b;
      if (belong[a] == belong[b]) {
        cout << weight[b] - weight[a] << endl;
      } else {
        cout << "WARNING" << endl;
      }
    } else {
      cin >> a >> b >> c;
      --a, --b;
      if (belong[a] != belong[b]) {
        merge(a, b, c);
      }
      weight[a] += c;
      weight[b] += c;
    }
  }
}
Problem p01009 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

class UnionFind {
public:
  vector<ll> parent, gap, up;

  UnionFind(ll n) : parent(n), gap(n), up(n) { REP(i, 0, n) parent[i] = i; }

  ll find(ll i) {
    if (parent[i] == i)
      return i;
    ll p = parent[i];
    ll t = find(p);
    gap[i] += gap[p];
    return parent[i] = t;
  }

  void unite(ll a, ll b, ll c) {
    ll pa = find(a), pb = find(b);
    if (pa != pb) {
      ll w = height(a) - height(b) + c;
      gap[pa] = -w;
      parent[pa] = find(pb);
    }
    up[a] += c;
    up[b] += c;
  }

  ll height(ll i) { return gap[i] + up[i]; }
};

int main(void) {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> T(Q), A(Q), B(Q), C(Q);
  REP(i, 0, Q) {
    string type;
    cin >> type >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    if (type == "IN") {
      T[i] = 0;
      cin >> C[i];
    }
    if (type == "COMPARE")
      T[i] = 1;
  }

  UnionFind uf(N);
  REP(i, 0, Q) {
    if (T[i] == 0) {
      uf.unite(A[i], B[i], C[i]);
    } else {
      if (uf.find(A[i]) != uf.find(B[i])) {
        cout << "WARNING" << endl;
      } else {
        cout << uf.height(B[i]) - uf.height(A[i]) << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

class UnionFind {
public:
  vector<ll> parent, gap, up;

  UnionFind(ll n) : parent(n), gap(n), up(n) { REP(i, 0, n) parent[i] = i; }

  ll find(ll i) {
    if (parent[i] == i)
      return i;
    ll p = parent[i];
    gap[i] += gap[p];
    return parent[i] = find(parent[i]);
  }

  void unite(ll a, ll b, ll c) {
    ll w = height(a) - height(b) + c;
    gap[a] -= w;
    parent[a] = b;
    up[a] += c;
    up[b] += c;
  }

  ll height(ll i) {
    find(i);
    return gap[i] + up[i];
  }
};

int main(void) {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> T(Q), A(Q), B(Q), C(Q);
  REP(i, 0, Q) {
    string type;
    cin >> type >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    if (type == "IN") {
      T[i] = 0;
      cin >> C[i];
    }
    if (type == "COMPARE")
      T[i] = 1;
  }

  UnionFind uf(N);
  REP(i, 0, Q) {
    if (T[i] == 0)
      uf.unite(A[i], B[i], C[i]);
    else if (uf.find(A[i]) != uf.find(B[i]))
      cout << "WARNING" << endl;
    else
      cout << uf.height(B[i]) - uf.height(A[i]) << endl;
  }
}
Problem p01012 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-7;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

ld getans(ld l, ld r, int b, ld c) {
  if (b == 0 || abs(r - l) < eps) {
    return (1 - l) * (r - l);
  }
  ld aa = getans(l, (1 - c) * l + c * r, b - 1, c);
  ld ab = getans((1 - c) * l + c * r, r, b - 1, c);
  return aa + ab;
}
ld get() {
  int m, n, x;
  cin >> m >> n >> x;
  ld c = (ld(m)) / (m + n);
  x = min(x, 40);
  return getans(0, 1, x, c);
}

int main() {
  ld a = get();
  ld b = get();
  cout << setprecision(10) << fixed << a * b << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-8;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

ld getans(ld l, ld r, int b, ld c) {
  if (b == 0 || abs(r - l) < eps) {
    return (1 - l) * (r - l);
  }
  ld aa = getans(l, (1 - c) * l + c * r, b - 1, c);
  ld ab = getans((1 - c) * l + c * r, r, b - 1, c);
  return aa + ab;
}
ld get() {
  int m, n, x;
  cin >> m >> n >> x;
  ld c = (ld(m)) / (m + n);
  x = min(x, 40);
  return getans(0, 1, x, c);
}

int main() {
  ld a = get();
  ld b = get();
  cout << setprecision(10) << fixed << a * b << endl;
  return 0;
}
Problem p01012 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>

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

using namespace std;

typedef long double ld;

int m, n, x, k, l, y;

ld side(ld a, int k) {
  if (k == 0) {
    return 1;
  }
  return (2 * a * a - 2 * a + 1) * side(a, k - 1) + a * (1 - a);
}

int main() {
  scanf("%d%d%d%d%d%d", &m, &n, &x, &k, &l, &y);
  printf("%Lf\\n", side(n / ld(n + m), x) * side(l / ld(l + k), y));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

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

using namespace std;

typedef long double ld;

int m, n, x, k, l, y;

ld expect(ld x1, ld y1, ld x2, ld y2, int a, int b) {
  if (a == 0 && b == 0) {
    return x2 * y2 * (x2 - x1) * (y2 - y1);
  }
  if (a > b) {
    ld x0 = (x1 * m + x2 * n) / (m + n);
    return expect(x1, y1, x0, y2, a - 1, b) + expect(x0, y1, x2, y2, a - 1, b);
  } else {
    ld y0 = (y1 * k + y2 * l) / (k + l);
    return expect(x1, y1, x2, y0, a, b - 1) + expect(x1, y0, x2, y2, a, b - 1);
  }
}

int main() {
  scanf("%d%d%d%d%d%d", &m, &n, &x, &k, &l, &y);
  printf("%Lf\\n", expect(0, 0, 1, 1, x, y));
  return 0;
}
Problem p01048 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define Int long long int
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  int num = 0;
  while (1) {
    num++;
    int cou = 0;
    REAP(i, 1, num + 1) {
      if (num % i == 0)
        cou++;
    }
    if (cou == n) {
      cout << num << endl;
      break;
    }
  }
}


Error: TLE

Fixed submission:

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

#define Int long long int
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  int num = 0;
  while (1) {
    num++;
    int cou = 0;
    REAP(i, 1, num + 1) {
      if (!num % i)
        cou++;
    }
    if (cou == n) {
      cout << num << endl;
      break;
    }
  }
}
Problem p01049 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef vector<int> vec;

int main() {
  int N, M, a, b;
  cin >> N >> a >> b >> M;

  map<int, int> A;

  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;

    if (x == 0) {
      int Y, Z;
      if (A.find(y) != A.end())
        Y = A[y];
      else
        Y = a + (y - 1) * b;
      if (A.find(z) != A.end())
        Z = A[z];
      else
        Z = a + (z - 1) * b;

      A[y] = Z;
      A[z] = Y;
    } else {
      int Y, Z;
      if (A.find(z) != A.end())
        Z = A[z];
      else
        Z = a + (z - 1) * b;

      A[y] = Z;
    }
  }

  int k;
  cin >> k;
  if (A.find(k) != A.end())
    cout << A[k] << endl;
  else
    cout << a + (k - 1) * b << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

typedef vector<int> vec;

int main() {
  int N, M, a, b;
  cin >> N >> a >> b >> M;
  vec A(N);
  for (int i = 0; i < N; i++) {
    A[i] = a + b * i;
  }

  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    y--;
    z--;
    if (x == 0) {
      int tmp = A[y];
      A[y] = A[z];
      A[z] = tmp;
    } else {
      A[y] = A[z];
    }
  }

  int k;
  cin >> k;
  cout << A[k - 1] << endl;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

map<int, int> series;

int main() {
  int N, A, D, M, K;
  cin >> N >> A >> D >> M;
  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    y--;
    z--;
    if (series.find(y) == series.end())
      series[y] = y;
    if (series.find(z) == series.end())
      series[z] = z;
    if (x == 0)
      swap(series[y], series[z]);
    if (x == 1)
      series[y] = series[z];
  }
  cin >> K;
  K--;
  if (series.find(K) == series.end())
    series[K] = K;
  printf("%d\\n", (ll)A + (ll)D * series[K]);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

int series[(int)1e8];

int main() {
  for (int i = 0; i < 1e8; i++)
    series[i] = i;
  int N, A, D, M, K;
  cin >> N >> A >> D >> M;
  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    y--;
    z--;
    if (x == 0)
      swap(series[y], series[z]);
    if (x == 1)
      series[y] = series[z];
  }
  cin >> K;
  K--;
  printf("%d\\n", (ll)A + (ll)D * series[K]);
  return 0;
}
Problem p01049 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define loop(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
typedef long long ll;

// 1 indexed

int main() {
  ll N;
  while (cin >> N && N) {
    ll a, d;
    ll M;
    cin >> a >> d >> M;
    vector<ll> x(M), y(M), z(M);
    ll k;

    auto kth = [&](ll k) {
      ll res = a;
      rep(i, k - 1) { res += d; }
      return res;
    };

    rep(i, M) cin >> x[i] >> y[i] >> z[i];
    set<int> s;
    s.insert(y.begin(), y.end());
    s.insert(z.begin(), z.end());
    cin >> k;

    // rep(i,5){
    //     cout << kth(i+1) << " ";
    // }
    // cout << endl;

    ll ans = -1;
    if (s.count(k)) {
      map<ll, ll> m;
      for (int i : s)
        m[i] = i;
      rep(i, M) {
        if (x[i] == 0) {
          swap(m[y[i]], m[z[i]]);
        } else {
          m[y[i]] = m[z[i]];
        }
      }
      ans = kth(m[k]);
    } else {
      ans = kth(k);
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

#define loop(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
typedef long long ll;

// 1 indexed

int main() {
  ll N;
  while (cin >> N && N) {
    ll a, d;
    ll M;
    vector<ll> x(N), y(N), z(N);
    ll k;

    auto kth = [&](ll k) {
      ll res = a;
      rep(i, k - 1) { res += d; }
      return res;
    };

    cin >> a >> d;
    cin >> M;
    rep(i, M) { cin >> x[i] >> y[i] >> z[i]; }
    set<int> s;
    s.insert(y.begin(), y.end());
    s.insert(z.begin(), z.end());
    cin >> k;

    // rep(i,5){
    //     cout << kth(i+1) << " ";
    // }
    // cout << endl;

    ll ans = -1;
    if (s.count(k)) {
      map<int, int> m;
      for (int i : s)
        m[i] = i;
      rep(i, M) {
        if (x[i] == 0) {
          swap(m[y[i]], m[z[i]]);
        } else {
          m[y[i]] = m[z[i]];
        }
      }
      ans = kth(m[k]);
    } else {
      ans = kth(k);
    }
    cout << ans << endl;
  }
}
Problem p01049 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int SIZE = 10 + 11;

typedef long long Int;
typedef map<Int, Int> IntMap;

Int n, a, d;
Int m;
Int x[SIZE];
Int y[SIZE];
Int z[SIZE];
Int k;

bool input() {
  if (cin >> n >> a >> d) {
    if (cin >> m) {
      for (Int i = 0; i < m; ++i) {
        cin >> x[i] >> y[i] >> z[i];
      }
      cin >> k;
      return true;
    }
  }
  return false;
}

Int solve() {
  IntMap arr;

  for (Int i = 0; i < m; ++i) {
    arr[y[i]] = a + (y[i] - 1) * d;
    arr[z[i]] = a + (z[i] - 1) * d;
  }
  arr[k] = a + (k - 1) * d;

  for (Int i = 0; i < m; ++i) {
    if (x[i]) { // update
      arr[y[i]] = arr[z[i]];
    } else { // swap
      Int c = arr[y[i]];
      arr[y[i]] = arr[z[i]];
      arr[z[i]] = c;
    }
  }

  return arr[k];
}

int main() {
  while (input())
    cout << solve() << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

const int SIZE = 10 + 11;

typedef map<int, int> IntMap;

int n, a, d;
int m;
int x[SIZE];
int y[SIZE];
int z[SIZE];
int k;

bool input() {
  if (cin >> n >> a >> d) {
    if (cin >> m) {
      for (int i = 0; i < m; ++i) {
        cin >> x[i] >> y[i] >> z[i];
      }
      cin >> k;
      return true;
    }
  }
  return false;
}

int solve() {
  IntMap arr;

  for (int i = 1; i <= n; ++i) {
    arr[i] = a + (i - 1) * d;
  }

  for (int i = 0; i < m; ++i) {
    if (x[i]) { // update
      arr[y[i]] = arr[z[i]];
    } else { // swap
      swap(arr[y[i]], arr[z[i]]);
    }
  }

  return arr[k];
}

int main() {
  while (input())
    cout << solve() << endl;
  return 0;
}
Problem p01049 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = 1; i < n; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  REP(i, v.size()) cout << #v << "[" << i << "]: " << v[i] << endl
#define ALL(a) (a).begin(), (a).end()

int main() {
  int N, a, d, M;
  cin >> N >> a >> d >> M;
  int x[M], y[M], z[M];
  REP(i, M) {
    cin >> x[i] >> y[i] >> z[i];
    --y[i];
    --z[i];
  }
  int K;
  cin >> K;
  --K;

  REP(i_, M) {
    int i = M - 1 - i_;
    if (y[i] == K) {
      K = z[i];
    } else if (z[i] == K && x[i] == 0) {
      K = y[i];
    }
  }

  cout << (a + d * K) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = 1; i < n; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  REP(i, v.size()) cout << #v << "[" << i << "]: " << v[i] << endl
#define ALL(a) (a).begin(), (a).end()

int main() {
  int N, a, d, M;
  cin >> N >> a >> d >> M;
  // create
  vi A(N);
  A[0] = a;
  REPR(i, N) A[i] = A[i - 1] + d;
  REP(i, M) {
    int x, y, z;
    cin >> x >> y >> z;
    --y;
    --z;
    if (x == 0)
      swap(A[y], A[z]);
    if (x == 1)
      A[y] = A[z];
  }
  int K;
  cin >> K;
  cout << A[K - 1] << endl;
  return 0;
}
Problem p01049 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {

  int N, a, b, M, x[11], y[11], z[11], K;

  cin >> N >> a >> b >> M;
  for (int j = 1; j <= M; j++) {
    cin >> x[j] >> y[j] >> z[j];
  }
  cin >> K;
  for (int j = M; j > 0; j--) {
    if (x[j] == 0) {
      if (y[j] == K)
        ;
      else if (z[j] == K)
        swap(y[j], z[j]);
      else
        continue;
      K = z[j];
    } else {
      if (y[j] == K)
        K = z[j];
    }
  }
  cout << a + b * (K - 1) << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

int n[100000010] = {};
int main() {
  int N, a, b, M, x, y, z, K, i = 1;

  cin >> N >> a >> b >> M;
  while (N--) {
    n[i++] = a + b * (i - 1);
  }

  while (M--) {
    cin >> x >> y >> z;
    if (x)
      n[y] = n[z];
    else
      swap(n[y], n[z]);
  }
  cin >> K;
  cout << n[K] << endl;

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

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)
using namespace std;
typedef long long ll;
int main() {
  int n, a, d, m;
  cin >> n >> a >> d >> m;
  map<ll, int> dat;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      int yy = dat.count(y) ? dat[y] : y;
      int zz = dat.count(z) ? dat[z] : z;
      dat[y] = zz;
      dat[z] = yy;
    } else {
      int zz = dat.count(z) ? dat[z] : z;
      dat[y] = zz;
    }
  }
  int k;
  cin >> k;
  if (dat.count(k)) {
    printf("%lld\\n", (ll)a + (dat[k] - 1) * d);
  } else {
    printf("%lld\\n", (ll)a + (k - 1) * d);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)
using namespace std;
typedef long long ll;
#define SIZE 100000000
int main() {
  int n, m, a, d;
  cin >> n >> a >> d >> m;
  ll dat[n];
  dat[0] = a;
  REP(i, n - 1) dat[i + 1] = dat[i] + d;
  REP(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    y--;
    z--;
    if (x == 0) {
      swap(dat[y], dat[z]);
    } else {
      dat[y] = dat[z];
    }
  }
  int k;
  cin >> k;
  k--;
  cout << dat[k] << endl;
}
Problem p01052 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define N 101
#define INF (1e9)
using namespace std;
int n, a[N], b[N], ans, c;
bool used[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= 31; i++) {
    c = INF;
    for (int j = 0; j < n; j++)
      if (!used[j] && a[j] <= i && i <= b[j]) {
        if (c == INF)
          c = j;
        else if (b[j] < b[c])
          c = j;
      }
    if (c != INF)
      used[c] = true;
  }
  for (int i = 0; i < n; i++)
    if (used[i])
      ans++;
  cout << ans * 100 + (31 - ans) * 50 << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define N 101
#define INF (1e9)
using namespace std;
int n, a[N], b[N], ans, c;
bool used[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= 31; i++) {
    c = INF;
    for (int j = 0; j < n; j++)
      if (!used[j] && a[j] <= i && i <= b[j] && b[j] <= b[c])
        c = j;
    if (c != INF)
      used[c] = true;
  }
  for (int i = 0; i < n; i++)
    if (used[i])
      ans++;
  cout << ans * 100 + (31 - ans) * 50 << endl;
  return 0;
}
Problem p01052 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int n;
  cin >> n;
  vector<pair<int, int>> date;
  vector<int> happy(n, 100);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    date.push_back(make_pair(a, b));
  }
  for (int day = 1; day <= 31; day++) {
    int watching = INT_MAX;
    int w = -1;
    for (int i = 0; i < n; i++) {
      if (date[i].first <= day && day <= date[i].second) {
        if (watching > date[i].second && happy[i] == 100) {
          w = i;
          watching = date[i].second;
        }
      }
    }
    if (w == -1) {
      for (int i = 0; i < n; i++) {
        if (date[i].first <= day && day <= date[i].second) {
          ans += 50;
          break;
        }
      }
    } else {
      ans += happy[w];
      happy[w] = 50;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> happy;
vector<pair<int, int>> date;

int func(int day) {
  if (day == 32)
    return 0;
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (date[i].first <= day && day <= date[i].second) {
      int h = happy[i];
      happy[i] = 50;
      res = max(res, func(day + 1) + h);
      happy[i] = h;
    }
  }
  return res;
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    happy.push_back(100);
    date.push_back(make_pair(a, b));
  }
  cout << func(1) << endl;
}