Problem p00701 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int m, I, J, p[10000][100], s[10000], P;
pair<int, int> pos[101];

void destroy(int pI, int ppI, pair<int, int> *pos) {
  p[pI][ppI] = 0;
  for (int i = ppI + 1; i < s[pI]; i++) {
    p[P][0] = p[pI][i];
    s[P] = 1;
    pos[p[P][0]] = make_pair(P++, 0);
    p[pI][i] = 0;
  }
  s[pI] = ppI;
}

int main() {
  while (cin >> m && m) {
    rep(i, 10000) fill(p[i], p[i] + 100, 0);
    fill(s, s + 10000, 0);
    rep(i, m) {
      p[i][0] = i + 1;
      s[i] = 1;
      pos[i + 1] = make_pair(i, 0);
    }
    P = m;
    while (cin >> I >> J && I) {
      if (I == J)
        continue;
      int pI = pos[I].first, ppI = pos[I].second, pJ = pos[J].first,
          ppJ = pos[J].second;
      if (!J) {
        if (ppI) {
          destroy(pI, ppI, pos);
          p[P][0] = I;
          s[P] = 1;
          pos[I] = make_pair(P++, 0);
        }
      } else if (pI == pJ) {
        if (ppI > ppJ)
          continue;
        else {
          int pppJ = P + ppJ - ppI - 1;
          destroy(pI, ppI, pos);
          p[pppJ][1] = I;
          s[pppJ]++;
          pos[I] = make_pair(pppJ, 1);
        }
      } else {
        destroy(pI, ppI, pos);
        p[pJ][s[pJ]] = I;
        pos[I] = make_pair(pJ, s[pJ]++);
      }
      // cout<<I<<"->"<<J<<endl;
      // rep(i,m)cout<<i+1<<":"<<pos[i+1].first<<","<<pos[i+1].second<<(i<m-1?"
      // ":"\\n"); int sum=0;rep(i,P)sum+=s[i];cout<<sum<<endl;
      // rep(i,P){if(s[i])cout<<i<<":";rep(j,s[i]){cout<<p[i][j]<<(j<s[i]-1?"
      // ":"");}if(s[i])cout<<endl;}cout<<endl;
    }
    sort(s, s + 10000);
    rep(i, 10000) if (s[i]) cout << s[i] << (i < 9999 ? "\\n" : "\\nend\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int m, I, J, p[10000][100], s[10000], P;

void destroy(int pI, int ppI) {
  p[pI][ppI] = 0;
  for (int i = ppI + 1; i < s[pI]; i++) {
    p[P][0] = p[pI][i];
    s[P++] = 1;
    p[pI][i] = 0;
  }
  s[pI] = ppI;
}

int main() {
  while (cin >> m && m) {
    rep(i, 10000) fill(p[i], p[i] + 100, 0);
    fill(s, s + 10000, 0);
    rep(i, m) {
      p[i][0] = i + 1;
      s[i] = 1;
    }
    P = m;
    while (cin >> I >> J && I) {
      if (I == J)
        continue;
      int pI, ppI, pJ, ppJ;
      if (!J) {
        rep(i, 10000) rep(j, m) {
          if (p[i][j] == I) {
            pI = i;
            ppI = j;
          }
        }
        if (ppI) {
          destroy(pI, ppI);
          p[P][0] = I;
          s[P++] = 1;
        }
        continue;
      }
      rep(i, 10000) rep(j, m) {
        if (p[i][j] == I) {
          pI = i;
          ppI = j;
        }
        if (p[i][j] == J) {
          pJ = i;
          ppJ = j;
        }
      }
      if (pI == pJ) {
        if (ppI > ppJ)
          continue;
        else {
          int pppJ = P + ppJ - ppI;
          destroy(pI, ppI);
          p[pppJ][1] = I;
          s[pppJ]++;
        }
      } else {
        destroy(pI, ppI);
        p[pJ][s[pJ]++] = I;
      }
    }
    sort(s, s + 10000);
    rep(i, 10000) if (s[i]) cout << s[i] << (i < 9999 ? "\\n" : "\\nend\\n");
  }
}