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. 【uva11613】生产销售规划

    这很像之前做的一道noip模拟题…… 所以当时那题也可以用费用流写(雾) 拆点,将每个月拆成两个点,一个向起点连边表示产量,另一个点连汇点表示销量. 然后每个点依次往后面的点2连边,表示保存. #in ...

  2. Educational Codeforces Round 23 补题小结

    昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...

  3. java中this的用法如:this.name=name

    package com.chensi; /** * 这个是为了搞懂那个 this.name = name的. * @author ZHL * */ public class ThisTestZhl { ...

  4. java的装饰设计模式

    类似python中的装饰器. 示例: public class Test5 { public static void main(String[] args) { Worker w = new Work ...

  5. awk处理之案例六:awk根据条件插入文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  6. POJ-1681

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4839   Accepted: 2350 ...

  7. Django web框架之模板继承

    模板继承 (extend) Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 block ...

  8. git学习资源合集

    git官网 Pro git 电子书,这里还有中文版,这也是官方推荐的. 再加一个廖雪峰的简明git教程.

  9. ibatis中的符号#跟$区别

    昨天一个项目中在写ibatis中的sql语句时,order by #field#, 运行时总是报错,后来上网查了查,才知道这里不该用#,而应该用$,随即查了下#与$的区别.  总结如下:  1.#是把 ...

  10. Go语言中的匿名函数和闭包的样子

    1). 函数也是值,可以像普通值那样,传来传去: 2). 匿名函数: 3). 函数的类型,类似于:func(float64,float64) float64 ===================== ...