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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

const int MAX_T = 30;
const int MAX_DAY = 100;

int T, N;
int t[MAX_T];
int m[MAX_DAY]; // the first meeting time of the day
int memo[MAX_DAY][MAX_T];
int dfs(int day, int step) {
  if (day == MAX_DAY)
    return 0;
  if (memo[day][step] != -1)
    return memo[day][step];
  if (m[day] < t[step])
    return inf;

  return memo[day][step] =
             min(dfs(day + 1, (step + 1) % T), dfs(day + 1, 0) + 1);
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  while (cin >> T, T) {
    rep(i, T) cin >> t[i];
    cin >> N;
    fill(m, m + MAX_DAY, inf);
    rep(i, N) {
      int D, M;
      cin >> D >> M;
      D--;
      m[D] = min(m[D], M);
    }
    rep(i, MAX_DAY) fill(memo[i], memo[i] + MAX_T, -1);
    cout << dfs(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

int T, N;
int t[30];
int m[100]; // the first meeting time of the day
int memo[100][30];
int dfs(int day, int step) {
  if (day == N)
    return 0;
  if (memo[day][step] != -1)
    return memo[day][step];
  if (m[day] < t[step])
    return inf;

  return min(dfs(day + 1, (step + 1) % T), dfs(day + 1, 0) + 1);
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  while (cin >> T, T) {
    rep(i, T) cin >> t[i];
    cin >> N;
    fill(m, m + 100, inf);
    rep(i, N) {
      int D, M;
      cin >> D >> M;
      D--;
      m[D] = min(m[D], M);
    }
    rep(i, 100) fill(memo[i], memo[i] + 30, -1);
    cout << dfs(0, 0) << endl;
  }
}
Problem p01284 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main(void) {
  int t;
  while (cin >> t && t) {
    vector<int> s(t);
    for (int i = 0; i < t; i++)
      cin >> s[i];
    int n;
    cin >> n;
    vector<int> d(101, 24);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      d[a] = min(d[a], b);
    }

    vector<vector<int>> dp(101, vector<int>(t, 101));
    dp[0][0] = 0;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < t; j++) {
        if (s[j] <= d[i]) {
          dp[i + 1][(j + 1) % t] = min(dp[i + 1][(j + 1) % t], dp[i][j]);
          dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 1);
        } else
          dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 1);
      }
    }
    cout << *min_element(dp[100].begin(), dp[100].end()) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int t;
vector<int> s;
vector<int> d(101);

int dfs(int l) {
  int r = 100;
  for (int i = l; i < 100; i++) {
    if (d[i] < s[(i - l) % t]) {
      r = i;
      break;
    }
  }
  if (r == 100)
    return 0;
  vector<int> ret;
  for (int i = l + 1; i < r + 1; i++) {
    bool f = true;
    for (int j = i; j < r + 1; j++)
      if (d[j] < s[(j - i) % t]) {
        f = false;
        break;
      }
    if (!f)
      continue;
    ret.push_back(1 + dfs(i));
  }
  return *min_element(ret.begin(), ret.end());
}

int main() {
  while (cin >> t && t) {
    for (int i = 0; i < 101; i++)
      d[i] = 24;
    s = vector<int>(t);
    for (int i = 0; i < t; i++)
      cin >> s[i];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      d[a] = min(d[a], b);
    }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int color[8];
map<string, int> used;
set<int> S;
int t;

void rot1() {
  rep(i, 3) swap(color[i], color[i + 1]);
  rep(i, 3) swap(color[i + 4], color[i + 5]);
}

void rot2() {
  swap(color[0], color[4]);
  swap(color[1], color[5]);
  swap(color[4], color[7]);
  swap(color[5], color[6]);
  swap(color[7], color[3]);
  swap(color[6], color[2]);
}

bool check() {
  int s = 0;
  rep(i, 8) s = s * 10 + color[i];
  t = s;
  return S.count(t) != 0;
}

void solve() {
  S.clear();
  int num[8];
  int cp[8];
  rep(i, 8) num[i] = i, cp[i] = color[i];
  do {
    rep(i, 8) color[i] = cp[num[i]];
    bool f = false;
    rep(i, 4) {
      rot1();
      rep(j, 4) {
        rot2();
        rep(k, 4) {
          rot1();
          if (check())
            f = true;
        }
      }
    }
    if (!f)
      S.insert(t);
  } while (next_permutation(num, num + 8));
  cout << S.size() << endl;
}

string in[8];
int main() {
  while (cin >> in[7]) {
    rep(i, 7) cin >> in[i];
    used.clear();
    int Num = 1;
    rep(i, 8) {
      if (!used.count(in[i]))
        used[in[i]] = Num++;
      color[i] = used[in[i]];
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
string color[8];
map<string, int> used;
set<int> S;
int t;

void rot1() {
  rep(i, 3) swap(color[i], color[i + 1]);
  rep(i, 3) swap(color[i + 4], color[i + 5]);
}

void rot2() {
  swap(color[0], color[4]);
  swap(color[1], color[5]);
  swap(color[4], color[7]);
  swap(color[5], color[6]);
  swap(color[7], color[1]);
  swap(color[6], color[2]);
}

bool check() {
  int s = 0;
  rep(i, 8) s = s * 10 + used[color[i]];
  t = s;
  return S.count(t) != 0;
}

void solve() {
  S.clear();
  int num[8];
  string cp[8];
  rep(i, 8) num[i] = i, cp[i] = color[i];
  do {
    rep(i, 8) color[i] = cp[num[i]];
    bool f = false;
    rep(i, 4) {
      rot1();
      rep(j, 4) {
        rot2();
        rep(k, 4) {
          rot1();
          if (check())
            f = true;
        }
      }
    }
    if (!f)
      S.insert(t);
  } while (next_permutation(num, num + 8));
  cout << S.size() << endl;
}

int main() {
  while (cin >> color[7]) {
    rep(i, 7) cin >> color[i];
    used.clear();
    int Num = 0;
    rep(i, 8) if (!used.count(color[i])) used[color[i]] = Num++;
    solve();
  }
  return 0;
}
Problem p01287 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

int cc[8];

int solve(vector<int> c) {
  int s, t;
  int m = INF;

  rep(i1, 0, 4) rep(i2, 0, 4) rep(i3, 0, 4) {
    rep(i, 0, 8) cc[i] = c[i];

    rep(i, 0, i1) {
      t = cc[0];
      cc[0] = cc[4];
      cc[4] = cc[7];
      cc[7] = cc[3];
      cc[3] = t;

      t = cc[1];
      cc[1] = cc[5];
      cc[5] = cc[6];
      cc[6] = cc[2];
      cc[2] = t;
    }

    rep(i, 0, i2) {
      t = cc[0];
      cc[0] = cc[1];
      cc[1] = cc[5];
      cc[5] = cc[4];
      cc[4] = t;

      t = cc[3];
      cc[3] = cc[2];
      cc[2] = cc[6];
      cc[6] = cc[7];
      cc[7] = t;
    }

    rep(i, 0, i3) {
      t = cc[0];
      cc[0] = cc[1];
      cc[1] = cc[2];
      cc[2] = cc[3];
      cc[3] = t;

      t = cc[4];
      cc[4] = cc[5];
      cc[5] = cc[6];
      cc[6] = cc[7];
      cc[7] = t;
    }

    s = 0;
    rep(i, 0, 8) s = s * 10 + cc[i];
    m = min(m, s);
  }

  return m;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s[8];
  while (cin >> s[0]) {
    rep(i, 1, 8) cin >> s[i];

    map<string, int> m;
    rep(i, 0, 8) m[s[i]] = 0;
    int i = 0;
    for (auto p : m) {
      m[p.first] = i;
      i++;
    }

    vector<int> c(8);
    rep(i, 0, 8) c[i] = m[s[i]];

    set<int> ans;
    sort(c.begin(), c.end());
    do {
      ans.insert(solve(c));
    } while (next_permutation(c.begin(), c.end()));

    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)

set<vector<string>> ans;
vector<string> c(8);

bool check() {
  for (auto vs : ans) {
    bool ok = true;
    rep(i, 0, 8) {
      if (vs[i] != c[i])
        ok = false;
    }
    if (ok)
      return true;
  }
  return false;
}

void solve() {
  string s;

  rep(j, 0, 2) {
    if (check())
      return;
    rep(i, 0, 3) {
      s = c[0];

      c[0] = c[1];
      c[1] = c[2];
      c[2] = c[3];
      c[3] = s;

      if (check())
        return;
    }

    swap(c[0], c[6]);
    swap(c[1], c[5]);
    swap(c[2], c[4]);
    swap(c[3], c[7]);
  }

  ans.insert(c);
}

void dfs(int d) {
  if (d == 8) {
    solve();
    return;
  }

  rep(i, d, 8) {
    swap(c[d], c[i]);
    dfs(d + 1);
    swap(c[d], c[i]);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  while (cin >> s) {
    c[0] = s;
    rep(i, 1, 8) cin >> c[i];
    ans.clear();
    dfs(0);
    cout << ans.size() << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef long long ll;
const int maxn = 100010;
int tree[maxn];
int Find(int x) {
  if (tree[x] == x)
    return x;
  return Find(tree[x]);
}
int main() {
  int n, m, id, i;
  char com[10];
  ll ans;

  while (scanf("%d%d", &n, &m), n || m) {
    tree[1] = 1;
    ans = 0;
    for (i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (i = 0; i < m; i++) {
      scanf("%s%d", com, &id);
      if (com[0] == 'M')
        tree[id] = id;
      else
        ans += Find(id);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef long long ll;
const int maxn = 100010;
int tree[maxn];
int Find(int x) {
  if (tree[x] == x)
    return x;
  return Find(tree[x]);
}
int main() {
  int n, m, id, i;
  char com[10];
  ll ans;

  while (scanf("%d%d", &n, &m), n || m) {
    tree[1] = 1;
    ans = 0;
    for (i = 1; i < n; i++)
      scanf("%d", &tree[i]);
    for (i = 0; i < m; i++) {
      scanf("%s%d", com, &id);
      if (com[0] == 'M')
        tree[id] = id;
      else
        ans += Find(id);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 100005;
int pre[N];    // 存??点之?的?系
int marked[N]; // 存?是否被??以及??的?序

int operation_order[N];      // 按?序存??入的Q操作
int operation_node_index[N]; // 存?Q操作??的?点下?

long long sum;
int process_time;

int find_ance(int index) {
  if (marked[index] < process_time) {
    return index;
  } else {
    pre[index] = find_ance(pre[index]);
    return pre[index];
  }
}

int main() {
  int n, q;
  char op;
  int index;

  while (cin >> n >> q, n) {
    sum = 0;
    pre[1] = 1;
    marked[1] = 0;
    for (int i = 2; i <= n; ++i) {
      cin >> pre[i];
      marked[i] = q + 1;
    }
    int Q_operation_num = 0;
    for (int i = 1; i <= q; ++i) {
      cin >> op >> index;
      if (op == 'Q') {
        operation_order[Q_operation_num] = i;
        operation_node_index[Q_operation_num++] = index;
      } else {
        marked[index] = min(marked[index], i);
      }
    }

    while (Q_operation_num--) {
      process_time = operation_order[Q_operation_num];
      sum += find_ance(operation_node_index[Q_operation_num]);
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int N = 100005;
int a[2][N];

int sum;

int find_ance(int index) {

  if (a[1][index] == 1) {
    return index;
  } else {
    return find_ance(a[0][index]);
  }
}

int main() {
  int n, q;
  char op;
  int quest;
  sum = 0;
  cin >> n >> q;
  a[0][1] = 1;
  a[1][1] = 1;
  for (int i = 2; i <= n; ++i) {
    cin >> a[0][i];
    a[1][i] = 0;
  }

  while (true) {
    cin >> op >> quest;
    if (op == '0' && quest == 0) {
      break;
    }
    if (op == 'M') {
      a[1][quest] = 1;
    } else {
      sum += find_ance(quest);
    }
  }
  cout << sum << endl;

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

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

#define MAX_N 100001

typedef unsigned long long ull;

bool M[MAX_N]; // マークされてるかどうか
int P[MAX_N];  // 親

int main() {
  int N, Q;
  while (cin >> N >> Q, N != 0 && Q != 0) {
    memset(M, 0, sizeof(M));
    M[1] = true;
    memset(P, 0, sizeof(P));
    for (int i = 2; i < N + 1; i++) {
      int p;
      cin >> p;
      P[i] = p;
    }

    //        for (int i = 1; i <= N; i++) cout << P[i] << ' ';
    //        cout << endl;
    //        for (int i = 1; i <= N; i++) cout << M[i] << ' ';
    //        cout << endl;

    ull sum = 0;
    for (int i = 0; i < Q; i++) {
      char q;
      int v;
      cin >> q >> v;
      if (q == 'Q') {
        while (!M[v]) {
          v = P[v];
        }
        sum += v;
      } else {
        M[v] = true;
      }
    }
    //        for (int i = 1; i <= N; i++) cout << M[i] << ' ';
    //        cout << endl;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int N, O; // O: number of operations

struct Node {
  int index;
  int r, l;
  int p;
  bool marked;
  Node() {
    r = l = 0;
    p = 0;
    marked = false;
  }
};

int Q(Node *tree, int index) {
  while (!tree[tree[index].p].marked) {
    index = tree[index].p;
  }
  return tree[index].p;
}

void M(Node *tree, int index) { tree[index].marked = true; }

int main() {
  while (cin >> N >> O) {
    if (N == 0 && O == 0)
      break;
    Node tree[N + 1];
    tree[1].index = 1;
    tree[1].marked = true;
    for (int i = 2; i <= N; i++) {
      int parent;
      cin >> parent;
      tree[i].index = i;
      tree[i].p = parent;
    }

    int sum = 0;
    for (int i = 0; i < O; i++) {
      char op;
      int index;
      cin >> op >> index;
      if (op == 'Q') {
        sum += Q(tree, index);
      } else {
        M(tree, index);
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long ll;

const int MAX_N = 100000, MAX_Q = 100000;

int N, Q;
int p[MAX_N];
char q[MAX_Q + 1];
int v[MAX_Q];

int colored[MAX_N]; //?????????????????°

vector<ll> out;

int find(int i) {
  //?????????i?????????????????????????????????????????????
  if (colored[i]) {
    return i;
  }
  return p[i] = find(p[i]);
}

ll solve() {
  //?????¨?????????????????????
  fill(colored, colored + N, 0);
  colored[0] = 1; //????????????????????????????????????
  for (int i = 0; i < Q; i++) {
    if (q[i] == 'M') {
      colored[v[i]]++;
    }
  }
  ll sum = 0;
  for (int i = Q - 1; i >= 0; i--) {
    if (q[i] == 'M') {
      colored[v[i]]--;
    } else {
      sum += find(v[i]) + 1;
    }
  }
  return sum;
}

int main() {
  do {
    scanf("%d %d", &N, &Q);
    if (N || Q) {
      p[0] = 0;
      for (int i = 0; i < N - 1; i++) {
        scanf("%d", &p[i + 1]);
        p[i + 1]--;
      }
      for (int i = 0; i < Q; i++) {
        scanf(" %c %d", &q[i], &v[i]);
        v[i]--;
      }
      out.push_back(solve());
    }
  } while (N || Q);
  for (int i = 0; i < out.size(); i++) {
    printf("%lld\\n", out[i]);
  }
  return 0;
}

Error: 0
debug


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAX_N = 100000, MAX_Q = 100000;

int N, Q;
int p[MAX_N];
char q[MAX_Q + 1];
int v[MAX_Q];

int colored[MAX_N]; //?????????????????°

vector<int> out;

int find(int i) {
  //?????????i?????????????????????????????????????????????
  if (colored[i]) {
    return i;
  }
  return p[i] = find(p[i]);
}

int solve() {
  //?????¨?????????????????????
  fill(colored, colored + N, 0);
  colored[0] = 1; //????????????????????????????????????
  for (int i = 0; i < Q; i++) {
    if (q[i] == 'M') {
      colored[v[i]]++;
    }
  }
  int sum = 0;
  for (int i = Q - 1; i >= 0; i--) {
    if (q[i] == 'M') {
      colored[v[i]]--;
    } else {
      sum += (p[v[i]] = find(p[v[i]])) + 1;
    }
  }
  return sum;
}

int main() {
  do {
    scanf("%d %d", &N, &Q);
    if (N || Q) {
      p[0] = 0;
      for (int i = 0; i < N - 1; i++) {
        scanf("%d", &p[i + 1]);
        p[i + 1]--;
      }
      for (int i = 0; i < Q; i++) {
        scanf(" %c %d", &q[i], &v[i]);
        v[i]--;
      }
      fprintf(stderr, "debug\\n");
      out.push_back(solve());
    }
  } while (N || Q);
  for (int i = 0; i < out.size(); i++) {
    printf("%d\\n", out[i]);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int find(vector<int> &par, int k) {
  if (par[k] == k)
    return k;
  return par[k] = find(par, par[k]);
}

int main() {
  int N, Q;
  while (cin >> N >> Q, N) {
    vector<int> par(N);
    for (int i = 0; i < N; ++i)
      par[i] = i;
    int p[N];
    p[0] = 0;
    for (int i = 1; i < N; ++i) {
      cin >> p[i];
      --p[i];
      par[i] = p[i];
    }
    vector<pair<char, int>> Query;
    char c;
    int k, count[N];
    fill(count, count + N, 0);
    for (int i = 0; i < Q; ++i) {
      cin >> c >> k;
      --k;
      if (c == 'M') {
        par[k] = k;
        ++count[k];
      }
      Query.push_back(make_pair(c, k));
    }
    reverse(Query.begin(), Query.end());
    /*
    for(int i = 0; i < N; ++i){
      cout << par[i] << " ";
    }cout << endl;
    */
    long long int ans = 0;
    for (int i = 0; i < Q; ++i) {
      c = Query[i].first;
      k = Query[i].second;
      if (c == 'Q') {
        ans += (long long int)find(par, k) + 1;
      } else {
        --count[k];
        if (!count[k])
          par[k] = par[p[k]];
      }
    }
    /*
    for(int i = 0; i < N; ++i){
      cout << par[i] << " ";
    }cout << endl;
    */
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int find(vector<int> par, int k) {
  if (par[k] == k)
    return k;
  return par[k] = find(par, par[k]);
}

int main() {
  int N, Q;
  while (cin >> N >> Q, N) {
    vector<int> par(N);
    for (int i = 0; i < N; ++i)
      par[i] = i;
    int p[N];
    p[0] = 0;
    for (int i = 1; i < N; ++i) {
      cin >> p[i];
      --p[i];
      par[i] = p[i];
    }
    vector<pair<char, int>> Query;
    char c;
    int k, count[N];
    fill(count, count + N, 0);
    for (int i = 0; i < Q; ++i) {
      cin >> c >> k;
      --k;
      if (c == 'M') {
        par[k] = k;
        ++count[k];
      }
      Query.push_back(make_pair(c, k));
    }
    reverse(Query.begin(), Query.end());
    int ans = 0;
    for (int i = 0; i < Q; ++i) {
      c = Query[i].first;
      k = Query[i].second;
      if (c == 'Q') {
        ans += find(par, k) + 1;
      } else {
        --count[k];
        if (!count[k])
          par[k] = par[p[k]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long n, m, x, sum, flg;
string str;
int t[100001], u[100001];
int dfs(int x) {
  if (u[x] == 1)
    return x;
  return dfs(t[x]);
}
int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < 100001; i++)
      u[i] = 0;
    t[1] = u[1] = 1;
    sum = 0;
    for (int i = 0; i < n - 1; i++)
      cin >> t[i + 2];
    for (int i = 0; i < m; i++) {
      cin >> str >> x;
      if (str == "Q") {
        sum += dfs(x);
      } else {
        u[x] = 1;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int k = 0;
    for (int i = 0; i < 1000000000; ++i)
      k += i;
    cout << 0 << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 100000;

int n, p;
int pre[N + 1];

int find(int x) {
  while (x != pre[x])
    x = pre[x];
  return x;
}

int main(void) {
  while (scanf("%d%d", &n, &p) != EOF) {
    if (!n && !p)
      break;
    pre[1] = 1;
    for (int i = 2; i <= n; i++)
      scanf("%d", &pre[i]);
    long long sum = 0;
    for (int i = 1; i <= p; i++) {
      char s[2];
      int x;
      scanf("%s%d", s, &x);
      if (s[0] == 'M')
        pre[x] = x;
      else
        sum += find(x);
    }
    printf("%lld\\n", sum);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define ll long long
int q, n, v;
char c;
int p[100005];
bool m[100005];
int main() {
  while ((~scanf("%d %d", &n, &q)) && q && n) {
    for (int i = 1; i <= n; ++i)
      scanf("%d", &p[i]);
    memset(m, 0, sizeof m);
    m[1] = true;
    ll ans = 0;
    for (int i = 0; i < q; ++i) {
      scanf("\\n%c %d", &c, &v);
      if (c == 'M')
        p[v] = v;
      else {
        while (p[v] != v)
          v = p[v];
        ans += v;
        // printf("%d**\\n", v);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}