Problem:
Problem G: Longest Chain

Let us compare two triples a = (xa, ya, za) and b = (xb, yb, zb) by a partial order ∠ defined as follows.

a ∠ b ⇔ xa < xb and ya < yb and za < zb

Your mission is to find, in the given set of triples, the longest ascending series a1 ∠ a2 ∠ ... ∠ ak.

Input

The input is a sequence of datasets, each specifying a set of triples formatted as follows.

m n A B
x1 y1 z1
x2 y2 z2
...
xm ym zm

Here, m, n, A and B in the first line, and all of xi, yi and zi (i = 1, . . . , m) in the following lines are non-negative integers.

Each dataset specifies a set of m + n triples. The triples p1 through pm are explicitly specified in the dataset, the i-th triple pi being (xi, yi, zi). The remaining n triples are specified by parameters A and B given to the following generator routine.

int a = A, b = 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;
}

Repeated 3n calls of r() defined as above yield values of xm+1, ym+1, zm+1, xm+2, ym+2, zm+2, ..., xm+n, ym+n, and zm+n, in this order.

You can assume that 1 ≤ m + n ≤ 3 × 105, 1 ≤ A,B ≤ 216, and 0 ≤ xk, yk, zk < 106 for 1 ≤ k ≤ m + n.

The input ends with a line containing four zeros. The total of m + n for all the datasets does not exceed 2 × 106.

Output

For each dataset, output the length of the longest ascending series of triples in the specified set. If pi1 ∠ pi2 ∠ ... ∠ pik is the longest, the answer should be k.

Sample Input

6 0 1 1
0 0 0
0 2 2
1 1 1
2 0 2
2 2 0
2 2 2
5 0 1 1
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
10 0 1 1
3 0 0
2 1 0
2 0 1
1 2 0
1 1 1
1 0 2
0 3 0
0 2 1
0 1 2
0 0 3
0 10 1 1
0 0 0 0

Output for the Sample Input

3
5
1
3

Solution in C++14 (judge verdict: Time Limit Exceeded)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<int, P> Pint;
typedef pair<P, P> PP;
int a, b, C, M;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}
const int MAX_N=1<<19;
int m0;
vector<P> v1[2*MAX_N-1];
vector<int> v[2*MAX_N-1];
int m[2*MAX_N-1];
vector<Pint> p;
  
void init(int m_){
    m0=1;
    while(m0<m_) m0*=2;
    for(int i=0; i<2*m0-1; i++){
        v[i].clear(); v1[i].clear();
      vector<int>().swap(v[i]);
      vector<P>().swap(v1[i]);
    }
  for(int i=0; i<p.size(); i++){
    int x=p[i].first, y=p[i].second.first, z=p[i].second.second;
      int i0=y+m0-1;
      v1[i0].push_back(P(z, i));
      while(i0>0){
        i0=(i0-1)/2;
        v1[i0].push_back(P(z, i));
      }
  }
  for(int i=0; i<2*m0-1; i++){
    if(v1[i].empty()) continue;
    sort(v1[i].begin(), v1[i].end());
    m[i]=1;
    while(m[i]<v1[i].size()) m[i]*=2;
    for(int j=0; j<2*m[i]-1; j++){
      if(j<m[i]-1) v[i].push_back(0);
      else v[i].push_back(0);
    }
  }
}
  
void update(int k, int i, int a){
    i+=(m[k]-1);
    v[k][i]=a;
    while(i>0){
        i=(i-1)/2;
        v[k][i]=max(v[k][i*2+1], v[k][i*2+2]);
    }
}
 
void update0(int i, int y, int z, int a, int k, int l, int r){
    if(r<=y || y+1<=l) return;
  int i0=lower_bound(v1[k].begin(), v1[k].end(), P(z, i))-v1[k].begin();
  if(i0<v1[k].size()){
    update(k, i0, a);
  }
  if(r-l>1){
    update0(i, y, z, a, 2*k+1, l, (l+r)/2);
    update0(i, y, z, a, 2*k+2, (l+r)/2, r);
  }
}
 
int find(int k, int i, int k0, int l, int r){
  if(r<=0 || i<=l) return 0;
  if(0<=l && r<=i) return v[k][k0];
  int vl=find(k, i, 2*k0+1, l, (l+r)/2);
  int vr=find(k, i, 2*k0+2, (l+r)/2, r);
  return max(vl, vr);
}
 
int find0(int y, int z, int k, int l, int r){
    if(r<=0 || y<=l) return 0;
    if(0<=l && r<=y){
      if(v1[k].empty()) return 0;
      int i=lower_bound(v1[k].begin(), v1[k].end(), P(z, -1))-v1[k].begin();
      if(i==0) return 0;
        return find(k, i, 0, 0, m[k]);
    }else{
        int vl=find0(y, z, k*2+1, l, (l+r)/2);
        int vr=find0(y, z, k*2+2, (l+r)/2, r);
        return max(vl, vr);
    }
}
 
int main()
{
    while(1){
      int m, n, A, B;
      scanf("%d %d %d %d", &m, &n, &A, &B);
      if(m==0 && n==0 && A==0 && B==0) return 0;
      a=A, b=B, C=~(1<<31), M=(1<<16)-1;
      int mx=0;
      for(int i=0; i<m; i++){
        int x, y, z;
        cin>>x>>y>>z;
        p.push_back(Pint(x, P(y, z)));
      }
      for(int i=0; i<n; i++){
        int x=r();
        int y=r();
        int z=r();
        p.push_back(Pint(x, P(y, z)));
      }
      sort(p.begin(), p.end());
      p.erase(unique(p.begin(), p.end()), p.end());
	  int ind[300000];
	  for(int i=0; i<p.size(); i++) ind[i]=i;
	  sort(ind, ind+p.size(), [&](int x, int y){ return p[x].second.first<p[y].second.first;});
	  int ct=-1, pr=-1;
	  for(int i=0; i<p.size(); i++){
		  if(pr!=p[ind[i]].second.first){
			  pr=p[ind[i]].second.first;
			  ct++;
			  p[ind[i]].second.first=ct;
		  }else{
			  p[ind[i]].second.first=ct;
		  }
	  }
      init(ct+1);
      vector<PP> mxv;
      int xp=-1;
      for(int i=0; i<p.size(); i++){
        int x=p[i].first, y=p[i].second.first, z=p[i].second.second;
        if(xp==x){
          mxv.push_back(PP(P(i, y), P(z, find0(y, z, 0, 0, m0))));
        }else{
          for(auto t:mxv){
            update0(t.first.first, t.first.second, t.second.first, t.second.second+1, 0, 0, m0);
          }
          mxv.clear();
          xp=x;
          mxv.push_back(PP(P(i, y), P(z, find0(y, z, 0, 0, m0))));
        }
      }
      for(auto t:mxv){
        update0(t.first.first, t.first.second, t.second.first, t.second.second+1, 0, 0, m0);
      }
      printf("%d\n", find0(ct+1, 1000000, 0, 0, m0));
      p.clear();
    }
    return 0;
}