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

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

#pragma warning(disable : 4996)

using namespace std;

int n, m, p, q, c, r1, r2;

vector<tuple<int, int, int>> dat;

inline void divide(int pos) {
  vector<tuple<int, int, int>> dat1;

  int u = dat.size();

  for (int i = 0; i < u; i++) {
    int l = get<0>(dat[i]);
    int r = get<1>(dat[i]);
    int t = get<2>(dat[i]);

    if (l < pos && pos < r) {
      dat[i] = make_tuple(l, pos, t);

      dat.insert(dat.begin() + i + 1, make_tuple(pos, r, t + pos - l));
    }
  }
}

inline void shuffle(int a, int b) {
  divide(a);
  divide(b);

  int l =
      lower_bound(dat.begin(), dat.end(), make_tuple(a, 0, 0)) - dat.begin();
  int r =
      lower_bound(dat.begin(), dat.end(), make_tuple(b, 0, 0)) - dat.begin();
  int u = dat.size();

  vector<tuple<int, int, int>> dat1;

  int pos = 0;

  for (int i = r; i < u; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  for (int i = l; i < r; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  for (int i = 0; i < l; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  dat = dat1;
}

int main() {
  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    scanf("%d", &m);
    scanf("%d", &p);
    p--;
    scanf("%d", &q);
    scanf("%d", &c);

    dat = {make_tuple(0, n, 1)};

    for (int i = 0; i < m; i++) {
      scanf("%d", &r1);
      scanf("%d", &r2);

      shuffle(r1, r2);
    }

    int ret = 0;

    for (int i = 0; i < dat.size(); i++) {
      int l = get<0>(dat[i]);
      int r = get<1>(dat[i]);
      int t = get<2>(dat[i]);

      if (p <= l && r <= q) {
        ret += min(max(c - t + 1, 0), r - l);
      } else if (l <= p && q <= r) {
        ret += min(max(c - (t + (p - l)), 0), q - p);
      } else if (p <= l && l < q) {
        ret += min(max(c - t + 1, 0), q - l);
      } else if (p < r && r <= q) {
        ret += min(max(c - (t + (p - l)) + 1, 0), r - p);
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int n, m, p, q, c, r1, r2;

vector<tuple<int, int, int>> dat;

inline void divide(int pos) {
  vector<tuple<int, int, int>> dat1;

  for (int i = 0; i < dat.size(); i++) {
    int l = get<0>(dat[i]);
    int r = get<1>(dat[i]);
    int t = get<2>(dat[i]);

    if (l < pos && pos < r) {
      dat1.push_back(make_tuple(l, pos, t));
      dat1.push_back(make_tuple(pos, r, t + pos - l));
    } else {
      dat1.push_back(make_tuple(l, r, t));
    }
  }

  dat = dat1;
}

inline void shuffle(int a, int b) {
  divide(a);
  divide(b);

  int l =
      lower_bound(dat.begin(), dat.end(), make_tuple(a, 0, 0)) - dat.begin();
  int r =
      lower_bound(dat.begin(), dat.end(), make_tuple(b, 0, 0)) - dat.begin();
  int u = dat.size();

  vector<tuple<int, int, int>> dat1;

  int pos = 0;

  for (int i = r; i < u; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  for (int i = l; i < r; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  for (int i = 0; i < l; i++) {
    dat1.push_back(
        make_tuple(pos, pos - get<0>(dat[i]) + get<1>(dat[i]), get<2>(dat[i])));
    pos += get<1>(dat[i]) - get<0>(dat[i]);
  }

  dat = dat1;
}

int main() {
  while (true) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }

    scanf("%d", &m);
    scanf("%d", &p);
    p--;
    scanf("%d", &q);
    q--;
    scanf("%d", &c);

    dat = {make_tuple(0, n, 1)};

    for (int i = 0; i < m; i++) {
      scanf("%d", &r1);
      scanf("%d", &r2);

      shuffle(r1, r2);
    }

    int ret = 0;

    for (int i = 0; i < dat.size(); i++) {
      int l = get<0>(dat[i]);
      int r = get<1>(dat[i]);
      int t = get<2>(dat[i]);

      if (p <= l && r <= q + 1) {
        ret += min(max(c - t + 1, 0), r - l);
      } else if (p <= l && l <= q) {
        ret += min(max(c - t + 1, 0), q - l + 1);
      } else if (p <= r && r <= q) {
        ret += max(c - (t + (p - l)) + 1, 0);
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}