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 ;
}

Super Mario HDU - 4417 (主席树询问区间比k小的个数)的更多相关文章

  1. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  2. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  3. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  4. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  5. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  6. HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)

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

  7. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  8. 主席树(区间第k小的数)

    题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...

  9. 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)

    题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...

随机推荐

  1. 获取ExtJS中开启Form.fileUpload时,返回的信息

    当Form,开启fileUpload后,不能按默认的方式得到action.result,开启fileUpload与否,返回的action.result的内容是不一样的 未开启fileUpload时,返 ...

  2. WebView的学习

    加载网页: 加载URL(网络或者本地assets文件下的html文件) 加载html代码 Native和JavaScript相互调用(利于混合开发) 1.加载网络URL webview.loadUrl ...

  3. 什么是控制反转IOC

    1.IOC 是什么 IOC- Inversion of Control , 即“控制反转” ,不是一个技术,而是一个设计思想,在java 开发中,IOC意味着将你设计好的Java 对象交个容器控制,而 ...

  4. JS-语句三

    关于if语句的几个练习: 1. 输入三个整数,x,y,z,最终以从小到大的方式输出.  思路:先列举出每种可能,然后做if套嵌.        var x = prompt("请输入一个数字 ...

  5. SQL语句、PL/SQL块和SQL*Plus命令之间的区别

    SQL语句.PL/SQL块和SQL*Plus命令之间的区别   原文链接:https://blog.csdn.net/liuzhushiqiang/article/details/12320941 在 ...

  6. UVA 12663 第九届省赛 高桥与低桥 线段树

    题意很简单,n个桥的高度是事先给出来的,然后有m次涨水与落水的高度,问有多少座桥在这m次涨落之后 被淹超过了k次,如果某桥本身被水淹了,此时再涨水,就不能算多淹一次 看下数据10的五次方,10的五次方 ...

  7. Cobub无码埋点关键技术的实现

    随着大数据时代的到来,数据采集也已经变的越来越重要.前端埋点作为一个比较成熟的数据接入手段被广泛应用着.目前埋点分为两种方式,有码与无码埋点.有码埋点比较容易理解,即调用SDK的API,在代码中插入埋 ...

  8. UML-GoF设计模式-总结

    1.GRASP 2.设计模式

  9. 浅copy

    person=['aaa',['a',bbb'] p1=copy.copy(person) p2=person[:] p3=list(person) p4=person.copy() print(ty ...

  10. linux下的hashpump安装

    hashpump是linux上的一个进行hash长度拓展攻击的工具 安装: git clone https://github.com/bwall/HashPump apt-get install g+ ...