HDU - 4417 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): 7250    Accepted Submission(s): 3123


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
 

标准的主席树 

需要离散化

#include <queue>
#include <stdio.h>
#include <map>
#include <string>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <set>
using namespace std;
#define maxn 100010
struct xjy
{
    int left;
    int right;
    int num;
};
struct xxjy
{
    int num;
    int id;
    bool operator < (const xxjy &r)const
    {
        return num<r.num;
    }
};
xjy tree[maxn*20];
int T[maxn];
xxjy a[maxn];
int tot;
int n,m;
int build(int left,int right)
{
    int root=tot++;
    if(left==right)
    {
        tree[root].num=0;
        tree[root].left=tree[root].right=0;
        return root;
    }
    int mid=(left+right)>>1;
    tree[root].left=build(left,mid);
    tree[root].right=build(mid+1,right);
    tree[root].num=0;
    return root;
}

int update(int root,int pos,int val)
{
    int now=tot++;
    int tmp=now;
    tree[now].num=tree[root].num+val;
    int left=1;
    int right=n;
    while(left<right)
    {
        int mid=(left+right)>>1;
        if(pos<=mid)
        {
            tree[now].left=tot++;
            tree[now].right=tree[root].right;
            root=tree[root].left;
            now=tree[now].left;
            right=mid;
        }
        else
        {
            tree[now].left=tree[root].left;
            tree[now].right=tot++;
            root=tree[root].right;
            now=tree[now].right;
            left=mid+1;
        }
        tree[now].num=tree[root].num+val;
    }
    return tmp;
}
int ans;
void query(int root,int left,int right,int l,int r)
{
    if(left==l&&right==r)
    {
        ans+=tree[root].num;
        return;
    }
    int mid=(l+r)>>1;
    if(right<=mid)
    {
        query(tree[root].left,left,right,l,mid);
    }
    else if(left>mid)
    {
        query(tree[root].right,left,right,mid+1,r);
    }
    else
    {
        query(tree[root].left,left,mid,l,mid);
        query(tree[root].right,mid+1,right,mid+1,r);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cnt=1;cnt<=t;cnt++)
    {
        printf("Case %d:\n",cnt);
        tot=0;
        scanf("%d%d",&n,&m);
        T[0]=build(1,n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i].num),a[i].id=i;
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++)
            {
                T[i]=update(T[i-1],a[i].id,1);
            }
        for(int i=1;i<=m;i++)
        {
            int l,r,mid;
            scanf("%d%d%d",&l,&r,&mid);
            xxjy midmid;
            midmid.num=mid;
            midmid.id=n+1;
            int time=upper_bound(a+1,a+n+1,midmid)-a-1;
            ans=0;
            query(T[time],l+1,r+1,1,n);
            printf("%d\n",ans);
        }

    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值