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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
struct Generator {
  int a, b, C, M;
  Generator(int A, int B) { a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1; }
  int generate() {
    a = 36969 * (a & M) + (a >> 16);
    b = 18000 * (b & M) + (b >> 16);
    return (C & ((a << 16) + b)) % 1000000;
  }
};

const int INF = 100000000;
typedef pair<int, int> Point;

void insert(int x, int y, set<Point> &s) {
  const Point np(x, y);
  auto itr = s.lower_bound(np);
  if ((--itr)->second <= y)
    return;
  itr++;
  vector<Point> removes;
  while (itr->second >= y) {
    removes.push_back(*itr);
    itr++;
  }
  for (Point p : removes) {
    s.erase(p);
  }
  s.insert(np);
  /*
  for(auto p : s){
      printf("(%d, %d) ", p.first, p.second);
  }
  cout << endl;
  */
}

int main() {
  int M, N, A, B;
  const int MAX = 1000000;
  while (cin >> M >> N >> A >> B && (M + N) != 0) {
    vector<vector<Point>> rect(MAX);
    for (int i = 0; i < M; i++) {
      int x;
      Point p;
      cin >> x >> p.first >> p.second;
      rect[x].push_back(p);
    }
    Generator gen(A, B);
    for (int i = 0; i < N; i++) {
      Point p;
      int x = gen.generate();
      p.first = gen.generate();
      p.second = gen.generate();
      rect[x].push_back(p);
    }

    vector<set<Point>> sets(N + M + 1);
    for (auto &s : sets) {
      s.insert(Point(INF, -1));
      s.insert(Point(-1, INF));
    }

    int answer = 0;
    for (const vector<Point> &points : rect) {
      vector<pair<int, Point>> querys;
      for (const Point &p : points) {
        int x = p.first, y = p.second;
        int l = -1, r = answer + 1;
        while (r - l > 1) {
          int c = (l + r) / 2;
          auto itr = sets[c].lower_bound(Point(x, -INF));
          if ((--itr)->second < y) {
            l = c;
          } else {
            r = c;
          }
        }
        querys.emplace_back(r, p);
        answer = max(answer, r + 1);
      }
      for (const auto &query : querys) {
        int k = query.first;
        Point p = query.second;
        // printf("%d (%d, %d)\\n", k, p.first, p.second);
        insert(p.first, p.second, sets[k]);
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
struct Generator {
  int a, b, C, M;
  Generator(int A, int B) { a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1; }
  int generate() {
    a = 36969 * (a & M) + (a >> 16);
    b = 18000 * (b & M) + (b >> 16);
    return (C & ((a << 16) + b)) % 1000000;
  }
};

const int INF = 100000000;
typedef pair<int, int> Point;

void insert(int x, int y, set<Point> &s) {
  const Point np(x, y);
  auto itr = s.lower_bound(np);
  if ((--itr)->second <= y)
    return;
  itr++;
  vector<Point> removes;
  while (itr->second >= y) {
    removes.push_back(*itr);
    itr++;
  }
  for (Point p : removes) {
    s.erase(p);
  }
  s.insert(np);
  /*
  for(auto p : s){
      printf("(%d, %d) ", p.first, p.second);
  }
  cout << endl;
  */
}

int main() {
  int M, N, A, B;
  const int MAX = 1000000;
  while (cin >> M >> N >> A >> B && (M + N) != 0) {
    vector<vector<Point>> rect(MAX);
    for (int i = 0; i < M; i++) {
      int x;
      Point p;
      cin >> x >> p.first >> p.second;
      rect[x].push_back(p);
    }
    Generator gen(A, B);
    for (int i = 0; i < N; i++) {
      Point p;
      int x = gen.generate();
      p.first = gen.generate();
      p.second = gen.generate();
      rect[x].push_back(p);
    }

    vector<set<Point>> sets(MAX + 1);
    for (auto &s : sets) {
      s.insert(Point(INF, -1));
      s.insert(Point(-1, INF));
    }

    int answer = 0;
    for (vector<Point> points : rect) {
      vector<pair<int, Point>> querys;
      for (const Point &p : points) {
        int x = p.first, y = p.second;
        int l = -1, r = answer + 1;
        while (r - l > 1) {
          int c = (l + r) / 2;
          auto itr = sets[c].lower_bound(Point(x, -INF));
          if ((--itr)->second < y) {
            l = c;
          } else {
            r = c;
          }
        }
        querys.emplace_back(r, p);
        answer = max(answer, r + 1);
      }
      for (const auto &query : querys) {
        int k = query.first;
        Point p = query.second;
        // printf("%d (%d, %d)\\n", k, p.first, p.second);
        insert(p.first, p.second, sets[k]);
      }
    }
    cout << answer << endl;
  }
  return 0;
}