Description
Time Limit : 8 sec, Memory Limit : 262144 KB
Let us compare two triples
a=(xa,ya,za)
and
b=(xb,yb,zb)
by a partial order
∠
defined as follows.
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
,
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
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
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
Sample Output
3
5
1
3
Solution & Code
这道题没有下标这个维度,所以相当于
法一:
按照
x
排序后按顺序处理,用一个二维的数据结构维护查询与插入。
法二:
CDQ分治后就可以降维为二维问题,相当于带下标维度的一维问题(也就是最基本的最长上升子序列),不过因为
#include <cstdio>
#include <cstring>
#include <algorithm>
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;
}
const int maxn = 3e5 + 5;
const int maxm = 1e6 + 5;
struct node{int x, y, z;} arry[maxn];
int ans, n, m, A, B, f[maxn], rnk[maxn];
bool cmp1(node i, node j){
if(i.x != j.x) return i.x < j.x;
if(i.y != j.y) return i.y > j.y;
// 保证x严格小于
return i.z > j.z;
}
bool cmp2(int i, int j){
if(arry[i].y != arry[j].y) return arry[i].y < arry[j].y;
else return arry[i].z > arry[j].z;
}
int bitt[maxm], vist[maxm], nowt;
// 不用时间戳就会T掉
int lowbit(int i){return i & (-i);}
int query(int pos){
int rtn = 0;
for(int i = pos; i >= 1; i -= lowbit(i)){
if(vist[i] == nowt) rtn = max(rtn, bitt[i]);
}
return rtn;
}
void modify(int pos, int val){
for(int i = pos; i <= m; i += lowbit(i)){
if(vist[i] == nowt) bitt[i] = max(bitt[i], val);
else{
bitt[i] = val;
vist[i] = nowt;
}
}
}
void devide(int left, int rght){
if(left >= rght) return;
int mid = (left + rght) >> 1;
devide(left, mid);
for(int i = left; i <= rght; ++i) rnk[i] = i;
sort(rnk + left, rnk + mid + 1, cmp2);
sort(rnk + mid + 1, rnk + rght + 1, cmp2);
++nowt;
for(int i = left, j = mid + 1; j <= rght; ++j){
while(arry[rnk[i]].y < arry[rnk[j]].y && i <= mid){
// 保证y严格小于
modify(arry[rnk[i]].z, f[rnk[i]]);
++i;
}
f[rnk[j]] = max(f[rnk[j]], query(arry[rnk[j]].z - 1) + 1);
// 保证z严格小于
}
devide(mid + 1, rght); // 先合并左右再处理右边
}
int work(){
a = A, b = B;
ans = 0;
for(int i = 1; i <= m; ++i) scanf("%d%d%d", &arry[i].x, &arry[i].y, &arry[i].z);
for(int i = 1; i <= n; ++i){
arry[m+i].x = r();
arry[m+i].y = r();
arry[m+i].z = r();
}
n = m + n, m = 0;
for(int i = 1; i <= n; ++i) f[i] = 1; // 赋初值
for(int i = 1; i <= n; ++i) arry[i].z += 2; // 防止边界情况爆炸
for(int i = 1; i <= n; ++i) m = max(m, arry[i].z);
sort(arry + 1, arry + n + 1, cmp1);
devide(1, n);
for(int i = 1; i <= n; ++i) ans = max(ans, f[i]);
printf("%d\n", ans);
}
int main(){
while(true){
scanf("%d%d%d%d", &m, &n, &A, &B);
if(m + n + A + B == 0) break;
work();
}
}