hdu4417 Super Mario (主席树入门)

Super Mario

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


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
题目大意:有q个询问,求区间 l~r 中 <=key的数的个数
思路:主席树经典题目,数据比较大,离散化一下成(1~n)。
然后没什么好说的了。
代码如下:
#include <map>  
#include <set>  
#include <cmath>  
#include <queue>  
#include <vector>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
const int maxn=1e5+10;  
struct node  
{  
    int l,r,sum,lson,rson;  
}tr[2000010];  
int rt[maxn],A[maxn],B[maxn],cnt,pos;  
map<int,int>C;
void Buildtree(int l,int r,int& cur)  
{  
    cur=++cnt;  
    tr[cur].l=l,tr[cur].r=r,tr[cur].sum=0;  
    if(l==r)return;  
    int mid=(l+r)>>1;  
    Buildtree(l,mid,tr[cur].lson);  
    Buildtree(mid+1,r,tr[cur].rson);  
}  
void Insert(int pre,int& cur)  
{  
    cur=++cnt;  
    tr[cur]=tr[pre],tr[cur].sum=tr[cur].sum+1;  
    if(tr[cur].l==tr[cur].r)return;  
    int mid=(tr[cur].l+tr[cur].r)>>1;  
    if(pos<=mid)Insert(tr[pre].lson,tr[cur].lson);  
    else Insert(tr[pre].rson,tr[cur].rson);  
}  
int Query(int lqj,int rqj,int ql,int qr)  
{  
    int total=0;
	int l=tr[rqj].l,r=tr[lqj].r;
	if(ql<=l&&r<=qr)return tr[rqj].sum-tr[lqj].sum;  //在查询的区间内的话直接返回差值
	if(l==r)return 0;
    //int cmp=tr[tr[rqj].lson].sum-tr[tr[lqj].lson].sum;
	int mid=(l+r)>>1;
	if(ql<=mid)total+=Query(tr[lqj].lson,tr[rqj].lson,ql,qr);  
    if(qr>mid)total+=Query(tr[lqj].rson,tr[rqj].rson,ql,qr);
	return total;	
}  
int main()  
{  
    int n,q,ql,qr,ans,key,cas=1;  
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&q);  
		if(cas>1)C.clear();
		printf("Case %d:\n",cas++);
		cnt=0;  
		for(int i=1;i<=n;i++)scanf("%d",&A[i]),B[i]=A[i];  
		sort(B+1,B+1+n);
		int num=unique(B+1,B+1+n)-B-1;
		for(int i=1;i<=num;i++)C[B[i]]=i;//离散化
		//for(int i=1;i<=n;i++)printf("%d ",C[A[i]]);printf("\n");
		Buildtree(0,num,rt[0]);//建立空数	
		for(int i=1;i<=n;i++)pos=C[A[i]],Insert(rt[i-1],rt[i]);  
		while(q--)
		{
			scanf("%d%d%d",&ql,&qr,&key);
			int temp=key;
			key=lower_bound(B+1,B+1+num,key)-B;//找到key离散后的值
			//cout<<key<<endl;
			if(B[key]!=temp)key--;
			ans=Query(rt[ql],rt[qr+1],0,key);
			printf("%d\n",ans);
		}
	}
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值