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

Super Mario

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

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


解题心得:

  1. 题意就是有n个水管,每个水管都有高度,马里奥要从水管a走到水管b,此时马里奥能跨过的高度为h,问你在区间[a,b]之间水管高度不大于h的有多少个。
  2. 这个题一看就知道是线段树,但是线段树每一个节点记录的状态肯定不能记录多个高度的数量。很容易想到解决办法,没办法记录多个高度,那就从低的高度开始累加,所以在面对询问的时候将询问按照高度排序,在询问之前更新数量,插入不大于当前询问高度的水管,然后每次用线段树记录当前答案存下来,然后再按照之前询问的顺序输出就行了。
  3. 为了不TLE,要记录水管的位置,然后排序,按照顺序插入。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
using namespace std;
const int maxn = 1e5+100;
struct tree {
    int cnt,l,r;
}node[maxn<<2];

struct Brick{
    int va,pos;
    friend bool operator < (const Brick a, const Brick b){
        return a.va < b.va;
    }
}brick[maxn];

int n,m,ans[maxn];

struct Query{
    int l,r,h,pos;
} qu[maxn];

bool cmp(Query a,Query b){
    return a.h < b.h;
}

void init(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",&brick[i].va);
        brick[i].pos = i;
    }
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].h);
        qu[i].pos = i;
    }
    sort(brick,brick+n);//水管排序
    sort(qu,qu+m,cmp);//询问排序
}

void build(int root,int l,int r){
    node[root].cnt = 0;
    node[root].l = l;
    node[root].r = r;
    if(l == r)
        return ;
    int mid = (l+r) >> 1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}

void insert_tree(int pos,int root,int l,int r){
    node[root].cnt++;
    if(l == r)
        return ;
    int mid = (l+r)>>1;
    if(mid < pos)
        insert_tree(pos,root<<1|1,mid+1,r);
    else
        insert_tree(pos,root<<1,l,mid);
}

int query(int l,int r,int root,int L,int R){
    if(l == L && r == R)
        return node[root].cnt;
    int mid = (L + R)>>1;
    if(mid >= r)
        return query(l,r,root<<1,L,mid);
    else if(mid < l)
        return query(l,r,root<<1|1,mid+1,R);
    else
        return query(l,mid,root<<1,L,mid) + query(mid+1,r,root<<1|1,mid+1,R);
}

void get_ans(){
    int k = 0;
    for(int i=0;i<m;i++){
        while(k<n){
            if(brick[k].va <= qu[i].h){
                insert_tree(brick[k].pos,1,0,n-1);//先插入
                k++;
            }
            else
                break;
        }
        int Ans = query(qu[i].l,qu[i].r,1,0,n-1);//再询问
        ans[qu[i].pos] = Ans;//把答案给记录下来
    }
}

void Print(){
    for(int i=0;i<m;i++)
        printf("%d\n",ans[i]);
}

int main(){
    int t,T = 1;
    scanf("%d",&t);
    while(t--){
        printf("Case %d:\n",T++);
        init();
        build(1,0,n-1);
        get_ans();
        Print();
    }
    return 0;
}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值