Super Mario

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

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
 
Recommend
liuyiding
 

这题就是查询一个区间内小于等于一个数的数的个数。

用树状数组离线搞过。

修改下划分树模板也可以搞定

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN = ;
int tree[][MAXN];
int sorted[MAXN];
int toleft[][MAXN]; void build(int l,int r,int dep)
{
if(l == r)return;
int mid = (l+r)>>;
int same = mid-l+;
for(int i = l;i <= r;i++)
if(tree[dep][i] < sorted[mid])
same--;
int lpos = l;
int rpos = mid+;
for(int i = l;i <= r;i++)
{
if(tree[dep][i] < sorted[mid])
tree[dep+][lpos++] = tree[dep][i];
else if(tree[dep][i] == sorted[mid] && same > )
{
tree[dep+][lpos++] = tree[dep][i];
same--;
}
else
tree[dep+][rpos++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l-] + lpos - l;
}
build(l,mid,dep+);
build(mid+,r,dep+);
}
//查询区间[l,r]上比k小于等于的数的个数
int query(int L,int R,int l,int r,int dep,int k)
{
//printf("%d %d %d %d %d %d\n",L,R,l,r,dep,k);
if(l == r)
{
if(tree[dep][l] <= k)return ;
else return ;
}
int mid = (L+R)>>;
int cnt = toleft[dep][r] - toleft[dep][l-];
if(sorted[mid] <= k)
{
int newr = r + toleft[dep][R] - toleft[dep][r];
int newl = newr - (r-l+-cnt) + ;
return cnt + query(mid+,R,newl,newr,dep+,k);
}
else
{
int newl = L + toleft[dep][l-] - toleft[dep][L-];
int newr = newl + cnt -;
if(newr >= newl)return query(L,mid,newl,newr,dep+,k);
else return ;
}
}
int main()
{
int T;
int iCase = ;
scanf("%d",&T);
int n;
while(T--)
{
iCase ++;
int m;
scanf("%d%d",&n,&m);
memset(tree,,sizeof(tree));
memset(toleft,,sizeof(toleft));
for(int i = ;i <= n;i++)
{
scanf("%d",&tree[][i]);
sorted[i] = tree[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
int L,R,H;
printf("Case %d:\n",iCase);
while(m--)
{
scanf("%d%d%d",&L,&R,&H);
L++;R++;
printf("%d\n",query(,n,L,R,,H));
}
}
return ;
}

HDU 4417 Super Mario(划分树)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 4417 Super Mario 主席树

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

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

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

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

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

  8. 【划分树+二分】HDU 4417 Super Mario

    第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...

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

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

随机推荐

  1. 如何用jQuery获得radio的值

    如何获得radio的值,在网上查了一下,下面总结几种解决方法,. 1.获取选中值: $('input:radio:checked').val(): $("input[type='radio' ...

  2. [New learn]动画-基于UIView

    原文:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/Ani ...

  3. MUI 页面刷新及页面传值问题

    一.页面刷新问题 1.父页面A跳转到子页面B,B页面修改数据后再跳回A页面,刷新A页面数据 (1).父页面A代码 window.addEventListener("pageflowrefre ...

  4. 虚拟存储管理中几种缺页中断算法(最佳置换法OPT)

    缺页中断就是要访问的页不在主存,需要操作系统将其调入主存后再进行访问. 在进行内存访问时,若所访问的页已在主存,则称此次访问成功: 若所访问的页不在主存,则称此次访问失败,并产生缺页中断. 最佳置换法 ...

  5. linux命令(37):locate命令

    1.命令格式: Locate [选择参数] [样式] 2.命令功能: locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期 ...

  6. linux命令(6):tar命令

    压缩方法:tar zcvf test.tar.gz test [表示把文件夹目录压缩成test.tar.gz文件保存] 解压方法:tar zxvf test.tar.gz –C /home [表示解压 ...

  7. css 三(清除浮动专题)

    1.  三个关于浮动的概念  不浮动float:none; 清除周围的浮动元素   float:both   这是清除浮动的本意 清除子元素浮动对父元素的影响  clearfix    很多人都理解成 ...

  8. PL/SQL 11.6 注册码

    PL/SQL Developer 下载地址:Download PL/SQL Developer 11.0.6 注册码 Product Code:4t46t6vydkvsxekkvf3fjnpzy5wb ...

  9. 【剑指offer】面试题 65. 不用加减乘除做加法

    面试题 65. 不用加减乘除做加法 题目描述 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. Java 实现 public class Solution {    ...

  10. 【转】python 之 collections

    转自:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0014110312394 ...