HDU4417.Super Mario(主席树区间查询的模板)

Super Mario
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

Recommend
liuyiding | We have carefully selected several similar problems for you: 6742 6741 6740 6739 6738

注意一下跳跃的高度比任何一个数都小的情况;

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int n,m,w[maxn],cnt,an[maxn],a[maxn];
struct node{
    int l,r;
    int sum;
}s[maxn*20];
void push_up(int p){
    s[p].sum=s[s[p].l].sum+s[s[p].r].sum;
}
int build_tree(int l,int r){
    int p=cnt++;
    if(l==r)  return p;
    s[p].sum=0;
    int mid=(l+r)>>1;
    s[p].l=build_tree(l,mid);
    s[p].r=build_tree(mid+1,r);
    push_up(p);
    return p;
}
int update(int old,int l,int r,int pos){
    int p=cnt++;s[p]=s[old];
    if(l==r&&pos==l){
        s[p].sum+=1;
        return p;
    }
    int mid=(l+r)>>1;
    if(pos<=mid)  s[p].l=update(s[old].l,l,mid,pos);
    else  s[p].r=update(s[old].r,mid+1,r,pos);
    push_up(p);
    return p;
}
int query(int l,int r,int old,int now,int nl,int nr){
    if(nl==l&&nr==r){
        return s[now].sum-s[old].sum;
    }
    int mid=(l+r)>>1;
    if(nr<=mid) return query(l,mid,s[old].l,s[now].l,nl,nr);
    else return query(l,mid,s[old].l,s[now].l,nl,mid)+query(mid+1,r,s[old].r,s[now].r,mid+1,nr);
}
int main()
{
    int t;cin>>t;int cas=0;
    while(t--){
        scanf("%d%d",&n,&m);cnt=1;
        for(int i=1;i<=n;i++){
            scanf("%d",&w[i]);
            an[i]=w[i];
        }
        sort(w+1,w+1+n);
        int len=unique(w+1,w+1+n)-w;
        a[0]=build_tree(1,len);
        int res=0;
        for(int i=1;i<=n;i++){
            int l=lower_bound(w+1,w+len,an[i])-w;
            a[++res]=update(a[res-1],1,len,l);
        }
        printf("Case %d:\n",++cas);
        for(int i=1;i<=m;i++){
            int l,r,val;scanf("%d%d%d",&l,&r,&val);
            if(val<w[1]){
                printf("0\n");
                continue;
            }
            int t=upper_bound(w+1,w+len,val)-w-1;
            int ans=query(1,len,a[l],a[r+1],1,t);
            printf("%d\n",ans);
        }
    }
    return 0;
}
/*
1
10 10
1 5 2 7 5 4 3 8 7 7
 */
发布了35 篇原创文章 · 获赞 25 · 访问量 807

猜你喜欢

转载自blog.csdn.net/qq_43816599/article/details/103995849