Problem p00920 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define MAX 300005
int m, n, A, B;
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;
}

int x[MAX], y[MAX], z[MAX];
set<P> t[MAX];

void insert(set<P> &s, P p) {
  set<P>::iterator it;
  while (1) {
    it = s.upper_bound(p);
    if (it == s.end())
      break;

    if (p.first <= it->first && p.second <= it->second) {
      s.erase(it);
    } else {
      break;
    }
  }
  s.insert(p);
}

bool check(set<P> &s, P p) {
  set<P>::iterator it;
  it = s.upper_bound(p);
  int cc = 0;
  while (it != s.begin()) {
    cc++;
    if (cc > 3)
      break;

    it--;
    if (it->first < p.first)
      if (it->second < p.second)
        return true;
  }
  return false;
}

int calc(P p) {
  int left = 0, right = m + n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (check(t[mid], p))
      left = mid + 1;
    else
      right = mid;
  }
  return left;
}

vector<P> G[1000005];

int solve() {
  int ans = 1;

  for (int i = 0; i <= m + n; i++) {
    t[i].clear();
  }
  t[0].insert(P(-1, -1));
  set<int> st;

  for (int i = 0; i < m + n; i++) {
    st.insert(z[i]);
    G[z[i]].push_back(P(x[i], y[i]));
  }

  for (int i : st) {
    vector<int> v(G[i].size());
    for (int j = 0; j < (int)G[i].size(); j++) {
      v[j] = calc(G[i][j]);
      ans = max(ans, v[j]);
    }
    for (int j = 0; j < (int)G[i].size(); j++) {
      insert(t[v[j]], G[i][j]);
    }
    G[i].clear();
  }
  /*
  for(int i=0;i<=ans;i++){
    cout<<i<<" :"<<endl;
    for(P p:t[i]){
      cout<<p.first<<' '<<p.second<<endl;
    }
    cout<<endl;
    cout<<endl;
  }
  cout<<endl;
  */
  return ans;
}

int main() {
  while (1) {
    scanf("%d %d %d %d", &m, &n, &A, &B);
    if (m == 0 && n == 0 && A == 0 && B == 0)
      break;

    a = A, b = B;

    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &x[i], &y[i], &z[i]);
    }

    for (int i = m; i < m + n; i++) {
      x[i] = r();
      y[i] = r();
      z[i] = r();
    }
    printf("%d\n", solve());
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define MAX 300005
int m, n, A, B;
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;
}

int x[MAX], y[MAX], z[MAX];
set<P> t[MAX];

void insert(set<P> &s, P p) {
  set<P>::iterator it;
  while (1) {
    it = s.upper_bound(p);
    if (it == s.end())
      break;

    if (p.first <= it->first && p.second <= it->second) {
      s.erase(it);
    } else {
      break;
    }
  }
  s.insert(p);
}

bool check(set<P> &s, P p) {
  set<P>::iterator it;
  it = s.upper_bound(p);
  int cc = 0;
  while (it != s.begin()) {
    cc++;
    if (cc > 3)
      break;

    it--;
    if (it->first < p.first)
      if (it->second < p.second)
        return true;
  }
  return false;
}

int calc(P p) {
  int left = 0, right = m + n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (check(t[mid], p))
      left = mid + 1;
    else
      right = mid;
  }
  return left;
}

vector<P> G[1000005];

int solve() {
  int ans = 1;

  for (int i = 0; i <= m + n; i++) {
    t[i].clear();
  }
  t[0].insert(P(-1, -1));
  set<int> st;

  for (int i = 0; i < m + n; i++) {
    st.insert(z[i]);
    G[z[i]].push_back(P(x[i], y[i]));
  }

  for (int i : st) {
    vector<int> v(G[i].size());
    for (int j = 0; j < (int)G[i].size(); j++) {
      v[j] = calc(G[i][j]);
      ans = max(ans, v[j]);
    }
    for (int j = 0; j < (int)G[i].size(); j++) {
      if (check(t[v[j]], G[i][j]) == false)
        insert(t[v[j]], G[i][j]);
    }
    G[i].clear();
  }
  /*
  for(int i=0;i<=ans;i++){
    cout<<i<<" :"<<endl;
    for(P p:t[i]){
      cout<<p.first<<' '<<p.second<<endl;
    }
    cout<<endl;
    cout<<endl;
  }
  cout<<endl;
  */
  return ans;
}

int main() {
  while (1) {
    scanf("%d %d %d %d", &m, &n, &A, &B);
    if (m == 0 && n == 0 && A == 0 && B == 0)
      break;

    a = A, b = B;

    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &x[i], &y[i], &z[i]);
    }

    for (int i = m; i < m + n; i++) {
      x[i] = r();
      y[i] = r();
      z[i] = r();
    }
    printf("%d\n", solve());
  }
}