HDU4417 Super Mario (主席树模板)

Super Mario

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


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
 
  
110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
 

Sample Output
 
  
Case 1:4003120151



提示:向左搜就搜到底,向右搜就将左树加上。

/*
题意:求区间中小于等于H的数

思路:主席树模板,修改query即可
 
*/

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int cnt,ans;
int a[maxn],root[maxn];
vector<int> v;

struct node{
    int l,r,sum;
}T[maxn*25];

int getid(int x){
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
} 

int getansid(int x){
    return upper_bound(v.begin(),v.end(),x)-v.begin();
} 

void update(int l,int r,int &x,int y,int pos){
    T[++cnt]=T[y];
    T[cnt].sum++;
    x=cnt;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(mid >= pos) update(l,mid,T[x].l,T[y].l,pos);
    else update(mid+1,r,T[x].r,T[y].r,pos);
}

int query(int l,int r,int x,int y,int val){
    if(l==r) return T[y].sum-T[x].sum;            //查询成功 
    int mid = (l+r)>>1;    
    int res=0;    
    if(mid >= val ){
        res=query(l,mid,T[x].l,T[y].l,val);
    }else{
        res+=T[T[y].l].sum-T[T[x].l].sum;
        res+=query(mid+1,r,T[x].r,T[y].r,val);
    }
    return res;
}


int main(){
    int t,n,m,l,r,p;
    scanf("%d",&t);
    for(int k=1;k<=t;k++){
        cnt=0;
        v.clear();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            v.push_back(a[i]);    
        }
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        for(int i=1;i<=n;i++) update(1,n,root[i],root[i-1],getid(a[i]));
        printf("Case %d:\n",k);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&l,&r,&p);
            if(getansid(p)==0) printf("0\n");
            else printf("%d\n",query(1,n,root[l],root[r+1],getansid(p)));
        }
    }
} 

猜你喜欢

转载自blog.csdn.net/rvelamen/article/details/80741173