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

#include <bits/stdc++.h>
using namespace std;

int a, b, C = ~(1 << 31), M = (1 << 16) - 1;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}

class HappySequence {
public:
  map<int, int> seq;
  HappySequence() {
    int inf = 1e9;
    seq[-inf] = -inf;
  }
  bool insert(int key, int val) {
    if ((--seq.upper_bound(key))->second >= val)
      return false;
    seq[key] = val;
    auto it = ++seq.find(key);
    while (it != seq.end() && (*it).second <= val)
      seq.erase(it++);
    return true;
  }
};

HappySequence seg[1000100];

void ins(int y, int z, int v) {
  for (int i = y; i < 1000100; i += i & -i)
    seg[i].insert(z, v);
}
int get(int y, int z) {
  int v = 0;
  for (int i = y; i > 0; i -= i & -i) {
    v = max(v, (--seg[i].seq.lower_bound(z))->second);
  }
  return v;
}

int main() {
  int n, m, A, B;
  while (cin >> m >> n >> A >> B && n + m) {
    a = A;
    b = B;
    vector<array<int, 3>> ps;
    vector<int> uy;
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      ps.push_back(array<int, 3>{x, y, z});
      uy.push_back(y);
    }
    for (int i = 0; i < n; i++) {
      int x = r();
      int y = r();
      int z = r();
      ps.push_back(array<int, 3>{x, y, z});
      uy.push_back(y);
    }
    sort(uy.begin(), uy.end());
    uy.erase(unique(uy.begin(), uy.end()), uy.end());
    for (int i = 0; i <= uy.size(); i++)
      seg[i] = HappySequence();
    sort(ps.begin(), ps.end(), [&](array<int, 3> a, array<int, 3> b) {
      if (a[0] != b[0])
        return a[0] < b[0];
      return make_pair(a[1], a[2]) > make_pair(b[1], b[2]);
    });
    int ans = 0;
    for (auto p : ps) {
      p[1] = lower_bound(uy.begin(), uy.end(), p[1]) - uy.begin() + 1;
      int mx = get(p[1] - 1, p[2]) + 1;
      ans = max(ans, mx);
      ins(p[1], p[2], mx);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int a, b, C = ~(1 << 31), M = (1 << 16) - 1;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}

class HappySequence {
public:
  map<int, int> seq;
  HappySequence() {
    int inf = 1e9;
    seq[-inf] = -inf;
  }
  bool insert(int key, int val) {
    if ((--seq.upper_bound(key))->second >= val)
      return false;
    seq[key] = val;
    auto it = ++seq.find(key);
    while (it != seq.end()) {
      if ((*it).second <= val)
        seq.erase(it++);
      else
        it++;
    }
    return true;
  }
};

HappySequence seg[1000100];

void ins(int y, int z, int v) {
  for (int i = y; i < 1000100; i += i & -i)
    seg[i].insert(z, v);
}
int get(int y, int z) {
  int v = 0;
  for (int i = y; i > 0; i -= i & -i) {
    v = max(v, (--seg[i].seq.lower_bound(z))->second);
  }
  return v;
}

int main() {
  int n, m, A, B;
  while (cin >> m >> n >> A >> B && n + m) {
    a = A;
    b = B;
    vector<array<int, 3>> ps;
    vector<int> uy;
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      ps.push_back(array<int, 3>{x, y, z});
      uy.push_back(y);
    }
    for (int i = 0; i < n; i++) {
      int x = r();
      int y = r();
      int z = r();
      ps.push_back(array<int, 3>{x, y, z});
      uy.push_back(y);
    }
    sort(uy.begin(), uy.end());
    uy.erase(unique(uy.begin(), uy.end()), uy.end());
    for (int i = 0; i <= uy.size(); i++)
      seg[i] = HappySequence();
    sort(ps.begin(), ps.end(), [&](array<int, 3> a, array<int, 3> b) {
      if (a[0] != b[0])
        return a[0] < b[0];
      return make_pair(a[1], a[2]) > make_pair(b[1], b[2]);
    });
    int ans = 0;
    for (auto p : ps) {
      p[1] = lower_bound(uy.begin(), uy.end(), p[1]) - uy.begin() + 1;
      int mx = get(p[1] - 1, p[2]) + 1;
      ans = max(ans, mx);
      ins(p[1], p[2], mx);
    }
    cout << ans << endl;
  }
}