Super Mario - hdu4417 - 主席树(离线/在线)

Super Mario

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

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

题意:

区间[L,R]内小于等于h的数的个数

思路:

静态主席树相关的知识:戳这里

不同之处就是改变一下query部分的代码,注意题中区间是从0开始的,所以左右区间++

int query(int lr,int rr,int l,int r,int k){//求有多少个小于等于k的数
    //rr表示第R棵树根节点编号
    //lr表示第L-1棵树根节点编号
    if(l==r){//递归到叶子节点时,直接加上叶子节点的sum值只差
        return sum[rr]-sum[lr];
    }
    int m=(l+r)>>1;
    if(k<=m){//如果点小于等于中间节点,接着向下查询
        return query(ls[lr],ls[rr],l,m,k);
    }
    else {//否则,加上左子树的sum值只差,然后接着向下查询
        return sum[ls[rr]]-sum[ls[lr]]+query(rs[lr],rs[rr],m+1,r,k);
    }
}

离线:

把查询的点也都提前存好,然后直接差小于等于h的数有几个

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long

typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int N=100005;
int a[N*2],b[N],tot=0;
int rt[N],ls[N*20],rs[N*20],sum[N*20];
struct A{
    int l,r,k;
}op[N];
void build(int &o,int l,int r){
    o=++tot;
    sum[o]=0;
    if(l==r)return ;
    int m=(l+r)>>1;
    build(ls[o],l,m);
    build(rs[o],m+1,r);
}

void update(int &o,int pre,int l,int r,int p){
    o=++tot;
    sum[o]=sum[pre]+1;
    ls[o]=ls[pre];
    rs[o]=rs[pre];
    if(l==r)return ;
    int m=(l+r)>>1;
    if(p<=m)update(ls[o],ls[pre],l,m,p);
    else update(rs[o],rs[pre],m+1,r,p);
}

int query(int lr,int rr,int l,int r,int k){//求有多少个小于等于k的数
    if(l==r){
        return sum[rr]-sum[lr];
    }
    int m=(l+r)>>1;
    if(k<=m){//如果点小于等于中间节点
        return query(ls[lr],ls[rr],l,m,k);
    }
    else {
        return sum[ls[rr]]-sum[ls[lr]]+query(rs[lr],rs[rr],m+1,r,k);
    }
}

int main(){
    int t;
    scanf("%d",&t);
    for(int q=1;q<=t;q++){
        printf("Case %d:\n",q);
        int n,m;
        tot=0;
        int k=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
            a[++k]=b[i];
        }

        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&op[i].l,&op[i].r,&op[i].k);
            op[i].l++;
            op[i].r++;
            a[++k]=op[i].k;
        }
        sort(a+1,a+1+k);
        int nn=unique(a+1,a+1+k)-(a+1);
        build(rt[0],1,nn);
        for(int i=1;i<=n;i++){
            int x=lower_bound(a+1,a+1+nn,b[i])-a;
            update(rt[i],rt[i-1],1,nn,x);
        }
        for(int i=1;i<=m;i++){
            int x=lower_bound(a+1,a+1+nn,op[i].k)-a;
            printf("%d\n",query(rt[op[i].l-1],rt[op[i].r],1,nn,x));
        }
    }
}
/*
1
10 10
0 5 2 7 5 4 3 8 7 7
1 9 4
2 8 6
3 5 0
1 3 1
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
*/

在线:

查大于等于h的点时,h值可能没在数组中,用lower_bound查到大于等于h的值的元素的下标x

(1)h==a[x];那么直接查询小于等于x的结果

(2)h<a[x];查询小于等于x-1的结果,注意这里要特判一下,当x==1时,输出0(没有一个元素小于h)

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long

typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int N=100005;
int a[N*2],b[N],tot=0;
int rt[N],ls[N*20],rs[N*20],sum[N*20];
struct A{
    int l,r,k;
}op[N];
void build(int &o,int l,int r){
    o=++tot;
    sum[o]=0;
    if(l==r)return ;
    int m=(l+r)>>1;
    build(ls[o],l,m);
    build(rs[o],m+1,r);
}

void update(int &o,int pre,int l,int r,int p){
    o=++tot;
    sum[o]=sum[pre]+1;
    ls[o]=ls[pre];
    rs[o]=rs[pre];
    if(l==r)return ;
    int m=(l+r)>>1;
    if(p<=m)update(ls[o],ls[pre],l,m,p);
    else update(rs[o],rs[pre],m+1,r,p);
}

int query(int lr,int rr,int l,int r,int k){//求有多少个小于等于k的数
    if(l==r){
        return sum[rr]-sum[lr];
    }
    int m=(l+r)>>1;
    if(k<=m){//如果点小于等于中间节点
        return query(ls[lr],ls[rr],l,m,k);
    }
    else {
        return sum[ls[rr]]-sum[ls[lr]]+query(rs[lr],rs[rr],m+1,r,k);
    }
}

int main(){
    int t;
    scanf("%d",&t);
    for(int q=1;q<=t;q++){
        printf("Case %d:\n",q);
        int n,m;
        tot=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
            a[i]=b[i];
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&op[i].l,&op[i].r,&op[i].k);
            op[i].l++;
            op[i].r++;
        }
        sort(a+1,a+1+n);
        int nn=unique(a+1,a+1+n)-(a+1);
        build(rt[0],1,nn);
        for(int i=1;i<=n;i++){
            int x=lower_bound(a+1,a+1+nn,b[i])-a;
            update(rt[i],rt[i-1],1,nn,x);
        }
        for(int i=1;i<=m;i++){
            int x=lower_bound(a+1,a+1+nn,op[i].k)-a;
            if(op[i].k<a[x]){
                if(x==1)printf("0\n");
                else printf("%d\n",query(rt[op[i].l-1],rt[op[i].r],1,nn,x-1));
            }
            else{
                printf("%d\n",query(rt[op[i].l-1],rt[op[i].r],1,nn,x));
            }
        }
    }
}
/*
1
10 10
0 5 2 7 5 4 3 8 7 7
1 9 4
2 8 6
3 5 0
1 3 1
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值