Longest Chain

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.
abxa<xb and ya<yb and za<zb
Your mission is to find, in the given set of triples, the longest ascending series a1a2...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 ith 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 1m+n3×105 , 1A,B216 , and 0xk,yk,zk<106 for 1km+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 pi1pi2...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,y,z 三个维度。
法一:
按照 x 排序后按顺序处理,用一个二维的数据结构维护查询与插入。
法二:
CDQ分治后就可以降维为二维问题,相当于带下标维度的一维问题(也就是最基本的最长上升子序列),不过因为 x,y,z 都必须严格小于,所以需要处理一些细节,详见代码。

#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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值