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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

int sz[10000];

void change(vector<P> &nums, int x, int y) {
  vector<P> res;
  int n = nums.size();
  int indx, indy;
  int ub = n + 1, lb = 0;
  sz[0] = 0;
  rep(i, n) { sz[i + 1] = sz[i] + nums[i].second - nums[i].first + 1; }

  while (ub - lb > 1) {
    int mb = (ub + lb) / 2;
    if (sz[mb] < x)
      lb = mb;
    else
      ub = mb;
  }
  indx = lb;
  ub = n + 1, lb = 0;

  while (ub - lb > 1) {
    int mb = (ub + lb) / 2;
    if (sz[mb] <= y)
      lb = mb;
    else
      ub = mb;
  }
  indy = lb;

  int dy = indy == n ? 0 : y - sz[indy], dx = indx == n ? 0 : x - sz[indx];

  res.push_back(P(nums[indy].first + dy, nums[indy].second));
  rep2(i, indy + 1, n) { res.push_back(nums[i]); }
  if (indx != indy)
    res.push_back(P(nums[indx].first + dx, nums[indx].second));
  else
    res.push_back(P(nums[indx].first + dx, nums[indx].first + dy - 1));
  rep2(i, indx + 1, indy) { res.push_back(nums[i]); }
  if (indx != indy && dy != 0)
    res.push_back(P(nums[indy].first, nums[indy].first + dy - 1));
  rep(i, indx) { res.push_back(nums[i]); }
  if (dx != 0)
    res.push_back(P(nums[indx].first, nums[indx].first + dx - 1));
  nums = res;
}

int get_num(P p, int r) {
  // cout << "***" << p.first << " " << p.second << endl;;
  if (p.second < p.first)
    return 0;
  if (r < p.first)
    return 0;
  else if (p.second <= r)
    return p.second - p.first + 1;
  else
    return r - p.first + 1;
}

int counter(const vector<P> &nums, int p, int q, int r) {
  int cnt = 1;
  int res = 0;
  int n = nums.size();
  rep(i, n) {
    //	cout << "+ " <<  nums[i].first << " " << nums[i].second << endl;
    int sz = nums[i].second - nums[i].first + 1;
    int pn = nums[i].first + p - cnt;
    int qn = nums[i].first + q - cnt;

    res += get_num(P(max(nums[i].first, pn), min(nums[i].second, qn)), r);
    cnt += sz;
  }
  return res;
}

int main() {
  int n, m;
  int p, q, r;
  int x, y;
  while (cin >> n && n) {
    vector<P> nums;
    nums.push_back(P(1, n));
    cin >> m;
    cin >> p >> q >> r;
    rep(i, m) {
      cin >> x >> y;
      change(nums, x, y);
    }
    cout << counter(nums, p, q, r) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

int sz[10000];

vector<P> change(const vector<P> &nums, int x, int y) {
  vector<P> res;
  int n = nums.size();
  int indx, indy;
  int ub = n + 1, lb = 0;
  sz[0] = 0;
  rep(i, n) { sz[i + 1] = sz[i] + nums[i].second - nums[i].first + 1; }

  while (ub - lb > 1) {
    int mb = (ub + lb) / 2;
    if (sz[mb] < x)
      lb = mb;
    else
      ub = mb;
  }
  indx = lb;
  ub = n + 1, lb = 0;

  while (ub - lb > 1) {
    int mb = (ub + lb) / 2;
    if (sz[mb] <= y)
      lb = mb;
    else
      ub = mb;
  }
  indy = lb;

  int dy = indy == n ? 0 : y - sz[indy], dx = indx == n ? 0 : x - sz[indx];

  res.push_back(P(nums[indy].first + dy, nums[indy].second));
  rep2(i, indy + 1, n) { res.push_back(nums[i]); }
  if (indx != indy)
    res.push_back(P(nums[indx].first + dx, nums[indx].second));
  else
    res.push_back(P(nums[indx].first + dx, nums[indx].first + dy - 1));
  rep2(i, indx + 1, indy) { res.push_back(nums[i]); }
  if (indx != indy && dy != 0)
    res.push_back(P(nums[indy].first, nums[indy].first + dy - 1));
  rep(i, indx) { res.push_back(nums[i]); }
  if (dx != 0)
    res.push_back(P(nums[indx].first, nums[indx].first + dx - 1));
  return res;
}

int get_num(P p, int r) {
  // cout << "***" << p.first << " " << p.second << endl;;
  if (p.second < p.first)
    return 0;
  if (r < p.first)
    return 0;
  else if (p.second <= r)
    return p.second - p.first + 1;
  else
    return r - p.first + 1;
}

int counter(const vector<P> &nums, int p, int q, int r) {
  int cnt = 1;
  int res = 0;
  int n = nums.size();
  rep(i, n) {
    //	cout << "+ " <<  nums[i].first << " " << nums[i].second << endl;
    int sz = nums[i].second - nums[i].first + 1;
    int pn = nums[i].first + p - cnt;
    int qn = nums[i].first + q - cnt;

    res += get_num(P(max(nums[i].first, pn), min(nums[i].second, qn)), r);
    cnt += sz;
  }
  return res;
}

int main() {
  int n, m;
  int p, q, r;
  int x, y;
  while (cin >> n && n) {
    vector<P> nums;
    nums.push_back(P(1, n));
    cin >> m;
    cin >> p >> q >> r;
    rep(i, m) {
      cin >> x >> y;
      nums = change(nums, x, y);
    }
    cout << counter(nums, p, q, r) << endl;
  }
  return 0;
}