hdu 4417 Super Mario

Super Mario

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


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

Recommend
liuyiding
//线段树离线处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
struct node{
    int l,r,s,k;
}T[N*4],b[N];
int a[N],r[N],ans[N],res;
void build(int l,int r,int k){
    T[k].l=l;T[k].r=r;
    T[k].s=T[k].k=0;
    if(l==r)return;
    int mid=(l+r)/2;
    build(l,mid,2*k);
    build(mid+1,r,2*k+1);
}
void insert(int x,int k){
    if(T[k].l==T[k].r){
		if(T[k].l==x){T[k].s=1;}
    return;												
    }
    int mid=(T[k].l+T[k].r)/2;
    if(mid>=x)insert(x,2*k);
    else insert(x,2*k+1);
    T[k].s=T[2*k].s+T[2*k+1].s;
}
void query(int l,int r,int k){
    if(T[k].l==l&&T[k].r==r){
        res+=T[k].s;
        return;
    }
    if(T[k].l==T[k].r)return;
    int mid=(T[k].l+T[k].r)/2;
    if(mid>=r)query(l,r,2*k);
    else if(mid<l)query(l,r,2*k+1);
    else{
        query(l,mid,2*k);
        query(mid+1,r,2*k+1);
    }
}
int cmp1(int i,int j){
    return a[i]<a[j];
}
int cmp2(struct node& x,struct node& y){
    return x.k<y.k;
}
int main(){
    int T,t=1;
    int n,m,i,j,k;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        build(0,n-1,1);
        for(i=0;i<n;i++)scanf("%d",&a[r[i]=i]);
        for(i=0;i<m;i++){
            scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].k);
            b[i].s=i;
        }
    sort(r,r+n,cmp1);
    sort(b,b+m,cmp2);
    //for(i=0;i<n;i++)cout<<a[r[i]]<<" ";cout<<endl;
    //for(i=0;i<m;i++)cout<<b[i].k<<" ";cout<<endl;
    for(i=0,j=0;i<m;i++){
        for(;j<n;j++)
			if(a[r[j]]<=b[i].k){insert(r[j],1);}
            else break;
        res=0;
        query(b[i].l,b[i].r,1);
        ans[b[i].s]=res;
    }
    printf("Case %d:\n",t++);
    for(i=0;i<m;i++)printf("%d\n",ans[i]);
    }
return 0;
}

 
//函数式线段树在线处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
#define mid(i,j)  ((i+j)>>1)
struct node{
	int l,r,cnt;
	void init(int ll,int rr,int s){
		l=ll;r=rr;cnt=s;
	}
}T[N*20];
int a[N],b[N],r[N],n,m,cnt;
void update(int p,int x,int l,int r,int &k){
	k=cnt++;T[k].init(T[p].l,T[p].r,T[p].cnt+1);
	if(l==r)return;
	int mid=mid(l,r);
	if(mid>=x)update(T[p].l,x,l,mid,T[k].l);//住左
	else update(T[p].r,x,mid+1,r,T[k].r);//往右
}
int query(int i,int j,int l,int r,int k){//[i,j]中第k大
	if(l==r)return r;
	int mid=mid(l,r);
	int t=T[T[j].l].cnt-T[T[i].l].cnt;
	if(t>=k)return query(T[i].l,T[j].l,l,mid,k);
	else return query(T[i].r,T[j].r,mid+1,r,k-t);
}
int main(){
	int i,j,k,tt,t=1; 
	r[0]=cnt=0;
	scanf("%d",&tt);
	while(tt--){
		scanf("%d%d",&n,&m);

                cnt=1;
		for(i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}
		sort(b+1,b+n+1);//从小到大排
		int num=unique(b+1,b+n+1)-b-1;//除相邻重复元素后的元素个数
		for(i=1;i<=n;i++){
			a[i]=lower_bound(b+1,b+num+1,a[i])-b;//返回第一个大于a[i]的位置
			update(r[i-1],a[i],1,num,r[i]);
		}
		printf("Case %d:\n",t++);

		while(m--){
			scanf("%d%d%d",&i,&j,&k);
			int p=0,q=j-i+2,s,ans=0;
			while(p+1<q){
				s=(p+q)/2;
				int tmp=b[query(r[i],r[j+1],1,num,s)];
				//cout<<tmp<<"***"<<s<<endl;
				if(tmp<=k){
					p=ans=s;
				}else{//大于
					q=s;
				}
			}
			printf("%d\n",ans);
		}
	}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值