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

#include <iostream>
#include <vector>

#define AMNT 4096

using namespace std;

typedef pair<int, int> range;
typedef vector<range> deck;

inline int quant(const range &r) { return r.second - r.first + 1; }

int shuffle(int x, int y, deck &D, bool count = false, int r = 0) {
  int i = 0;
  deck d1, d2, d3;
  int cts[D.size()];

  for (int i = 0; i < D.size(); ++i) {
    cts[i] = (i == 0 ? 0 : cts[i - 1]) + (D[i].second - D[i].first) + 1;
  }

  d1.reserve(AMNT);
  d2.reserve(AMNT);
  d3.reserve(AMNT);

  // part I
  int precnt = 0, cnt = 0;
  for (i; i < D.size(); ++i) {
    range R = D[i];
    cnt += quant(R);
    if (cnt >= x) {
      d1.push_back(range(R.first, R.first + (x - precnt) - 1));
      break;
    } else {
      d1.push_back(R);
    }
    precnt = cnt;
  }
  i = max(i - 1, 0);
  precnt = i <= 1 ? 0 : cts[i - 2];
  cnt = i <= 0 ? 0 : cts[i - 1];

  // part II
  bool first = true;
  for (i; i < D.size(); ++i) {
    range R = D[i];
    cnt += quant(R);
    if (precnt <= x && precnt <= y && cnt > x && cnt > y) {
      d2.push_back(range(R.first + (x - precnt), R.first + (y - precnt) - 1));
      break;
    } else if (cnt >= x && first) {
      d2.push_back(range(R.first + (x - precnt), R.second));
      first = false;
    } else if (cnt >= x && cnt < y) {
      d2.push_back(R);
    } else if (cnt >= y) {
      d2.push_back(range(R.first, R.first + (y - precnt) - 1));
      break;
    }
    precnt = cnt;
  }
  i = max(i - 1, 0);
  precnt = i <= 1 ? 0 : cts[i - 2];
  cnt = i <= 0 ? 0 : cts[i - 1];

  // part III
  first = true;
  for (i; i < D.size(); ++i) {
    range R = D[i];
    cnt += quant(R);
    if (cnt > y && first) {
      d3.push_back(range(R.first + (y - precnt), R.second));
      first = false;
    } else if (!first) {
      d3.push_back(R);
    }
    precnt = cnt;
  }

  /*
    cout << "D1" << endl;
    for(deck::iterator itd=d1.begin();itd!=d1.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }cout << "D2" << endl;
    for(deck::iterator itd=d2.begin();itd!=d2.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }cout << "D3" << endl;
    for(deck::iterator itd=d3.begin();itd!=d3.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }
    cout << endl;
  */

  if (count) {
    int ret = 0;
    if (d1.back().second <= r)
      ++ret;
    for (i = 0; i < d2.size(); ++i) {
      range R = d2[i];
      // cout << R.first << ' ' << R.second << endl;
      if (R.first <= r) {
        ret += (min(R.second, r) - R.first + 1);
      }
    }
    return ret;
  }

  int ni = 0;
  //  D.clear();
  D.resize(d1.size() + d2.size() + d3.size());
  for (i = 0; i < d3.size(); ++i) {
    D[i] = d3[i];
  }
  ni = d3.size();
  for (i = ni; i < ni + d2.size(); ++i) {
    D[i] = d2[i - ni];
  }
  ni = d3.size() + d2.size();
  for (i = ni; i < ni + d1.size(); ++i) {
    D[i] = d1[i - ni];
  }

  return 0;
}

int main() {
  while (true) {
    int n, m, p, q, r;
    deck D;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d%d%d%d", &m, &p, &q, &r);
    D.push_back(range(1, n));
    for (int i = 0; i < m; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      shuffle(x, y, D);
      // compress
      // CompressDeck( D );
      /*
      for(deck::iterator itd=D.begin();itd!=D.end();++itd){
        cout << '(' << itd->first << " - " << itd->second << ')' << endl;
      }
      cout << endl;
      */
    }
    printf("%d\\n", shuffle(p, q, D, true, r));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>

using namespace std;

typedef pair<int, int> range;
typedef list<range> deck;

inline int quant(const range &r) { return r.second - r.first + 1; }

int shuffle(int x, int y, deck &D, bool count = false, int r = 0) {
  deck d1, d2, d3;

  // part I
  int precnt = 0, cnt = 0;
  for (deck::iterator itd = D.begin(); itd != D.end(); ++itd) {
    cnt += quant(*itd);
    if (cnt >= x) {
      d1.push_back(range(itd->first, itd->first + (x - precnt) - 1));
      break;
    } else {
      d1.push_back(*itd);
    }
    precnt = cnt;
  }

  // part II
  precnt = cnt = 0;
  bool first = true;
  for (deck::iterator itd = D.begin(); itd != D.end(); ++itd) {
    cnt += quant(*itd);
    if (precnt <= x && precnt <= y && cnt > x && cnt > y) {
      d2.push_back(
          range(itd->first + (x - precnt), itd->first + (y - precnt) - 1));
      // cout << "A : " << d2.back().first << ' ' << d2.back().second << endl;
      break;
    } else if (cnt >= x && first) {
      d2.push_back(range(itd->first + (x - precnt), itd->second));
      // cout << "B : " << d2.back().first << ' ' << d2.back().second << endl;
      first = false;
    } else if (cnt >= x && cnt < y) {
      d2.push_back(*itd);
      // cout << "C : " << d2.back().first << ' ' << d2.back().second << endl;
    } else if (cnt >= y) {
      d2.push_back(range(itd->first, itd->first + (y - precnt) - 1));
      // cout << "D : " << d2.back().first << ' ' << d2.back().second << endl;
      break;
    }
    precnt = cnt;
  }

  // part III
  precnt = cnt = 0;
  first = true;
  for (deck::iterator itd = D.begin(); itd != D.end(); ++itd) {
    cnt += quant(*itd);
    if (cnt > y && first) {
      d3.push_back(range(itd->first + (y - precnt), itd->second));
      first = false;
    } else if (!first) {
      d3.push_back(*itd);
    }
    precnt = cnt;
  }

  D.clear();
  /*
    cout << "D1" << endl;
    for(deck::iterator itd=d1.begin();itd!=d1.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }cout << "D2" << endl;
    for(deck::iterator itd=d2.begin();itd!=d2.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }cout << "D3" << endl;
    for(deck::iterator itd=d3.begin();itd!=d3.end();++itd){
      cout << '(' << itd->first << " - " << itd->second << ')' << endl;
    }
    cout << endl;
  */

  if (count) {
    int ret = 0;
    if (d1.back().second <= r)
      ++ret;
    for (deck::iterator itd = d2.begin(); itd != d2.end(); ++itd) {
      // cout << itd->first << ' ' << itd->second << endl;
      if (itd->first <= r) {
        ret += (min(itd->second, r) - itd->first + 1);
      }
    }
    return ret;
  }

  D.insert(D.begin(), d1.begin(), d1.end());
  D.insert(D.begin(), d2.begin(), d2.end());
  D.insert(D.begin(), d3.begin(), d3.end());

  return 0;
}

void CompressDeck(deck &D) {
  deck::iterator itd = D.begin();
  for (; itd != D.end(); ++itd) {
    deck::iterator itnx = itd;
    itnx++;
    if (itnx == D.end())
      break;
    if (itd->second + 1 == itnx->first) {
      itd->second = itnx->second;
      itd = D.erase(itnx);
      --itd;
      --itd;
    }
  }
}

int main() {
  while (true) {
    int n, m, p, q, r;
    deck D;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d%d%d%d", &m, &p, &q, &r);
    D.push_back(range(1, n));
    for (int i = 0; i < m; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      shuffle(x, y, D);
      // compress
      CompressDeck(D);
      /*
      for(deck::iterator itd=D.begin();itd!=D.end();++itd){
        cout << '(' << itd->first << " - " << itd->second << ')' << endl;
      }
      cout << endl;
      */
    }
    printf("%d\\n", shuffle(p, q, D, true, r));
  }
  return 0;
}