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. Lua4.0中getn陷阱

    Lua4.0中getn潜规则 • 使用t[n] = nil方式删除元素会导致意外结果 t = {, , , , , , , , , }; t[] = nil; t = {, , , , , , , , ...

  2. 2017 Multi-University Training Contest - Team 4 hdu6070 Dirt Ratio

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6070 题面: Dirt Ratio Time Limit: 18000/9000 MS (Ja ...

  3. [转]关于Navicat和MYSQL字符集不统一出现的中文乱码问题

    原文链接:关于Navicat和MYSQL字符集不统一出现的中文乱码问题 最近遇到一串关于MYSQL中文乱码的问题,问题背景是这样的: 在此之前,服务器上安装好MySQL之后就立马重新配置了字符集为ut ...

  4. [leetcode刷题笔记]Implement Trie (Prefix Tree)

    题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...

  5. [Android] 录音与播放录音实现

    http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...

  6. http://www.kindsoft.net/docs/qna.html

    http://www.kindsoft.net/docs/qna.html 感觉 Kindediter 非常好用  界面效果好 API也全面 很不错的编辑器

  7. ASP.NET MVC 必备开发环境

    许多初学者为了搭建开发环境,很多软件找不齐,或者找不到的比较新而且稳定版本.所以我将下载和安装的资料整理了下,供大家下载.资料均收集于网络,但基本核实资料的可靠性,但不能完全保证.如果你在使用过程中发 ...

  8. Java 可重入锁

    一般意义上的可重入锁就是ReentrantLock http://www.cnblogs.com/hongdada/p/6057370.html 广义上的可重入锁是指: 可重入锁,也叫做递归锁,指的是 ...

  9. Nginx+vsftpd搭建图片服务器

    安装Nginx 参考:http://www.cnblogs.com/idefav2010/p/nginx-concat.html Nginx配置文件 location ~ .*\.(gif|jpg|j ...

  10. 解题报告:hdu1008 Elvator

    2017-09-07 19:30:22 writer:pprp 比较顺利,最近生活出现了各种问题, 发生了很多矛盾,我要耐下心来,最重要的不是努力不努力,而是选择 希望我能处理好人际关系还有学业上的压 ...