HDU-4417 Super Mario (主席树)

Super Mario

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


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


#include <bits/stdc++.h>
using namespace std;    
const int maxn = 100010;
int a[maxn], b[maxn], tot, rt[maxn];
struct tree{
	int sz, lson, rson;
}c[maxn << 5];
int build(int l, int r){
	int id = ++tot;
	c[id].sz = 0;
	if(l == r) return id;
	int mid = l + r >> 1;
	c[id].lson = build(l, mid);
	c[id].rson = build(mid + 1, r);
	return id;
}
int insert(int rt, int pos, int l, int r){
	int id = ++tot;
	c[id].sz = c[rt].sz;
	c[id].lson = c[rt].lson;
	c[id].rson = c[rt].rson;
	if(l == r){
		c[id].sz++; return id;
	}
	int mid = l + r >> 1;
	if(pos <= mid) c[id].lson = insert(c[id].lson, pos, l, mid);
	else c[id].rson = insert(c[id].rson, pos, mid + 1, r);
	c[id].sz = c[c[id].lson].sz + c[c[id].rson].sz;
	return id;
}
int query(int pre, int rt, int l, int r, int L, int R){
	if(L > R) return 0;
	if(l >= L && r <= R){
		return c[rt].sz - c[pre].sz;
	}
	int mid = l + r >> 1, ans = 0;
	if(mid >= L) ans += query(c[pre].lson, c[rt].lson, l, mid, L, R);
	if(mid < R) ans += query(c[pre].rson, c[rt].rson, mid + 1, r, L, R);
	return ans;
}
int main(){
	int T, casenum = 1;
	scanf("%d", &T);
	while(T--){
		printf("Case %d:\n", casenum++);
		int n, m, num, pos, l, r, h;
		scanf("%d %d", &n, &m);
		for(int i = 1; i <= n; ++i){
			scanf("%d", &a[i]);
			b[i] = a[i];
		}
		sort(b + 1, b + n + 1);
		num = unique(b + 1, b + 1 + n) - b - 1;
		tot = 0;
		rt[0] = build(1, num);
		for(int i = 1; i <= n; ++i){
			pos = lower_bound(b + 1, b + 1 + num, a[i]) - b;
			rt[i] = insert(rt[i - 1], pos, 1, num);
		}
		for(int i = 1; i <= m; ++i){
			scanf("%d %d %d", &l, &r, &h);
			pos = upper_bound(b + 1, b + num + 1, h) - b - 1; 
			printf("%d\n", query(rt[l], rt[r + 1], 1, num, 1, pos));
		}
	}
}    
  
/*
题意:
1e5个数,1e5次询问,每次询问区间有多少个数小于等于H。

思路:
主席树 板题。注意一下H的边界情况和离散时的正确性。
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值