换个角度思考 题解

换个角度思考

https://ac.nowcoder.com/acm/problem/19427

看样子,应该有比较好的做法,不过,这道题,肯定要上主席树辣!!

如果打算用主席树做这道题的话,这道题就是一个主席树的纯板子问题了。。。

相当于查询区间l-r中1-x的数字的个数

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
struct node{
    int lson,rson,w;
}t[N<<5];
int rt[N],cnt;
inline void insert(int &now,int pas,int l,int r,int x){
    now=++cnt;
    t[now]=t[pas];
    ++t[now].w;
    if(l==r){
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid){
        insert(t[now].lson,t[pas].lson,l,mid,x);
    }else{
        insert(t[now].rson,t[pas].rson,mid+1,r,x);
    }
}
inline int find(int now,int pas,int l,int r,int lc,int rc){
    if(lc<=l&&r<=rc){
        return t[now].w-t[pas].w;
    }
    int mid=(l+r)>>1,res=0;
    if(lc<=mid){
        res+=find(t[now].lson,t[pas].lson,l,mid,lc,rc);
    }
    if(rc>mid){
        res+=find(t[now].rson,t[pas].rson,mid+1,r,lc,rc);
    }
    return res;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i){
        int x;
        scanf("%d",&x);
        insert(rt[i],rt[i-1],1,1e5,x);
    }
    while(m--){
        int l,r,x;
        scanf("%d%d%d",&l,&r,&x);
        printf("%d\n",find(rt[r],rt[l-1],1,1e5,1,x));
    }
    return 0;
}
全部评论

相关推荐

大疆在线测评都考什么呀,会考企业概况啥的吗
又被画饼了的做题家很...:不会。刚做完,就是材料分析、态度题、算术题、逻辑题。总共60道。
投递大疆等公司7个岗位
点赞 评论 收藏
分享
Lorn的意义:你这标个前端是想找全栈吗?而且项目确实没什么含金量,技术栈太少了,边沉淀边找吧 现在学院本想就业好一点四年至少得高三模式两年加油吧
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务