HDU 4417 Super Mario(离线线段树or树状数组)

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

普通的线段树果断T成狗,故转向离线。

将砖块高度val和玛丽能跳的高度H都存起来,更新p[k].val<=q[i].h的,区间求和的时候就能保证当前更新的都是小于玛丽能跳的的高度。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1e5+10;
struct tree{
    int l,r;
    int cnt;
}t[maxn<<2];
struct node1{
    int val;
    int pos;
    bool operator<(const node1 l1)const{
       return val<l1.val;
    }
}p[maxn];//保存玛丽的原始位置和能跳的高度
struct node2{
    int l,r;
    int id,h;
    bool operator<(const node2 l2)const{
        return h<l2.h;
    }
}q[maxn];//保存m次询问
int ans[maxn];
void pushup(int rs)
{
    t[rs].cnt=t[rs<<1].cnt+t[rs<<1|1].cnt;
}
void build(int rs,int l,int r)
{
    t[rs].l=l;
    t[rs].r=r;
    t[rs].cnt=0;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(rs<<1,l,mid);
    build(rs<<1|1,mid+1,r);
}
void update(int rs,int pos)
{
     t[rs].cnt++;
     if(t[rs].l==t[rs].r)
        return ;
    int mid=(t[rs].l+t[rs].r)>>1;
    if(pos<=mid)  update(rs<<1,pos);
    else   update(rs<<1|1,pos);
}
int query(int l,int r,int rs)
{
    if(t[rs].l==l&&t[rs].r==r)
        return t[rs].cnt;
    int mid=(t[rs].l+t[rs].r)>>1;
    if(r<=mid)   return query(l,r,rs<<1);
    else if(l>mid)  return query(l,r,rs<<1|1);
    else  return query(l,mid,rs<<1)+query(mid+1,r,rs<<1|1);
}
int main()
{
    int t,n,m;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i].val);
            p[i].pos=i;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
            q[i].id=i;
        }
        sort(p+1,p+1+n);
        sort(q+1,q+1+m);
        int k=1;
        for(int i=1;i<=m;i++)
        {
            while(k<=n&&p[k].val<=q[i].h)
            {
                update(1,p[k].pos);
                k++;
            }
            ans[q[i].id]=query(q[i].l+1,q[i].r+1,1);
        }
        printf("Case %d:\n",cas++);
        for(int i=1;i<=m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

另外树状数组也可以做。类似的单点更新和区间求和问题都可以用树状数组来写。

类似离线线段树的思想将节点和询问保存排序后依次更新,就能保证结果的正确性。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1e5+10;
int cnt[maxn];
struct node1{
    int val;
    int pos;
    bool operator<(const node1 l1)const{
       return val<l1.val;
    }
}p[maxn];//保存玛丽的原始位置和能跳的高度
struct node2{
    int l,r;
    int id,h;
    bool operator<(const node2 l2)const{
        return h<l2.h;
    }
}q[maxn];//保存m次询问
int ans[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void update(int x)
{
    while(x<maxn)
    {
        cnt[x]++;
        x+=lowbit(x);
    }
}
int query(int x)
{
    int s=0;
    while(x>0)
    {
        s+=cnt[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
    int t,n,m;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(cnt,0,sizeof(cnt));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i].val);
            p[i].pos=i;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
            q[i].id=i;
        }
        sort(p+1,p+1+n);
        sort(q+1,q+1+m);
        int k=1;
        for(int i=1;i<=m;i++)
        {
            while(k<=n&&p[k].val<=q[i].h)
            {
                update(p[k].pos);
                k++;
            }
            ans[q[i].id]=query(q[i].r+1)-query(q[i].l);
        }
        printf("Case %d:\n",cas++);
        for(int i=1;i<=m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}
时间: 2024-10-10 02:56:58

HDU 4417 Super Mario(离线线段树or树状数组)的相关文章

HDU ACM 4417 Super Mario 离线线段树

分析:离线线段树,把所有询问离线读入,然后按H从小到大排序.对于所有结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,最后就是一个区间求和.这题貌似也可以用划分树,树状数组等方法做. #include<iostream> #include<algorithm> using namespace std; #define N 100005 struct Tree { int left,right,cnt; } TREE[N<<2]; struct Query

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

hdu 4417 Super Mario (线段树+动态数组)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2685    Accepted Submission(s): 1306 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDU 4417 Super Mario ( 超级马里奥 + 主席树 + 线段树/树状数组离线处理 + 划分树 )

HDU 4417 - Super Mario ( 主席树 + 线段树/树状数组离线处理 + 划分树 ) 这道题有很多种做法,我先学习的是主席树.后面陆续补上线段树离线和划分树 题目大意就是给定一个区间给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 主席树做法: 最基本的是静态第k大,这里是求静态的 <= K,差不多,在Query操作里面需要修改修改 先建立size棵主席树,然后询问的时候统计的是 第R棵主席树中[1,K]的数量 - 第L-1棵主席树中[1,K]的数量 注意这里下标

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai

HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5101    Accepted Submission(s): 2339 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping abilit

hdu 4417 Super Mario/树套树

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可解吧,可弱弱的沙茶一直没弄懂其精髓,只好用树套树暴力碾压了 额树套树,线段树的每一个节点套一个sb树. 当查询[l,r]区间中的值小于等于H的个数,先用线段树找到相应的区间, 然后再查询该区间下对应的平衡树中小于等于H的个数,累加即可. 一直以为会超时,结果400+ms就过了,数据应该很弱吧(自己对

HDU 4417 Super Mario (划分树)(二分)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6077    Accepted Submission(s): 2645 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ab

hdu 4417 Super Mario(主席树)

题意:给你一些数,有多次询问,问你在l,r区间内小于k的数有多少个 思路:主席树大发好,虽然树状数组和线段树离线也可以做 代码: #include <set> #include <map> #include <queue> #include <stack> #include <math.h> #include <vector> #include <string> #include <stdio.h> #incl