Super Mario

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

Total Submission(s): 5370    Accepted Submission(s): 2461

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秒,所以要离散化,否则会超时也会超内存
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map> using namespace std;
typedef long long int LL;
const int maxn=1e5;
int l[maxn*18+5];
int r[maxn*18+5];
int rt[maxn+5];
int num[maxn*18+5];
int p;
int n,m;
int ll,rr;
LL H;
LL a[maxn+5];
LL b[maxn+5];
map<LL,int> mm;
int nownode()
{
l[p]=r[p]=0;
num[p]=0;
return p++;
}
void update(int &node,LL begin,LL end,LL tag)
{
if(!node) node=nownode();
else
{
num[p]=num[node];l[p]=l[node];
r[p]=r[node];node=p;
p++;
}
if(begin==end)
{
num[node]++;
return ;
}
LL mid=(begin+end)>>1;
if(tag<=mid) update(l[node],begin,mid,tag);
else update(r[node],mid+1,end,tag);
num[node]=num[l[node]]+num[r[node]];
}
int query(int node1,int node2,LL begin,LL end,LL tag) {
if(0<=begin&&end<=tag)
{
return num[node2]-num[node1];
}
LL mid=(begin+end)>>1;
if(tag<=mid) return query(l[node1],l[node2],begin,mid,tag);
else return num[l[node2]]-num[l[node1]]+query(r[node1],r[node2],mid+1,end,tag);
}
int bin(LL x)
{
int l=1;int r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]<=x)
l=mid+1;
else
r=mid-1;
}
return r;
}
int main()
{
int t;
scanf("%d",&t);
LL len=1e9;
LL x;
int cas=0;
while(t--)
{
scanf("%d%d",&n,&m);
memset(rt,0,sizeof(rt));
memset(num,0,sizeof(num));
p=1;
for(int i=1;i<=n;i++)
{ scanf("%lld",&a[i]);
b[i]=a[i]; }
sort(a+1,a+n+1);
int tot=0;
mm.clear();
for(int i=1;i<=n;i++)
if(!mm[a[i]])
mm[a[i]]=tot++;
for(int i=1;i<=n;i++)
{
update(rt[i]=rt[i-1],0,maxn,mm[b[i]]);
}
printf("Case %d:\n",++cas);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld",&ll,&rr,&H);
int x=bin(H);
if(x==0) {printf("0\n");continue;}
ll++,rr++;
printf("%d\n",query(rt[ll-1],rt[rr],0,maxn,mm[a[x]]));
}
}
return 0;
}

 

HDU 4417 Super Mario(线段树)的更多相关文章

  1. HDU 4417 Super Mario(划分树)

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

  2. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

  3. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

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

  4. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  5. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  6. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  7. HDU 4417 Super Mario ( 离线树状数组 )

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  8. HDU 4417 Super Mario(划分树+二分)

    题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

  9. hdu 4417 Super Mario 离线线段树

    思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...

随机推荐

  1. 8086汇编之 CALL 和 RET指令

    Ret 和 call 也是转移指令,可是他们跟jmp不同的是,这两个转移指令都跟栈有关系. <1> ret 用栈中的数据改动IP的地址,从而实现近转移 ( ip ) = ( (ss)*16 ...

  2. Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript.

    Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript. 1. 现象::主程序卡住无反应,多行任务不往下执行 1 2. 原因::使用j ...

  3. &quot;围观&quot;设计模式(2)--里氏替换原则(LSP,Liskov Substitution Principle)

    在面向对象的程序设计中.里氏替换原则(Liskov Substitution principle)是对子类型的特别定义.它由芭芭拉·利斯科夫(Barbara Liskov)在1987年在一次会议上名为 ...

  4. CCOrbitCamera

    Cocos2d-x提供了一中根据球面坐标轨迹旋转的方式CCOrbitCamera CC_DEPRECATED_ATTRIBUTE static CCOrbitCamera* actionWithDur ...

  5. Proguard语法及常用proguard.cfg代码段

    本文主要ProGuard常用语法.标准proguard.cfg文件内容.常用proguard.cfg代码段及proguard与log level结合解决debug模式日志问题. 1.ProGuard的 ...

  6. dp背包之01背包poj2184

    http://poj.org/problem?id=2184 题意:给定两个属性,求这两个属性的和的最大值......... 思路:将第一个属性往后平移1000个单位,然后推导其动态转移方程,若是dp ...

  7. 基于js白色简洁样式计算器

    今天给大家分享一款白色简洁样式计算器JS代码是一款精美简洁计算器JS代码插件网页特效,软件应用,后台应用JS计算器插件代码免费下载.适用浏览器:360.FireFox.Chrome.Safari.Op ...

  8. UCOS2系统内核讲述(二)_初始化调用函数

    Ⅰ.写在前面 学习本文之前可以参看我前面的文章: UCOS2_STM32F1移植详细过程(汇总文章) UCOS2系统内核讲述(一)_总体描述 还是按照上一篇文章的思维(从外到内),本文(结合源代码)进 ...

  9. Android基础总结(四)网络通信

    网络图片查看器 确定图片的网址 发送http请求 URL url = new URL(address); //获取连接对象,并没有建立连接 HttpURLConnection conn = (Http ...

  10. CSS3实现0.5px的边框

    前端页面细节处理好了才会显得精致.边框在网页中是常见的一种样式了.虽然不把它处理为0.5px看上去没毛病,但是想让你做的东西征服更多的人,这些细节处理是必须的. 今天主要说一下如何让边框显示0.5px ...