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

#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int nil = 0;
int func(int a, int b) { return max(a, b); }

class SparseRMQ {
  map<int, int> mp;

public:
  void insert(int x, int v) {
    map<int, int>::iterator it, s, e;
    it = mp.lower_bound(x);
    if (it != mp.end() && it->first == x) {
      if (v <= it->second)
        return;
      it->second = v;
      s = it;
      ++s;
    } else if (it != mp.begin()) {
      s = it;
      if ((--it)->second >= v)
        return;
      mp.insert(it, make_pair(x, v));
    } else {
      s = it;
      mp.insert(it, make_pair(x, v));
    }
    e = s;
    while (e != mp.end() && e->second <= v)
      ++e;
    mp.erase(s, e);
  }
  int max(int xe) const { //(-inf,xe)
    map<int, int>::const_iterator it;
    it = mp.lower_bound(xe);
    if (it == mp.begin())
      return nil;
    return (--it)->second;
  }
};

class Dynamic2DRMQ_BIT {
  std::vector<SparseRMQ *> bit;

public:
  Dynamic2DRMQ_BIT(int xsize) : bit(xsize + 1, (SparseRMQ *)NULL) {}
  ~Dynamic2DRMQ_BIT() {
    for (int i = 0; i < (int)bit.size(); i++)
      if (bit[i])
        delete bit[i];
  }
  void insert(int x, int y, int v) {
    x++;
    while (x < (int)bit.size()) {
      if (!bit[x])
        bit[x] = new SparseRMQ();
      bit[x]->insert(y, v);
      x += x & -x;
    }
  }
  int query(int xe, int ye) const { //[0,xe)*[0,ye)
    int res = nil;
    while (0 < xe) {
      if (bit[xe])
        res = func(res, bit[xe]->max(ye));
      xe -= xe & -xe;
    }
    return res;
  }
};

int m, n, A, B;

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

int main() {
  while (cin >> m >> n >> A >> B, m | n) {
    vector<pair<int, pair<int, int>>> v(n + m), w;
    for (int i = 0; i < m; i++) {
      cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    }
    for (int i = m; i < m + n; i++) {
      v[i].first = r();
      v[i].second.first = r();
      v[i].second.second = r();
    }
    sort(v.begin(), v.end());
    int ans = 0;
    Dynamic2DRMQ_BIT rmq2d(2000000);
    int prevx = -1;
    for (int i = 0; i < m + n; i++) {
      if (prevx < v[i].first) {
        for (int j = 0; j < w.size(); j++)
          rmq2d.insert(w[j].second.first, w[j].second.second, w[j].first);
        w.clear();
        prevx = v[i].first;
      }
      int t = rmq2d.query(v[i].second.first, v[i].second.second) + 1;
      ans = max(ans, t);
      w.push_back(
          make_pair(t, make_pair(v[i].second.first, v[i].second.second)));
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;

const int nil = 0;
int func(int a, int b) { return max(a, b); }

class SparseRMQ {
  map<int, int> mp;

public:
  void insert(int x, int v) {
    map<int, int>::iterator it, s, e;
    it = mp.lower_bound(x);
    if (it != mp.end() && it->first == x) {
      if (v <= it->second)
        return;
      it->second = v;
      s = it;
      ++s;
    } else if (it != mp.begin()) {
      s = it;
      if ((--it)->second >= v)
        return;
      mp.insert(it, make_pair(x, v));
    } else {
      s = it;
      mp.insert(it, make_pair(x, v));
    }
    e = s;
    while (e != mp.end() && e->second <= v)
      ++e;
    mp.erase(s, e);
  }
  int max(int xe) const { //(-inf,xe)
    map<int, int>::const_iterator it;
    it = mp.lower_bound(xe);
    if (it == mp.begin())
      return nil;
    return (--it)->second;
  }
};

class Dynamic2DRMQ_BIT {
  std::vector<SparseRMQ> bit;

public:
  Dynamic2DRMQ_BIT(int xsize) : bit(xsize + 1) {}
  void insert(int x, int y, int v) {
    x++;
    while (x < (int)bit.size()) {
      bit[x].insert(y, v);
      x += x & -x;
    }
  }
  int query(int xe, int ye) const { //[0,xe)*[0,ye)
    int res = nil;
    while (0 < xe) {
      res = func(res, bit[xe].max(ye));
      xe -= xe & -xe;
    }
    return res;
  }
};

int m, n, A, B;

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

int main() {
  while (cin >> m >> n >> A >> B, m | n) {
    vector<pair<int, pair<int, int>>> v(n + m), w;
    for (int i = 0; i < m; i++) {
      cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    }
    for (int i = m; i < m + n; i++) {
      v[i].first = r();
      v[i].second.first = r();
      v[i].second.second = r();
    }
    sort(v.begin(), v.end());
    int ans = 0;
    Dynamic2DRMQ_BIT rmq2d(2000000);
    int prevx = -1;
    for (int i = 0; i < m + n; i++) {
      if (prevx < v[i].first) {
        for (int j = 0; j < w.size(); j++)
          rmq2d.insert(w[j].second.first, w[j].second.second, w[j].first);
        w.clear();
        prevx = v[i].first;
      }
      int t = rmq2d.query(v[i].second.first, v[i].second.second) + 1;
      ans = max(ans, t);
      w.push_back(
          make_pair(t, make_pair(v[i].second.first, v[i].second.second)));
    }

    cout << ans << endl;
  }
  return 0;
}