uvaLive 6667 Longest Chain ( splay )

题意 : 给你 n 个三元组 ( x , y , z ) , triple A < triple B 的定义的 :Xa < Xb && Ya < Yb && Za < Zb ,问最长的triple链满足 triple1 < triple2 < ... < triple3 的最大长度是多少 ?

思路 : 如果 n 比较小的话 , 我们很容易就能想到用 O( n ^ 2 ) 的dp 或者 最长路之类的算法 , 但是 n 的数据量是 3*10^5 ,这个复杂度就必然要跪了 。

那么我第一个想到的是 最长上升子序列的 nlogn 的算法 , 不过问题是这题的元素是三元组 , 不是一个整数,我们不用记录长度为L的最小元素,而是应该要记录多个三元组 。

首先我们可以通过对x排序来降低一维 ( 只有当 triple[i-1].x < triple[i].x 的时候 , 我们才把前面 x 等于 triple[i-1].x 的三元组的信息处理掉 , 这样保证每次操作的时候,已有的信息x都是小于当前triple , 这样就不用考虑这一维了 ) 。

然后我们要二分长度 , 判断每个长度所记录的三元组中有没有小于当前元组的 ,假设当前三元组是cur,那么我们就要查询这些三元组所有 y < cur.y 里面 z 的最小值 ,线段树可以搞,但是10^5 棵线段树如果提前开好果断就爆内存了 , 所以要用支持插入,区间查找的平衡树来维护 , 我用的是splay

ps .. 据说这题正解是 cdq分治 ... 看来我是乱搞过的 ... 再去补补


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define MAXN 1000005
#define INF 0x3f3f3f3f
#define keyNode ( ch[ch[root][1]][0] ) 
#define ls(x) ( ch[x][0] ) 
#define rs(x) ( ch[x][1] ) 

int a , A , b , B , C , M , n , m ;
struct triple{
	int x , y , z ;
	friend bool operator < ( triple a , triple b ) {
		if( a.x - b.x ) 
			return a.x < b.x ;
		if( a.y - b.y )
			return a.y < b.y ;
		return a.z < b.z ;
	}
}tr[MAXN] ;

int tot ;
int ch[MAXN][2] ;
int pre[MAXN] ;
int sz[MAXN] ;
int Min[MAXN] ;
int val[MAXN] ;
int key[MAXN] ;

struct Splay{

	int root , Minid , Maxid ;

	void Rotate( int x , int op ) {
		int y = pre[x] ;
		if( pre[y] ) {
			ch[pre[y]][ch[pre[y]][1] == y ] = x ;
		}
		pre[x] = pre[y] ;
		pre[y] = x ;
		ch[y][op] = ch[x][op^1] ;
		if( pre[ch[x][op^1]] )
			pre[ch[x][op^1]] = y ;
		ch[x][op^1] = y ;
		push_up(y) ; 
	}

	void splay( int x , int goal ) {
		while( pre[x] != goal ) {
			int y = pre[x] ;
			if( pre[y] == goal ) {
				Rotate( x , ch[y][1] == x ) ;
			}else{
				int y = pre[x] ;
				int op = rs(y) == x ;
				if( ch[pre[y]][op] == y ) {
					Rotate( y , op ) ;
					Rotate( x , op ) ;
				}else{
					Rotate( x , op ) ;
					Rotate( x , op ^ 1 ) ;
				}
			}
		}
		push_up( x ) ;
		if( goal == 0 ) root = x ;
	}

	void RotateTo( int k , int goal ) {
		int x = root ;
		while( sz[ls(x)] != k ) {
			if( k < sz[ls(x)] ) {
				x = ls(x) ;
			}else{
				k -= sz[ls(x)] + 1 ;
				x = rs(x) ;
			}
		}
		splay( x , goal ) ;
	}

	void makenode( int &x , int fa , int v , int k ) {
		x = tot ++ ;
		pre[x] = fa ;
		ls(x) = rs(x) = 0 ;
		sz[x] = 1 ;
		Min[x] = key[x] = k ;
		val[x] = v ;
	}

	void push_up( int x ) {
		sz[x] = sz[ls(x)] + sz[rs(x)] + 1 ;
		Min[x] = min( key[x] , min( Min[ls(x)] , Min[rs(x)] ) ) ;
	}

	void init(){
		makenode( root , 0 , -INF , INF ) ;
		makenode( rs(root) , root , INF , INF ) ;
		Minid = root ;
		Maxid = rs(root) ;
		push_up( root ) ;
	}

	void insert( triple a ) {
		int v = a.y , k = a.z ;
		int rc = root ;
		while( ch[rc][val[rc]<v] ) {
			if( val[rc] == v ) {
				key[rc] = min( key[rc] , k ) ;
				splay( rc , 0 ) ;
			}
			rc = ch[rc][val[rc]<v] ;
		}
		makenode( ch[rc][val[rc]<v] , rc , v , k ) ;
		splay( ch[rc][val[rc] < v] , 0 ) ;
	}

	int upper_bound( int key ) {
		int x = root ;    
                int ans = -1 ;  
                while( x ) {  
                       if( val[x] >= key ) {  
                                ans = x ;  
                                x = ls(x) ;  
                       }else{  
                               x = rs(x) ;  
                       } 
                }  
                return ans ; 
	}

	bool search( triple a ) {
		int k = upper_bound( a.y ) ;
		splay( Minid , 0 ) ;
		splay( k , root ) ;
		return Min[keyNode] < a.z ;
	}

}f[MAXN];
bool vis[MAXN] ;
int ins[MAXN] ;

int r() {
	a = 36969 * (a & M) + (a >> 16);
	b = 18000 * (b & M) + (b >> 16);
	return (C & ((a << 16) + b)) % 1000000;
}

int main(){
	
	//freopen( "out.txt" , "rt" , stdin ) ;
	//freopen( "out2.txt" , "wt" , stdout ) ;

	while( scanf( "%d%d%d%d" , &m , &n , &A , &B ) != EOF ) {
		if( n == 0 && m == 0 && A == 0 && B == 0 ) break;
		a = A, b = B, C = ~(1<<31), M = (1<<16)-1;
		for( int i = 1 ; i <= m ; i ++ ) {
			scanf( "%d%d%d" , &tr[i].x , &tr[i].y , &tr[i].z ) ;
		}
		for( int i = m + 1 ; i <= n + m ; i ++ ) {
			tr[i].x = r() ;
			tr[i].y = r() ;
			tr[i].z = r() ;
		}
		n = n + m ;
		sort( tr + 1 , tr + 1 + n ) ;
		tot = 1 ;
		sz[0] = ls(0) = rs(0) = 0 ;
		val[0] = 0 ; key[0] = Min[0] = INF ; pre[0] = 0 ;
		memset( ins , 0 , sizeof(ins) ) ;
		memset( vis , false , sizeof(vis) ) ;
		int R = 0 ;
		for( int i = 1 ; i <= n ; i ++ ) {
			if( i > 1 && tr[i].x != tr[i-1].x ) {
				for( int j = i - 1 ; j >= 1 ; j -- ) {
					if( tr[j].x != tr[i-1].x ) break;
					f[ins[j]].insert( tr[j] ) ;
					if( ins[j] > R ) R = ins[j] ;
				}
			}
			int ll = 1 , rr = R , mm ;
			if( R == 0 || f[R].search( tr[i] ) ) {
				if( !vis[R+1] ) {
					f[R+1].init() ;
					vis[R+1] = true ;
				}
				ins[i] = R + 1 ;
			} else if( f[1].search( tr[i] ) == false ) {
				ins[i] = 1 ;
			}
			else {
				while( ll < rr ) {
					mm = ( ll + rr + 1 ) >> 1 ;
					if( f[mm].search( tr[i] ) ) {
						ll = mm ;
					}else{
						rr = mm - 1 ;
					}
				}
				ins[i] = ll + 1 ;
			}
		}
		int Max = R ;
		for( int i = 1 ; i <= n ; i ++ ) 
			Max = max( Max , ins[i] );
		printf( "%d\n" , Max ) ;
	}
	return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值