Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

所以终于写对指针主席树了,网上怎么那么多人不喜欢指针。。。伐开心

Posted by jerrywans119 at 2018-04-08 17:18:10 on Problem 2104
#include <cstdio>
#include <algorithm>

const  int  N = 100000 + 5 ;

int  a [ N ] , dot [ N ] ;
int  n , m , x , y , k ; 

struct  Node {
	int  cnt ; 
	Node  * ls , * rs ;
	void  update (  ) {
		cnt = ls -> cnt + rs -> cnt ;
	}
}
pool [ N << 5 ] , * tail = pool , * root [ N ] ;

Node  * build ( int  l , int  r ) {
	Node  * nd = ++ tail ;
	if ( l == r ) {
		nd -> cnt = 0 ;
		return nd ;
	}
	else {
		int  mid = l + r >> 1 ;
		nd -> ls = build ( l , mid ) ;
		nd -> rs = build ( mid + 1 , r ) ;
		nd -> update (  ) ;
	}
	return  nd ;
}

Node  * insert ( Node  * nd , int  l , int  r , int  num ) {
	Node  * nnd = ++ tail ;
	if ( l == r ) {
		nnd -> cnt = nd -> cnt + 1 ; 
		return  nnd ;
	}
	else {
		int  mid = l + r >> 1 ;
		if ( num <= mid ) {
			nnd -> ls = insert ( nd -> ls , l , mid , num ) ;
			nnd -> rs = nd -> rs ;
		}
		else {
			nnd -> ls = nd -> ls ;
			nnd -> rs = insert ( nd -> rs , mid + 1 , r , num ) ;
		}
		nnd -> update (  ) ;
	}
	return  nnd ;
}

int  query ( Node  *lnd , Node  * rnd , int  l , int  r , int  k ) {
	if ( l == r ) 
		return  l ; 
	int  mid = l + r >> 1 ;
	int  lz =  rnd -> ls -> cnt - lnd -> ls -> cnt ;
	if ( k <= lz )  return  query ( lnd -> ls , rnd -> ls , l , mid , k ) ;
	else  return  query ( lnd -> rs , rnd -> rs , mid + 1 , r , k - lz ) ;
}
int  main (  ) {
	
	freopen ( "test.in" , "r" , stdin ) ;
	
	scanf ( "%d%d" , & n , & m ) ;
	
	for ( int  i = 1 ; i <= n ; i ++ ) {
		scanf ( "%d" , & a [ i ] ) ;
		dot [ i ] = a [ i ] ;
	}
	
	std :: sort ( dot + 1 , dot + n + 1 ) ;
	int  tot = std :: unique ( dot + 1 , dot + n + 1 ) - dot - 1 ;
	
	root [ 0 ] = build ( 1 , tot ) ;
	for ( int  i = 1 ; i <= n ; i ++ ) {
		int  dd = std :: lower_bound ( dot + 1 , dot + tot + 1 , a [ i ] ) - dot ; 
		root [ i ] = insert ( root [ i - 1 ] , 1 , tot , dd ) ;
	}
	
	for ( int  i = 1 ; i <= m ; i ++ ) {
		scanf ( "%d%d%d" , & x , & y , & k ) ;
		printf ( "%d\n" , dot [ query ( root [ x - 1 ] , root [ y ] , 1 , tot , k ) ] ) ;
	}
	
	return  0 ; 
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator