现在的位置: 首页 > 综合 > 正文

【HDU】4417 Super Mario 线段树

2017年11月20日 ⁄ 综合 ⁄ 共 3085字 ⁄ 字号 评论关闭

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2200    Accepted Submission(s): 1065

Problem Description

Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to
the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
 


Input

The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
 


Output

For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
 


Sample Input
1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
 


Sample Output
Case 1: 4 0 0 3 1 2 0 1 5 1
 


Source

2012 ACM/ICPC Asia Regional Hangzhou Online

传送门:【HDU】4417 Super Mario

题目大意:给你一串序列,每次要你查询区间【L,R】内小于等于H的个数。

题目分析:听说是划分树能做的。。。现在还不会,就笨笨的把线段树离线了做= =||。。。。总之就是离线了做。。。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

typedef long long BigNum ;

#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define rt o , l , r
#define root 1 , 1 , n
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define clear( A , X ) memset ( A , X , sizeof A )

const int maxN = 200005 ;
const int maxE = 1000000 ;

struct Node {
	int h , idx ;
	void Input ( int __idx ) {
		scanf ( "%d" , &h ) ;
		idx = __idx ;
	}
	bool operator < ( const Node &t ) const {
		return h < t.h ;
	}
} ;

struct Line {
	int l , r , h ;
	void Input () {
		scanf ( "%d%d%d" , &l , &r , &h ) ;
	}
} ;

struct Edge {
	int n ;
	int l , r , i ;
} ;

Node arr[maxN] ;
Edge edge[maxE] ;
Line line[maxN] ;
int adj[maxN] , cntE ;
int num[maxN << 2] ;
int a[maxN] ;
int ans[maxN] ;

void addedge ( int l , int r , int h , int i ) {
	edge[cntE].l = l ;
	edge[cntE].r = r ;
	edge[cntE].i = i ;
	edge[cntE].n = adj[h] ;
	adj[h] = cntE ++ ;
}

int Unique ( int a[] , int n ) {
	int cnt = 1 ;
	sort ( a + 1 , a + n + 1 ) ;
	for ( int i = 2 ; i <= n ; ++ i ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ;
	return cnt ;
}

int Search ( int x , int l , int r ) {
	while ( l < r ) {
		int m = mid ;
		if ( a[m] >= x ) r = m ;
		else l = m + 1 ;
	}
	return l ;
}

void init () {
	clear ( num ,  0 ) ;
	clear ( adj , -1 ) ;
	cntE = 0 ;
}

void PushUp ( int o ) {
	num[o] = num[ls] + num[rs] ;
}

void Update ( int x , int o , int l , int r ) {
	if ( l == r ) {
		num[o] = 1 ;
		return ;
	}
	int m = mid ;
	if ( x <= m ) Update ( x , lson ) ;
	else 	      Update ( x , rson ) ;
	PushUp ( o ) ;
}

int Query ( int L , int R , int o , int l , int r ) {
	if ( L <= l && r <= R ) return num[o] ;
	int m = mid , ans = 0 ;
	if ( L <= m ) ans += Query ( L , R , lson ) ;
	if ( m <  R ) ans += Query ( L , R , rson ) ;
	return ans ;
}

void Work () {
	int n , m , cnt = 0 ;
	scanf ( "%d%d" , &n , &m ) ;
	init () ;
	for ( int i = 1 ; i <= n ; ++ i ) {
		arr[i].Input ( i ) ;
		a[++ cnt] = arr[i].h ;
	}
	for ( int i = 1 ; i <= m ; ++ i ) {
		line[i].Input () ;
		a[++ cnt] = line[i].h ;
	}
	cnt = Unique ( a , cnt ) ;
	for ( int i = 1 ; i <= m ; ++ i ) {
		int H = Search ( line[i].h , 1 , cnt + 1 ) ;
		addedge ( line[i].l + 1 , line[i].r + 1 , H , i ) ;
	}
	int now = 1 ;
	sort ( arr + 1 , arr + n + 1 ) ;
	for ( int i = 1 ; i <= cnt ; ++ i ) {
		while ( now <= n && arr[now].h <= a[i] ) Update ( arr[now ++].idx , 1 , 1 , n ) ;
		for ( int j = adj[i] ; ~j ; j = edge[j].n ) {
			int idx = edge[j].i , L = edge[j].l , R = edge[j].r ;
			ans[idx] = Query ( L , R , 1 , 1 , n ) ;
		}
	}
	for ( int i = 1 ; i <= m ; ++ i ) printf ( "%d\n" , ans[i] ) ;
}

int main () {
	int T , cas ;
	for ( scanf ( "%d" , &T ) , cas = 1 ; cas <= T ; ++ cas ) {
		printf ( "Case %d:\n" , cas ) ;
		Work () ;
	}
	return 0 ;
}

抱歉!评论已关闭.