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.
InputThe 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.)OutputFor 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
题解:和查询区间第k小差不多,只不过修改了下询问函数,主要坑点就是特判下特殊情况。
#include<iostream>
#include<cstring>
#include<string>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=;
int lson[maxn*],rson[maxn*],sum[maxn*],root[maxn],a[maxn];
int cnt,lsh[maxn];
void build(int &rt,int l,int r)
{
rt=++cnt;
if(l==r)return ;
int mid=(l+r)/;
build(lson[rt],l,mid);
build(rson[rt],mid+,r);
}
int update(int last,int l,int r,int pos)
{
int now=++cnt;
lson[now]=lson[last],rson[now]=rson[last],sum[now]=sum[last]+;
if(l==r)return now;
int mid=(l+r)/;
if(pos<=mid)lson[now]=update(lson[now],l,mid,pos);
else rson[now]=update(rson[now],mid+,r,pos);
return now;
}
int querysum(int L,int R,int l,int r,int pos)
{
int mid=(l+r)/;
int ans=;
if(l==r){
return sum[R]-sum[L];
}
if(pos<=mid){
ans+=querysum(lson[L],lson[R],l,mid,pos);
}
else{
ans+=sum[lson[R]]-sum[lson[L]];//左区间的全部符合题意,直接加上
ans+=querysum(rson[L],rson[R],mid+,r,pos);
}
return ans;
}
int main()
{
ios::sync_with_stdio();
int T,top=;
cin>>T;
while(T--){
memset(lson,,sizeof(lson));
memset(rson,,sizeof(rson));
memset(sum,,sizeof(rson));
memset(root,,sizeof(root));
cnt=;
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>a[i];
lsh[i]=a[i];
}
sort(lsh+,lsh++n);
int len=unique(lsh+,lsh++n)-lsh-;
build(root[],,len);
for(int i=;i<=n;i++){
int pos=lower_bound(lsh+,lsh++len,a[i])-lsh;
root[i]=update(root[i-],,len,pos);
}
cout<<"Case "<<top++<<":"<<endl;
for(int i=;i<=m;i++){
int L,R,k;
cin>>L>>R>>k;
L++,R++;
int pos=upper_bound(lsh+,lsh++len,k)-lsh-;
if(pos==){//如果pos=0,不能进去querysum函数,否则会得到错误的结果
cout<<<<endl;
continue;
}
cout<<querysum(root[L-],root[R],,len,pos)<<endl;
}
}
return ;
}

最新文章

  1. DotNet程序配置文件
  2. 【JUC】JDK1.8源码分析之ArrayBlockingQueue(三)
  3. Node实践之二
  4. socket是什么?(翻译)
  5. java集合类的学习(二)
  6. Realm简单使用小记
  7. 用单分子测序(single-molecule sequencing)和局部敏感哈希(locality-sensitive hashing)来组装大型基因组
  8. Android IOS WebRTC 音视频开发总结(七六)-- 探讨直播低延迟低流量的粉丝连麦技术
  9. php安装phalcon扩展
  10. 通过运行时动态给OC分类添加属性
  11. 【BZOJ1030】[JSOI2007]文本生成器
  12. [Hapi.js] Extending the request with lifecycle events
  13. 【JavaScript】标签样式中多出了element.style
  14. Ioc容器BeanPostProcessor-Spring 源码系列(3)
  15. Centos6.5DRBD加载失败,系统更换yum源(国内163)
  16. 【CF446D】DZY Loves Games 高斯消元+矩阵乘法
  17. 【接口时序】3、UART串口收发的原理与Verilog实现
  18. log4j学习(二) 不要用log4j了,用slf4j + logback吧
  19. Jenkins参数化构建(三)之 Jenkins从文件中读取运行参数
  20. Oracle EBS AR应收核销取值

热门文章

  1. 高性能集群软件keepalived
  2. Java实用小工具
  3. Map的6种遍历方法
  4. 白痴级教程,新手看过来,具详细实操文档 (word图片复制不过来,0202年了还有这样的不便利,下回研究一下,图片下次补)
  5. Go-方法-接口-异常处理-错误处理
  6. c语言:自增自减运算符的操作详解
  7. 插曲 强大的神器 vmware
  8. Python说文解字_看起来有点儿像字典的元组(命名元祖)
  9. redis(2)
  10. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]