前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 4417 Super Mario(线段树)

HDU 4417 Super Mario(线段树)

作者头像
ShenduCC
发布2018-04-27 11:01:05
6150
发布2018-04-27 11:01:05
举报
文章被收录于专栏:算法修养

Super Mario

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

Total Submission(s): 5370    Accepted Submission(s): 2461

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

代码语言:javascript
复制
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

代码语言:javascript
复制
Case 1:
4
0
0
3
1
2
0
1
5
1
可持久化线段树,求动态区间比某个值小的有几个。时间限制1秒,所以要离散化,否则会超时也会超内存#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map>

using namespace std;
typedef long long int LL;
const int maxn=1e5;
int l[maxn*18+5];
int r[maxn*18+5];
int rt[maxn+5];
int num[maxn*18+5];
int p;
int n,m;
int ll,rr;
LL H;
LL a[maxn+5];
LL b[maxn+5];
map<LL,int> mm;
int nownode()
{
    l[p]=r[p]=0;
    num[p]=0;
    return p++;
}
void update(int &node,LL begin,LL end,LL tag)
{
    if(!node) node=nownode();
    else
    {
        num[p]=num[node];l[p]=l[node];
        r[p]=r[node];node=p;
        p++;
    }
    if(begin==end)
    {
         num[node]++;
         return ;
    }
    LL mid=(begin+end)>>1;
    if(tag<=mid) update(l[node],begin,mid,tag);
    else update(r[node],mid+1,end,tag);
    num[node]=num[l[node]]+num[r[node]];
}
int query(int node1,int node2,LL begin,LL end,LL tag)

{
    if(0<=begin&&end<=tag)
    {
        return num[node2]-num[node1];
    }
    LL mid=(begin+end)>>1;
    if(tag<=mid) return query(l[node1],l[node2],begin,mid,tag);
    else return num[l[node2]]-num[l[node1]]+query(r[node1],r[node2],mid+1,end,tag);
}
int bin(LL x)
{
    int l=1;int r=n;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(a[mid]<=x)
            l=mid+1;
        else
            r=mid-1;
    }
    return r;
}
int main()
{
    int t;
    scanf("%d",&t);
    LL len=1e9;
    LL x;
    int cas=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(rt,0,sizeof(rt));
        memset(num,0,sizeof(num));
        p=1;
        for(int i=1;i<=n;i++)
        {

            scanf("%lld",&a[i]);
            b[i]=a[i];

        }
        sort(a+1,a+n+1);
        int tot=0;
        mm.clear();
        for(int i=1;i<=n;i++)
            if(!mm[a[i]]) 
                mm[a[i]]=tot++;
        for(int i=1;i<=n;i++)
        {
            update(rt[i]=rt[i-1],0,maxn,mm[b[i]]);
        }
        printf("Case %d:\n",++cas);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%lld",&ll,&rr,&H);
            int x=bin(H);
            if(x==0) {printf("0\n");continue;}
            ll++,rr++;
            printf("%d\n",query(rt[ll-1],rt[rr],0,maxn,mm[a[x]]));
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-10-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Super Mario
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档