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

博客围绕Super Mario问题展开,该问题是求区间[L, R]内小于等于h的数的个数。介绍了静态主席树相关知识,给出离线和在线两种解法思路。离线是提前存好查询点再查询;在线则根据h与数组元素关系确定查询范围。
摘要由CSDN通过智能技术生成

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
*/

 

内容概要:该题库专为研究生入学考试计算机组成原理科目设计,涵盖名校考研真题、经典教材课后习题、章节题库和模拟试题四大核心模块。名校考研真题精选多所知名高校的计算机组成原理科目及计算机联考真题,并提供详尽解析,帮助考生把握考研命题趋势与难度。经典教材课后习题包括白中英《计算机组成原理》(第5版)和唐朔飞《计算机组成原理》(第2版)的全部课后习题解答,这两部教材被众多名校列为考研指定参考书目。章节题库精选代表性考题,注重基础知识与重难点内容,帮助考生全面掌握考试大纲要求的知识点。模拟试题依据历年考研真题命题规律和热门考点,精心编制两套全真模拟试题,并附标准答案,帮助考生检验学习成果,评估应试能力。 适用人群:计划参加研究生入学考试并报考计算机组成原理科目的考生,尤其是需要系统复习和强化训练的学生。 使用场景及目标:①通过研读名校考研真题,考生可以准确把握考研命题趋势与难度,有效评估复习成效;②通过经典教材课后习题的练习,考生可以巩固基础知识,掌握解题技巧;③通过章节题库的系统练习,考生可以全面掌握考试大纲要求的各个知识点,为备考打下坚实基础;④通过模拟试题的测试,考生可以检验学习成果,评估应试能力,为正式考试做好充分准备。 其他说明:该题库不仅提供详细的题目解析,还涵盖了计算机组成原理的各个方面,包括计算机系统概述、数据表示与运算、存储器分层、指令系统、中央处理器、总线系统和输入输出系统等。考生在使用过程中应结合理论学习与实践操作,注重理解与应用,以提高应试能力和专业知识水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值