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

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<P> v;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n, m;
  int a, b, r;
  int i, j, k;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    scanf("%d", &m);
    scanf("%d %d %d", &a, &b, &r);
    a--;
    v.clear();
    v.push_back(P(0, 1));
    for (i = 0; i < m; i++) {
      int p, q;
      scanf("%d %d", &p, &q);
      P lp = P(-1, 0), up = P(-1, 0);
      for (j = 0; j < v.size(); j++) {
        if (v[j].first <= p && v[j].first > lp.first) {
          lp.first = v[j].first;
          lp.second = (p - v[j].first) + v[j].second;
        }
        if (v[j].first <= q && v[j].first > up.first) {
          up.first = v[j].first;
          up.second = (q - v[j].first) + v[j].second;
        }
        if (v[j].first < p)
          v[j].first += n - p;
        else if (v[j].first < q)
          v[j].first = v[j].first - p + n - q;
        else
          v[j].first -= q;
      }
      v.push_back(P(n - q, lp.second));
      v.push_back(P(0, up.second));
    }
    v.push_back(P(n, 0));
    sort(v.begin(), v.end());
    int ans = 0;
    for (i = 0; i < v.size() - 1; i++) {
      if (v[i].first == v[i + 1].first)
        continue;
      if (v[i].second <= r) {
        int l = max(v[i].first, a) - v[i].first + v[i].second;
        int u = min(min(v[i + 1].first, b) - v[i].first + v[i].second, r + 1);
        ans += max(0, u - l);
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
typedef struct pair<int, int> P;
vector<P> v;
int search(int x) {
  int l = 0, r = v.size(), mid;
  int i;
  for (i = 0; i < 20; i++) {
    mid = (l + r) / 2;
    if (v[mid].first == x)
      return mid;
    else if (v[mid].first > x)
      r = mid;
    else
      l = mid;
  }
  return mid;
}
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m, p, q, r;
  int i, j;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    scanf("%d", &m);
    scanf("%d %d %d", &p, &q, &r);
    p--;
    q--;
    v.clear();
    v.push_back(P(-1, 0));
    v.push_back(P(0, r));
    for (i = 0; i < m; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      int x = search(a);
      int y = search(b);
      printf("%d %d\\n", x, y);
      if (b <= v[y].first + v[y].second) {
        v.push_back(P(b, v[y].first + v[y].second - b));
        v[y].second -= v[y].first + v[y].second - b;
      }
      if (a <= v[x].first + v[x].second) {
        v.push_back(P(n, v[x].first + v[x].second - a));
        v[x].second -= v[x].first + v[x].second - a;
      }
      for (j = 1; j <= x; j++)
        v[j].first += n - a;
      for (j = x + 1; j <= y; j++)
        v[j].first += (n - b) - a;
      for (j = y + 1; j < v.size(); j++)
        v[j].first -= b;
      sort(v.begin(), v.end());
    }
    int ans = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i].first >= p && v[i].first <= q) {
        ans += min(v[i].second, q - v[i].first + 1);
      }
    }
    printf("%d\\n", ans);
  }
}