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. STL make_heap push_heap pop_heap sort_heap

    make_heap: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessItera ...

  2. Linux系统——引导过程与服务控制

    一.Linux开机启动原理(十步) (1)开机自检BIOS 开机检测,主板检测 (2)MBR引导 硬盘512字节 (3)GRUB菜单 操作系统菜单 (4)加载内核(kernel) 启动操作系统核心,根 ...

  3. java实现FTP下载文件

    ftp上传下载文件,是遵照ftp协议上传下载文件的,本例仅以下载文件为例. 重要的方法解释: 1.FTP功能相关依赖路径:org.apache.commons.net.ftp.*: 2.ftp默认端口 ...

  4. CCF 炉石传说(模拟)

    试题编号: 201612-3 试题名称: 炉石传说 时间限制: 1.0s 内存限制: 256.0MB 问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Wa ...

  5. ACM对拍程序

    1.把所需对拍的代码的可执行文件a.exe b.exe放在同一目录下 2.把rand数据的代码的可执行文件c.exe放在该目录下 3.新建一个txt文件,里面添加代码,后把格式改成bat @echo ...

  6. linux下不同服务器间数据传输(wget,scp)

    一.wget是Linux下最常用的http/ftp文件下载工具1.wget断点续传,只需要加上-c参数即可,例如:代码:wget-chttp://www.abc.com/abc.zip-Oabc.zi ...

  7. ORA-00257: archiver error的解决方法

    背景:多个用户同时做测试数据,有时候突然Oracle系统就崩溃了,然后报一个ORA-00257: archiver error. Connect internal only, until freed的 ...

  8. 【c++ primer, 5e】构造函数 & 拷贝、赋值和析构

    [构造函数] 1.构造器就是创建对象时被调用的代码. 2.如果没有自定义构造器,那么编译器将自动合成一个默认的无参构造器. 3.自定义的构造器不允许加const,所创建const的对象只有在构造器代码 ...

  9. 照着官网来安装openstack pike之nova安装

    nova组件安装分为控制节点和计算节点,还是先从控制节点安装 1.前提条件,数据库为nova创建库和账户密码来连接数据库 # mysql -u root -p MariaDB [(none)]> ...

  10. Zoom Me FAQ

    Q: How to config custom shortcuts? A: Enter the preferences setting window from menu bar "Prefe ...