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. 调用约定__stdcall / __cdecl

    __cdecl与__stdcall这两种调用约定之间的主要差别在于由谁来执行对参数的清理工作. 如果是__cdecl,那么主调函数将负责执行清理工作,如果是__stdcall那被调函数将负责执行清理. ...

  2. ubuntu下面嘚一些常用基本命令

    1)环境变量配置: 9 ~/.bashrcor ~/.bash_profile. sudo gedit ~/.bashrc 第一种sudo vim ~/.bashrc export PYTHONPAT ...

  3. 转 SQL 的数据库 架构规范 之 58到家数据库30条军规解读

    军规适用场景:并发量大.数据量大的互联网业务 军规:介绍内容 解读:讲解原因,解读比军规更重要 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存 ...

  4. mysql时间类型总结及常用时间函数

    日期时间和类型 常用日期和时间类型 字节 year                1       表示年份                   值范围:(1901----2155) date     ...

  5. 流程引擎表单引擎的常见问题技术交流-关于广州xx公司对驰骋BPM提出

    第1章: 先使用.net 再使用java,数据迁移问题?会存在哪些问题. RE: .net 版本的ccflow与java版本的jflow系列版本都是一个数据库结构,一个操作手册,流程模版,表单模版通用 ...

  6. dac oracle

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  7. c++ 排序 冒泡 插入 选择 快速

    //冒泡 #include <iostream> using namespace std; void bubbleSort(int* list,int index) { ;i--) //i ...

  8. php对象: __clone, __toString, __call,__isset, __unset, __sleep, __wakeup,

    __clone: 克隆对象,自动完成操作   clone()  __toString:  return返回字符串  __call: 当调用不存在的函数时,自动执行该方法,并返回相关值 __isset: ...

  9. 教你如何使用JavaScript入门

    JavaScript简介   JavaScript是NetScape公司为Navigator浏览器开发的,是web前端卸载HTML文件中的一种脚本语言,能实现网页内容的交互显示.当用户在客户端显示该网 ...

  10. MySQL数据库索引常见问题

    笔者看过很多数据库相关方面的面试题,但大多数答案都不太准确,因此决定在自己blog进行一个总结. Q1:数据库有哪些索引?优缺点是什么? 1.B树索引:大多数数据库采用的索引(innoDB采用的是b+ ...