Super Mario

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

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   |   We have carefully selected several similar problems for you:  5498 5497 5496 5495 5494
给你n个数,让你在一段区间之内求不大于k的值得数有多少个
解决方法:
原模板不动,额外再家一个函数,用二分的方法看mid值去多少的时候《=所给的值,不断的更新ans,最后的ans一定是最大的值
下面代码
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN=;
int tree[][MAXN];//表示每层每个位置的值
int sorted[MAXN];//已经排序的数
int toleft[][MAXN];//toleft[p][i]表示第i层从1到i有多少个数分入左边 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;//从1到i放左边的个数 }
build(l,mid,dep+);
build(mid+,r,dep+); } //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
int query(int L,int R,int l,int r,int dep,int k)
{
if(l==r)return tree[dep][l];
int mid=(L+R)>>;
int cnt=toleft[dep][r]-toleft[dep][l-];//[l,r]中位于左边的个数
if(cnt>=k)
{
//L+要查询的区间前被放在左边的个数
int newl=L+toleft[dep][l-]-toleft[dep][L-];
//左端点加上查询区间会被放在左边的个数
int newr=newl+cnt-;
return query(L,mid,newl,newr,dep+,k);
}
else
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);
}
}
int n,m;
int ans;
void solve(int s,int t,int k){
int l=;
int r=t-s+;
ans=;
while(l<=r){
int mid=(l+r)>>;
int temp=query(,n,s,t,,mid);
if(temp<=k){
ans=mid;
l=mid+;
}
else
r=mid-;
}
printf("%d\n",ans);
} int main(){
int T;
int cas=;
int s,t,k;
scanf("%d",&T);
while(T--){
memset(tree,,sizeof(tree));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&tree[][i]);
sorted[i]=tree[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
printf("Case %d:\n",cas++);
for(int i=;i<=m;i++){
scanf("%d%d%d",&s,&t,&k);
s++;
t++;
solve(s,t,k);
} }
return ;
}

最新文章

  1. emgucv文字识别
  2. JQuery EasyUI validatebox(验证框)
  3. hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
  4. JSBinding / Memory Management (GC)
  5. Idea 安装 lombok
  6. ServiceStack.OrmLite 笔记9 -code first 必须的代码优先
  7. 【HDOJ】1403 Longest Common Substring
  8. Linux目录文件详解FHS标准(2013.09.05)
  9. 苹果公司的新的编程语言 Swift 高级语言()两--基本数据类型
  10. Jmeter对基于websocket协议的压力测试
  11. AX2009 批处理作业中使用多线程---批量捆绑
  12. NET缓存框架CacheManager在混合式开发框架中的应用(1)-CacheManager的介绍和使用
  13. Linux之基础知识
  14. SAP配置BOM的适用范围
  15. 如何理解&lt;base href=&quot;&lt;%=basePath%&gt;&quot;
  16. 【Codeforces811E】Vladik and Entertaining Flags [线段树][并查集]
  17. ABAP术语-Sales Order
  18. LinkedList基本用法
  19. spring中&lt;bean&gt;中parent标签的使用
  20. 魔术师发牌问题--java实现

热门文章

  1. hihocoder1776 序列
  2. 显示大图Activity(支持手势放大)
  3. iOS 多尺寸屏幕适配
  4. mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
  5. 51nod 1412 AVL数的种类(DP
  6. DRBD+NFS+Keepalived高可用环境
  7. python3中bytes、hex和字符串相互转换
  8. python暴力破解wifi密码程序
  9. ios设备屏幕尺寸与分辨率
  10. 【译】Swift 字符串速查表