Super Mario HDU - 4417 (可持续化值域线段树)(可持续化线段树+值域线段树)

Super Mario题目链接
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.
题意:n个数字,m次询问。每次询问让你求区间里小于X的值。
解题思路:求[l,r]里的小于X的值,相当于求[1,r]里小于x的值,减去[1,l-1]里小于x的值。每次输入数字时,都用可持续化线段树。线段树用值域线段树就可以了。记得离散化。
错误原因:离散化数组开小了。读题不认真,这道题是 0 <= L <= R < n .

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define LL long long
#define MT(a,b) memset(a,b,sizeof(a))
const int mod=10007;
const int maxn=1e5+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
struct node{
    int val;
    int ls,rs;
}tree[maxn*40];
struct node2{
    int l,r;
    LL h;
}q[maxn];
int root_num,rt[maxn<<5];
LL num[maxn<<2];
LL lsh[maxn<<2];
//map<LL,int>lsh;

void push_up(int root){
    tree[root].val=tree[tree[root].ls].val+tree[tree[root].rs].val;
}

int update(int l,int r,int pos,int root){
    int t=++root_num;
    tree[t]=tree[root];
    if (l==pos&&r==pos){
        tree[t].val++;
        return t;
    }
    int mid=(l+r)>>1;
    if (pos<=mid) tree[t].ls=update(l,mid,pos,tree[root].ls);
    else if (pos>mid) tree[t].rs=update(mid+1,r,pos,tree[root].rs);
    push_up(t);
    return t;
}

int query(int l,int r,int ql,int qr,int root){
    if (l==ql&&r==qr){
        return tree[root].val;
    }
    int mid=(l+r)>>1;
    if (qr<=mid) return query(l,mid,ql,qr,tree[root].ls);
    else if (ql>mid) return query(mid+1,r,ql,qr,tree[root].rs);
    else return query(l,mid,ql,mid,tree[root].ls)+query(mid+1,r,mid+1,qr,tree[root].rs);
}

int main (){
    int t;
    scanf("%d",&t);
    for (int T=1;T<=t;T++){
        int n,m;
        root_num=0;
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++){
            scanf("%lld",&num[i]);
            lsh[i]=num[i];
        }
        for (int i=1;i<=m;i++){
            scanf("%d%d%lld",&q[i].l,&q[i].r,&q[i].h);
            q[i].l+=1;q[i].r+=1;
            lsh[i+n]=q[i].h;
        }
        sort(lsh+1,lsh+1+n+m);
        int len=unique(lsh+1,lsh+1+n+m)-lsh;
        for (int i=1;i<=n;i++){
            int pos=lower_bound(lsh+1,lsh+len,num[i])-lsh;
            rt[i]=update(1,len-1,pos,rt[i-1]);
        }
        printf("Case %d:\n",T);
        for (int i=1;i<=m;i++){
            int h=lower_bound(lsh+1,lsh+len,q[i].h)-lsh;
            printf("%d\n",query(1,len-1,1,h,rt[q[i].r])-query(1,len-1,1,h,rt[q[i].l-1]));
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值