Super Mario(线段树 + 离散化)

Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7756 Accepted Submission(s): 3322

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个查询命令 查l,r 区间内有多少比h小的个数
第一次做离散化的处理 给我感觉就是我们并不是一开始就建树 而是在处理的过程中进行动态建树 其实这里还可以用主席树求第k个大的数是什么(可惜主席树还不会…….) 所以只能离散化处理了 首先我们需要将n个数的下标和值进行存储在一个结构体内 然后再讲待查询的数存储在另外一个结构体内 记住下标一定要存储 因为我们在最后输出答案时需要根据下标进行输出然后我们在将两个结构体数组按照高度从小到大进行排序 然后进行一一的比较 如果brick[i].h

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>

using namespace std;
#define MAXN 100007
struct Lnode
{
    int pos;//记录当前节点位置
    int h;//记录当前节点的高度
}brick[MAXN];
struct Tnode
{
    int pos;
    int h;
    int l;
    int r;
}node[MAXN];
int n,m;
int ans[MAXN];
int sum[MAXN*4];
bool mycmp(struct Lnode a,struct Lnode b)//高度从小到大进行排序
{
    return a.h < b.h;
}
bool cmp(struct Tnode a, struct Tnode b)
{
    return a.h < b.h;
}
void insertNode(int pos,int rt,int l,int r)
{
    if(l==r)
    {
        sum[rt] =1;
        return;
    }
    int mid = (l+r)/2;
    if(pos<= mid)
        insertNode(pos,rt*2,l,mid);
    else
        insertNode(pos,rt*2+1,mid+1,r);
    sum[rt] = sum[rt*2] + sum[rt*2+1];
}
int query(int l,int r,int L,int R,int rt)
{
    if(l==L && r == R)
    {
        return sum[rt];
    }
    int mid = (L+R)/2;
    if(r <= mid)
        return query(l,r,L,mid,rt*2);
    else if(l>mid)
        return query(l,r,mid +1,R,rt*2+1);
    else
        return query(l,mid,L,mid,rt*2) + query(mid +1,r,mid+1,R,rt*2+1);
}
int main()
{
    int ncase;
    scanf("%d",&ncase);
    int cnt =1;
    while(ncase--)
    {
        printf("Case %d:\n",cnt++);
        scanf("%d%d",&n,&m);
        memset(brick,0,sizeof(brick));
        memset(node,0,sizeof(node));
        memset(ans,0,sizeof(ans));
        memset(sum,0,sizeof(sum));
        for(int i =1;i<= n;i++)
        {
            scanf("%d",&brick[i].h);
            brick[i].pos = i;
        }
        for(int i =1;i<=m;i++)
        {
            scanf("%d%d%d",&node[i].l,&node[i].r,&node[i].h);
            node[i].pos = i;
            node[i].l++;
            node[i].r++;
        }
        sort(brick +1 ,brick +n+1,mycmp);
        sort(node +1 ,node +m+1,cmp);
        int j =1;
        for(int i =1;i<= n;)
        {
            if(brick[i].h > node[j].h)//如果当前节点比要查询的节点大 那么就不需要进行插入了 如果比其结果还要小就需要进行插入
            {
               ans[node[j].pos] = query(node[j].l,node[j].r,1,n,1);
               j++;
               if(j>m)
                break;
            }
            else
            {
                insertNode(brick[i].pos,1,1,n);
                i++;
            }
        }
        for(;j<=m;j++)
        {
            ans[node[j].pos] = query(node[j].l,node[j].r,1,n,1);
        }
        for(int i =1;i<=m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值