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. C基础 redis缓存访问

    引言 先说redis安装, 这里采用的环境是. Linux version --generic (buildd@lgw01-) (gcc version (Ubuntu -14ubuntu2) ) # ...

  2. [New learn]@class和#import的区别使用

    1.简介 我们在查看代码的时候经常会发现有些地方使用@class而有些地方使用#import,他们到底有什么区别呢, 本文意图去归纳和总结这两种类引用的是的处理方法和规则. 2.分析 此小节会通过一些 ...

  3. DevExpress控件-GridControl根据条件改变单元格(Dev GridControl 单元格着色)

    DevExpress控件-GridControl根据条件改变单元格颜色,如下图: 解决办法:可以参考:http://www.cnblogs.com/zeroone/p/4311191.html 第一步 ...

  4. IIS 7/8安装SSL证书

    文件说明:1. 证书文件1532113691949.pem,包含两段内容,请不要删除任何一段内容.2. 如果是证书系统创建的CSR,还包含:证书私钥文件1532113691949.key.PFX格式证 ...

  5. Nuget私服使用

    首先前提是师父已经搭好私服环境了(怎么搭建参考https://www.cnblogs.com/liupengblog/archive/2012/09/10/2678508.html). 然后在vs中打 ...

  6. 本人博客已经搬至SegmentFault.com

    本人博客已经搬至SegmentFault.com 具体链接:http://segmentfault.com/blog/zhoutk

  7. git冲突解决的方法

    在运行时,出现了冲突的报错.类似于<<<<<<< HEAD,在你改变的文件有分支与HEAD间的区别.这里就是冲突的地方. 1.解决方法一 使用命令  切换分支 ...

  8. 浅谈对js原型的理解

    一.  在JavaScript中,一切皆对象,每个对象都有一个原型对象(prototype),而指向该原型对象的内部指针则是__proto__.当我们对对象进行for in 或者for of遍历时,就 ...

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

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

  10. 转:fortios 5.4后门植入

    提示: 1.经过实验,fortios 5.4 beta4也是可以的. 2.在实验时,选择先下载fortios 5.2(做了快照),再升级5.4,则虚拟机挂载需要选择FortiGate-VM-disk1 ...