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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <queue>
#include <vector>
#define MAX_N 100000
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;

const int INF = 1e9;

int main() {
  int n, m, pp, q, r;

  while (cin >> n && n) {
    int save = -1, ans = 0;
    vector<int> vep, vep2, vep3;
    vep.push_back(1);
    vep.push_back(n);
    vector<int> vec, vec2, vec3;
    vec.push_back(1);
    vec.push_back(n);
    cin >> m >> pp >> q >> r;
    for (int i = 0; i < m; i++) {
      int x, y, p, p2;
      scanf("%d%d", &x, &y);
      p = lower_bound(vec.begin(), vec.end(), x) - vec.begin();
      if (vec[p] != x) {
        vec.insert(vec.begin() + p, x);
        vep.insert(vep.begin() + p, vec[p] - vec[p - 1] + vep[p - 1]);
      }
      if (vec[p] + 1 != vec[p + 1]) {
        vec.insert(vec.begin() + p + 1, x + 1);
        vep.insert(vep.begin() + p + 1, vep[p] + 1);
      }
      p2 = lower_bound(vec.begin(), vec.end(), y) - vec.begin();
      if (vec[p2] != y) {
        vec.insert(vec.begin() + p2, y);
        vep.insert(vep.begin() + p2, vec[p2] - vec[p2 - 1] + vep[p2 - 1]);
      }
      if (vec[p2] + 1 != vec[p2 + 1]) {
        vec.insert(vec.begin() + p2 + 1, y + 1);
        vep.insert(vep.begin() + p2 + 1, vep[p2] + 1);
      }
      for (int j = p2 + 1; j < vec.size(); j++) {
        vec2.push_back(vec[j] - vec[p2 + 1] + 1);
        vep2.push_back(vep[j]);
      }
      int size = vec2[vec2.size() - 1];
      for (int j = p + 1; j < p2 + 1; j++) {
        vec2.push_back(vec[j] - vec[p + 1] + 1 + size);
        vep2.push_back(vep[j]);
      }
      size = vec2[vec2.size() - 1];
      for (int j = 0; j < p + 1; j++) {
        vec2.push_back(vec[j] - vec[0] + 1 + size);
        vep2.push_back(vep[j]);
      }
      vec3 = vec;
      vep3 = vep;
      vec = vec2;
      vep = vep2;
      vec2 = vec3;
      vep2 = vep3;
      vec2.clear();
      vep2.clear();
      /*cout << endl;
      for (int i = 0; i < vec.size(); i++) {
              cout << vec[i] << "  " << vep[i] << endl;
      }
      cout << endl;*/
    }
    int p, p2;
    p = lower_bound(vec.begin(), vec.end(), pp) - vec.begin();

    if (vec[p] != pp) {
      vec.insert(vec.begin() + p, pp);
      vep.insert(vep.begin() + p, vec[p] - vec[p - 1] + vep[p - 1]);
    }
    if (vec[p] + 1 != vec[p + 1]) {
      vec.insert(vec.begin() + p + 1, pp + 1);
      vep.insert(vep.begin() + p + 1, vep[p] + 1);
    }
    p2 = lower_bound(vec.begin(), vec.end(), q) - vec.begin();
    if (vec[p2] != q) {
      vec.insert(vec.begin() + p2, q);
      vep.insert(vep.begin() + p2, vec[p2] - vec[p2 - 1] + vep[p2 - 1]);
    }
    if (vec[p2] + 1 != vec[p2 + 1]) {
      vec.insert(vec.begin() + p2 + 1, q + 1);
      vep.insert(vep.begin() + p2 + 1, vep[p2] + 1);
    }
    for (int i = p; i <= p2; i++) {
      if (i + 1 <= p2 && vep[i + 1] - vep[i] == vec[i + 1] - vec[i]) {
        if (vep[i + 1] <= r) {
          ans += vep[i + 1] - vep[i];
          if (save != vep[i])
            ans++;
          save = vep[i + 1];
        }
        if (vep[i] <= r && vep[i + 1] > r) {
          ans += r - vep[i];
          if (save != vep[i])
            ans++;
          save = q;
        }
      } else {
        if (vep[i] <= r && save != vep[i]) {
          ans++;
          save = vep[i];
        }
      }
    }
    cout << ans << endl;
  }
} // 0 172

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <queue>
#include <vector>
#define MAX_N 100000
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;

const int INF = 1e9;

int main() {
  int n, m, pp, q, r;

  while (cin >> n && n) {
    int save = -1, ans = 0;
    vector<int> vep, vep2;
    vep.push_back(1);
    vep.push_back(n);
    vector<int> vec, vec2;
    vec.push_back(1);
    vec.push_back(n);
    cin >> m >> pp >> q >> r;
    for (int i = 0; i < m; i++) {
      int x, y, p, p2;
      scanf("%d%d", &x, &y);
      p = lower_bound(vec.begin(), vec.end(), x) - vec.begin();
      if (vec[p] != x) {
        vec.insert(vec.begin() + p, x);
        vep.insert(vep.begin() + p, vec[p] - vec[p - 1] + vep[p - 1]);
      }
      if (vec[p] + 1 != vec[p + 1]) {
        vec.insert(vec.begin() + p + 1, x + 1);
        vep.insert(vep.begin() + p + 1, vep[p] + 1);
      }
      p2 = lower_bound(vec.begin(), vec.end(), y) - vec.begin();
      if (vec[p2] != y) {
        vec.insert(vec.begin() + p2, y);
        vep.insert(vep.begin() + p2, vec[p2] - vec[p2 - 1] + vep[p2 - 1]);
      }
      if (vec[p2] + 1 != vec[p2 + 1]) {
        vec.insert(vec.begin() + p2 + 1, y + 1);
        vep.insert(vep.begin() + p2 + 1, vep[p2] + 1);
      }
      for (int j = p2 + 1; j < vec.size(); j++) {
        vec2.push_back(vec[j] - vec[p2 + 1] + 1);
        vep2.push_back(vep[j]);
      }
      int size = vec2[vec2.size() - 1];
      for (int j = p + 1; j < p2 + 1; j++) {
        vec2.push_back(vec[j] - vec[p + 1] + 1 + size);
        vep2.push_back(vep[j]);
      }
      size = vec2[vec2.size() - 1];
      for (int j = 0; j < p + 1; j++) {
        vec2.push_back(vec[j] - vec[0] + 1 + size);
        vep2.push_back(vep[j]);
      }
      copy(vec.begin(), vec.end(), back_inserter(vec2));
      vec2.clear();
      copy(vep.begin(), vep.end(), back_inserter(vep2));
      vep2.clear();
    }
    int p, p2;
    p = lower_bound(vec.begin(), vec.end(), pp) - vec.begin();

    if (vec[p] != pp) {
      vec.insert(vec.begin() + p, pp);
      vep.insert(vep.begin() + p, vec[p] - vec[p - 1] + vep[p - 1]);
    }
    if (vec[p] + 1 != vec[p + 1]) {
      vec.insert(vec.begin() + p + 1, pp + 1);
      vep.insert(vep.begin() + p + 1, vep[p] + 1);
    }
    p2 = lower_bound(vec.begin(), vec.end(), q) - vec.begin();
    if (vec[p2] != q) {
      vec.insert(vec.begin() + p2, q);
      vep.insert(vep.begin() + p2, vec[p2] - vec[p2 - 1] + vep[p2 - 1]);
    }
    if (vec[p2] + 1 != vec[p2 + 1]) {
      vec.insert(vec.begin() + p2 + 1, q + 1);
      vep.insert(vep.begin() + p2 + 1, vep[p2] + 1);
    }
    for (int i = p; i <= p2; i++) {
      if (i + 1 <= p2 && vep[i + 1] - vep[i] == vec[i + 1] - vec[i]) {
        for (int j = vep[i]; j <= vep[i + 1]; j++) {
          if (j <= r && save != j) {
            ans++;
            save = j;
          }
        }
      } else {
        if (vep[i] <= r && save != vep[i]) {
          ans++;
          save = vep[i];
        }
      }
    }
    cout << ans << endl;
  }
}