Super Mario

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

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
 也是一道很好的区间求和的题目,与那道pingpong很相似可惜我思路错了越写越复杂,最后看的别人的思路,真是惭愧= =
难点不在于写区间求和的数据结构,这个很容易完成无论是BIT还是线段树代码量都不会很大,难点在于将题目所给的模型转化为容易求解的方式。
题目很容易看懂,从左至右给每个位置一个值,接着进行m次询问,(l,r,h) 求[l,r]位置之间<=h的值的位置总数。
暴力的话显然会超时,注意一点h可能会很大达到1e9但是n和m最大1e5,一开始我想的是将h值进行离散化操作保持小的数离散化之后仍然是较小的数,
然后定义线段树以H值为区间,最多10w个H,所以H最大10w,然后进行两次BIT算出L[i],R[i],表示li,ri之前满足要求的数,最后进行相减。
,可后来发现不对,因为询问的时候可能出现不属于离散化的n个数的数,这样的话除非对着10w个数也进行离散化,代码很冗余。
看了别人的思路后恍然大悟,正好和我的相反却使得问题很简单,他就是把位置作为区间段,结点值表示当前区间内满足要求的位置数,
这里用到了离线处理的思想,将m个询问按照H值升序排列,这样的好处在于,后面的询问H不会因为前面更新的值产生影响,因为<=Hpre一定<=H
巧妙地思路!
 
BIT和ST代码,时间差不大多,空间自然BIT好点;
 /*树状数组*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN (100000+15)
int C[MAXN];
inline int lowbit(int x){return x&-x;}
inline int sum(int x)
{
int ret=;
for(;x;x-=lowbit(x)) ret+=C[x];
return ret;
}
inline void add(int x,int d,int n)
{
while(x<=n){
C[x]+=d;
x+=lowbit(x);
}
}
struct node
{
int l,r,h,id,ans;
}P[MAXN];
bool cmpid(node A,node B){return A.id<B.id;}
bool cnph(node A,node B){return A.h<B.h;}
struct node2
{
int h,id;
bool operator<(const node2& x)const{
return h<x.h;}
}Q[MAXN];
int main()
{
int t,n,m,i,j,k=,s;
scanf("%d",&t);
for(k=;k<=t;++k)
{
memset(C,,sizeof(C));
scanf("%d%d",&n,&m);
for(i=;i<=n;++i) scanf("%d",&Q[i].h),Q[i].id=i;
sort(Q+,Q++n);
for(i=;i<=m;++i)
{
scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].h);
P[i].l++;
P[i].r++;
P[i].id=i;
}
printf("Case %d:\n",k);
sort(P+,P++m,cnph);
for(i=,j=;i<=m;++i)
{
while(j<=n&&Q[j].h<=P[i].h) add(Q[j++].id,,n);
P[i].ans=sum(P[i].r)-sum(P[i].l-);
}
sort(P+,P++m,cmpid);
for(i=;i<=m;++i)
printf("%d\n",P[i].ans);
}
return ;
} /* 线段树*/
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000
#define lc (id<<1)
#define rc (id<<1|1)
#define M ((L+R)>>1)
int C[(MAXN<<)+];
void update(int L,int R,int id,int x)
{
if(L==R){C[id]++;return;}
if(x<=M) update(L,M,lc,x);
else update(M+,R,rc,x);
C[id]=C[lc]+C[rc];
}
int query(int L,int R,int id,int l,int r)
{
if(L>=l&&R<=r){return C[id];}
if(r<=M) return query(L,M,lc,l,r);
else if(l>M) return query(M+,R,rc,l,r);
else return query(L,M,lc,l,r)+query(M+,R,rc,l,r);
}
struct node
{
int l,r,h,id,ans;
}P[MAXN+];
bool cmpid(node A,node B){return A.id<B.id;}
bool cmph(node A,node B){return A.h<B.h;}
struct node2
{
int h,id;
bool operator<(const node2&x)const{return h<x.h;}
}Q[MAXN+];
int main()
{
int t,i,j,n,m,k=;
scanf("%d",&t);
for(k=;k<=t;++k)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;++i) scanf("%d",&Q[i].h),Q[i].id=i;
sort(Q+,Q++n);
for(i=;i<=m;++i)
{
scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].h);
P[i].l++;
P[i].r++;
P[i].id=i;
}
sort(P+,P++m,cmph);
for(i=,j=;i<=m;++i)
{
while(j<=n&&Q[j].h<=P[i].h){update(,n,,Q[j].id);j++;}
P[i].ans=query(,n,,P[i].l,P[i].r);
}
sort(P+,P++m,cmpid);
printf("Case %d:\n",k);
for(i=;i<=m;++i) printf("%d\n",P[i].ans);
memset(C,,sizeof(C));
}
return ;
}

HDU 4417 BIT or ST的更多相关文章

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

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  3. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  4. HDU 4417 (划分树+区间小于k统计)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...

  5. HDU 4417:Super Mario(主席树)

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的 ...

  6. [HDU 4417] Super Mario (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个 ...

  7. hdu 4417 Super Mario/树套树

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...

  8. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  9. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

随机推荐

  1. Delphi程序调用C#.Net编译的DLL并打开窗体(详解)

    Delphi程序调用C#.Net编译的DLL并打开窗体(详解)最近用C#.Net写了一个公用模块, 本以为仅提供给.Net程序使用, 但是领导要求把这些功能提供给旧系统使用, 天啦, 几套旧系统全是D ...

  2. centos7安装rabbitmq并简单使用

    先安装erlang rpm -Uvh http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el7.centos.x86_64.rpm 安装rab ...

  3. 微信小程序组件loading

    操作反馈loading:官方文档 Demo Code: Page({ data: { hidden: true }, loadingChange: function () { console.log( ...

  4. bin2lib shell脚本

    #!/bin/sh#输入文件名filename=$1#分割文件大小filesize=4096#输出库文件名libname="lib"$(echo $filename | tr . ...

  5. DataTable转换成IList

    //文章出处: http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html DataTable转换成IList 在用C#作开发的时候经常要把 ...

  6. bash 获取时间段内的日志内容

    需求,获取时段内的/var/log/messages文件内出现错误的消息,支持多行的消息,支持天,小时分钟,秒级的区间,可以修改监控的日志对象 #!/bin/bash if [ $# != 1 ] ; ...

  7. 【c++ primer, 5e】【try语句块】

    p172~p177:c++的try语句块和异常处理: 1.通常,与用户交互的代码和对象相加(底层的代码)是分离开的,异常由与用户交互的代码处理(底层代码抛出异常就可以了). 2.C++的runtime ...

  8. centos7安装kvm环境采用网桥模式并创建虚拟机制作openstack需要的镜像

    初始环境的安装:centos7 mini iso镜像进行安装的系统 采用的环境是vm该软件,联网方式NAT模式下配置的静态ip(如何在NAT模式下配置静态ip参考之前的文章) 1.由于要安装kvm环境 ...

  9. Ubuntu 18.04配置机场客户端

    最近把自己的笔记本电脑安装成ubuntu18.04操作系统,为了更方便的查找文档,所以需要配置一下机场(v2ray)的客户端方便查找资料,以下是配置步骤: 1.下载并执行一键脚本: bash < ...

  10. php 与 c++ openssl 加密通信

    $key = '1234567890123456'; $iv = '1234567890123456'; $enc = openssl_encrypt("hello wolrd!" ...