HDU4417-Super Mario

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2183    Accepted Submission(s): 1061

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

题目意思就是一条线段上有n个点,每个点有个高度值,然后有m组查询,问你a到b区间里有多少个数是小于等于h的
这道题的解法为用二分+划分树
二分的是第K大数,与高度值进行比较

好久没1A了。。。。QAQ
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define MD(x,y) (((x)+(y))>>1)
const int maxn = 100000+10;
int lftnum[20][maxn];
int num[maxn];
int seg[20][maxn];
int n,m;
void build(int L,int R,int dep){
    if(L==R) return;
    int mid = MD(L,R),key = num[mid],lcnt = mid-L+1;
    for(int i = L; i <= R; i++){
        if(seg[dep][i] < key)
            lcnt--;
    }
    int lp = L,rp = mid+1;
    for(int i = L; i <= R; i++){
        if(L==i){
            lftnum[dep][i] = 0;
        }else{
            lftnum[dep][i] = lftnum[dep][i-1];
        }
        if(seg[dep][i] < key){
            lftnum[dep][i]++;
            seg[dep+1][lp++] = seg[dep][i];
        }
        else if(seg[dep][i] > key){
            seg[dep+1][rp++] =seg[dep][i];
        }
        else{
            if(lcnt>0){
                lcnt--;
                lftnum[dep][i]++;
                seg[dep+1][lp++] = seg[dep][i];
            }else{
                seg[dep+1][rp++] = seg[dep][i];
            }
        }
    }
    build(L,mid,dep+1);
    build(mid+1,R,dep+1);
}
int query(int L,int R,int ll,int rr,int dep,int k){
    if(L==R)
        return seg[dep][L];
    int a,aa,b,bb;
    int mid = MD(ll,rr);
    if(L==ll){
        a  = 0;
        aa = lftnum[dep][R] - a;
    }else{
        a = lftnum[dep][L-1];
        aa = lftnum[dep][R] - a;
    }
    if(aa >= k){
        L = ll+a;
        R = ll+a+aa-1;
        return query(L,R,ll,mid,dep+1,k);
    }else{
        b = L-ll-a;
        bb = R-L+1-aa;
        L = mid+1+b;
        R = mid+b+bb;
        return query(L,R,mid+1,rr,dep+1,k-aa);
    }
}
int main(){

    int ncase,T=1;
    cin >> ncase;
    while(ncase--){
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++){
            scanf("%d",&num[i]);
            seg[0][i] = num[i];
        }
        sort(num+1,num+1+n);
        build(1,n,0);
        printf("Case %d:\n",T++);
        while(m--){
            int a,b,h;
            scanf("%d%d%d",&a,&b,&h);
            ++a,++b;
            int l = 1,r = b-a+1;
            while(l <= r){
                int mid = MD(l,r);
                if(query(a,b,1,n,0,mid) > h){
                    r = mid-1;
                }else{
                    l = mid+1;
                }
            }
            printf("%d\n",r);
        }
    }
    return 0;
}

HDU4417-Super Mario,布布扣,bubuko.com

时间: 2024-10-14 01:52:21

HDU4417-Super Mario的相关文章

hdu-4417 Super Mario(树状数组 + 划分树)

题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is

hdu4417 Super Mario 树状数组离线/划分树

http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2720    Accepted Submission(s): 1322 Problem Description Mario is world-famous plumber

HDU4417 Super Mario(主席树)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4417 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

ACM学习历程—HDU4417 Super Mario(树状数组 &amp;&amp; 离线)

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 (

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learning Project

Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learning Project Posted by ehrenbrav on August 25, 2016Leave a comment (14)Go to comments For those who want to get right to the good stuff, the installati

HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3313    Accepted Submission(s): 1548 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDOJ 4417 Super Mario

划分树+二分 Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2625    Accepted Submission(s): 1274 Problem Description Mario is world-famous plumber. His "burly" figure and amazing ju

Super Mario(线段树离线区间k值)

以前见过这题,没做出来,知道是离线处理,这次仔细想了下, 首先把出现的高度都map离散化一下,以离散化出来的数目g建树,把每个位置都开俩个vector,一个存以这个位置为L的询问,一个存以这个位置为R的询问. 然后从1-g 进行更新,假如当前i是以第j个区间的开始位置,那么这时就可以询问一下<=p[j].h的个数s,显然这时第J个区间多加的,需要减掉,p[j].sum-=s; 然后更新第i个数,update(a[i],1,g,1);再找到某第k区间是以i结尾的,那么依旧询问一下,得出s,p[k]