hdu4417Super 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): 5531    Accepted Submission(s): 2514


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

题意:求区间【l,r】内不大于k的数有多少个

思路:假如求的是【l,r】内的第k大(小)那是比较熟悉的,上主席树,那么现在的难题就是如何处理不大于k……其实还是用主席树,还是用线段树的区间求和。只不过求的是在主席树[l,r]上的【1,x】(x<=k)的和。

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N=1e5+10;
int a[N],haha[N],cntN;
int tot;
int root[N],sum[N*20],lson[N*20],rson[N*20];
int build(int l,int r)
{
    int rt = ++tot;
    sum[rt] = 0;
    if(l != r)
    {
        int mid = l+r>>1;
        lson[rt] = build(l,mid);
        rson[rt] = build(mid+1,r);
    }
    return rt;
}
int update(int rt,int pos,int val)
{
    int l = 1,r = cntN;
    int newrt = ++tot,tmp = newrt;
    sum[newrt] = sum[rt] + val;
    while(l < r)
    {
        int mid = l+r>>1;
        if(pos <= mid)
        {
            r = mid;
            lson[newrt] = ++tot;
            rson[newrt] = rson[rt];
            newrt = lson[newrt];
            rt = lson[rt];
        }
        else
        {
            l = mid + 1;
            rson[newrt] = ++tot;
            lson[newrt] = lson[rt];
            newrt = rson[newrt];
            rt = rson[rt];
        }
        sum[newrt] = sum[rt] + val;
    }
    return tmp;
}
int query(int l_root,int r_root,int l,int r,int L,int R)
{
    if(L <= l && r <= R)return sum[r_root] - sum[l_root];
    int mid = l+r>>1;
    if(R <= mid)return query(lson[l_root],lson[r_root],l,mid,L,R);
    else if(L > mid)return query(rson[l_root],rson[r_root],mid+1,r,L,R);
    else
    {
        return query(lson[l_root],lson[r_root],l,mid,L,mid)+query(rson[l_root],rson[r_root],mid+1,r,mid+1,R);
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    int n,m,t;
    scanf("%d",&t);
    for(int ca = 1; ca <= t; ca++)
    {
        tot = 0;
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            haha[i] = a[i];
        }
        sort(haha+1,haha+1+n);
        cntN = unique(haha+1,haha+1+n) - haha-1;
        root[0] = build(1,cntN);
        for(int i = 1; i <= n; i++)
        {
            a[i] = lower_bound(haha+1,haha+cntN+1,a[i]) - haha;
            root[i] = update(root[i-1],a[i],1);
        }
        printf("Case %d:\n",ca);
        int l,r,h;
        while(m--)
        {
            scanf("%d%d%d",&l,&r,&h);
            l++,r++;
            int tmp = lower_bound(haha+1,haha+cntN+1,h) - haha ;
            if(haha[tmp] != h)tmp--;
            printf("%d\n",tmp?query(root[l-1],root[r],1,cntN,1,tmp):0);
        }
    }
    return 0;
}

树状数组做法,还是相当的机智…;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const int MAXN=100010;
int c[MAXN],ans[MAXN];
int n;

struct Node
{
    int val,id;
    bool operator < (const Node &a)const
    {
        return val < a.val;
    }
} node[MAXN];
struct Seg
{
    int l,r,h,id;
    bool operator <(const Seg &a)const
    {
        return h < a.h;
    }
} seg[MAXN];

void add(int pos,int k)
{
    while(pos <= n)
    {
        c[pos] += k;
        pos += (pos&(-pos));
    }
}
int Sum(int pos)
{
    int ret = 0;
    while(pos > 0)
    {
        ret += c[pos];
        pos -= (pos&(-pos));
    }
    return ret;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int q,t;
    scanf("%d",&t);
    for(int ca = 1; ca <= t; ca++)
    {
        memset(c,0,sizeof(c));
        memset(ans,0,sizeof(ans));
        scanf("%d%d",&n,&q);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&node[i].val);
            node[i].id = i;
        }
        sort(node+1,node+1+n);
        for(int i = 1; i <= q; i++)
        {
            scanf("%d%d%d",&seg[i].l,&seg[i].r,&seg[i].h);
            seg[i].l ++;
            seg[i].r ++;
            seg[i].id = i;
        }
        sort(seg+1,seg+1+q);
        printf("Case %d:\n",ca);
        int j = 1,i = 1;
        while(i <= q)
        {
            while(j <= n)
            {
                if(seg[i].h < node[j].val)break;
                add(node[j].id,1);
                j++;
            }
            ans[seg[i].id] = Sum(seg[i].r) - Sum(seg[i].l-1);
            i++;
        }
        for(int i = 1; i <= q; 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、付费专栏及课程。

余额充值