Longest Chain#6784

Longest Chain

60 seconds
256 megabytes
Very easy

Let us define a partial order on triples \mathbf{a} = (x_a, y_a, z_a) and \mathbf{b} = (x_b, y_b, z_b) as follows:

\mathbf{a} \prec \mathbf{b} if x_a < x_b, y_a < y_b, and z_a < z_b.

Given a set of triples, find the length of the longest strictly increasing sequence of triples \mathbf{a}_1 \prec \mathbf{a}_2 \prec \dotsb \prec \mathbf{a}_k.

Input

The input consists of several datasets. Each dataset describes a set of triples in the following format:

  • m~n~A~B

  • x_1~y_1~z_1

  • x_2~y_2~z_2

  • x_m~y_m~z_m

The first line contains four integers: , , , . All values x_i, y_i, z_i (1 \le i \le m) are non-negative integers.

Each dataset contains m + n triples. The first triples, \mathbf{p}_1 through \mathbf{p}_m, are given explicitly (\mathbf{p}_i = (x_i, y_i, z_i)). The remaining triples are generated as follows, using parameters and :

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;
}

Call a total of times to generate, in order:

x_{m+1},~y_{m+1},~z_{m+1},~x_{m+2},~y_{m+2},~z_{m+2},~\dotsc,~x_{m+n},~y_{m+n},~z_{m+n}.

You may assume:

1 \le m+n \le 3 \times 10^5,\\ 1 \le A,~B \le 2^{16},\\ 0 \le x_k,~y_k,~z_k < 10^6\quad \text{for}\ 1 \le k \le m+n.

The input ends with a line containing four zeros.

The total sum of m+n over all datasets does not exceed 2 \times 10^6.

Output

For each dataset, output the length of the longest strictly increasing sequence of triples in the given set—that is, the largest such that \mathbf{p}_{i_1} \prec \mathbf{p}_{i_2} \prec \dotsb \prec \mathbf{p}_{i_k}.

Examples

Example 1

Loading
Loading