HDU-4417-Super Mario(线段树+离线)

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8331    Accepted Submission(s): 3523



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
 


Source
2012 ACM/ICPC Asia Regional Hangzhou Online

题意:n个数m次查询,每次求在l到r区间内小于等于H的书有多少个;
思路:这题很显然需要离线处理,每个数从小到大排序,每个询问按H的大小从小到大排序,对于每次询问,把小于等于H的数更新到线段树中,然后区间求和即可;
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
struct node
{
    int x,pos;
    node(){};
    node(int tx,int tpos){x=tx;pos=tpos;}
}arr[maxn];
bool cmp1(node a,node b)
{
    if(a.x==b.x) return a.pos<b.pos;
    return a.x<b.x;
}
struct ques
{
    int l,r,k,id;
}Q[maxn];
bool cmp(ques a,ques b)
{
    if(a.k==b.k){
        if(a.l==b.l) return a.r<b.r;
        else return a.l<b.l;
    }
    return a.k<b.k;
}

struct NODE
{
    int l,r;
    int sum;
}tree[maxn*4];
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].sum=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}
void pushup(int root)
{
    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}
void update(int root,int pos)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(ll==rr){
        tree[root].sum++;
        return;
    }
    int mid=(ll+rr)>>1;
    if(pos<=mid) update(root<<1,pos);
    else update(root<<1|1,pos);
    pushup(root);
}
int query(int root,int l,int r)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(l<=ll&&rr<=r)
        return tree[root].sum;
    int ans=0;
    int mid=(ll+rr)>>1;
    if(l<=mid) ans+=query(root<<1,l,r);
    if(r>mid) ans+=query(root<<1|1,l,r);
    return ans;
}
int n,m,ans[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int x;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            arr[i]=node(x,i);
        }
        int l,r,k;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&Q[i].l,&Q[i].r,&Q[i].k);
            Q[i].l++;Q[i].r++;
            Q[i].id=i;
        }
        sort(Q+1,Q+1+m,cmp);
        sort(arr+1,arr+1+n,cmp1);
        build(1,1,n);
        int cnt=1;
        for(int i=1;i<=m;i++){
            while(cnt<=n&&arr[cnt].x<=Q[i].k){
                update(1,arr[cnt].pos);
                cnt++;
            }
            ans[Q[i].id]=query(1,Q[i].l,Q[i].r);
        }
        printf("Case %d:\n",cas++);
        for(int i=1;i<=m;i++){
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值