Super Mario HDU - 4417 (主席树询问区间比k小的个数)
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小的个数)的更多相关文章
- Super Mario HDU 4417 主席树区间查询
Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- J - Super Mario HDU - 4417 线段树 离线处理 区间排序
J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...
- HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- 主席树(区间第k小的数)
题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...
- 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)
题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...
随机推荐
- psp --2
PSP0 ---2 项目计划日志 姓名:赵腾 日期:9/12/2017 任务 日期 听课 编写程序 阅读课 ...
- Tensorflow学习教程------变量
#coding:utf-8 import tensorflow as tf x = tf.Variable([1,2]) a = tf.constant([3,3]) #增加一个减法op sub = ...
- 冒泡排序_python
def popdata(ls): for i in range(len(ls)): for j in range(i+1,len(ls)): if ls[i]>ls[j]: # tmp=ls[i ...
- Python笔记_第五篇_Python数据分析基础教程_相关安装和版本查看
1. IDE说明: 所有的案例用Anacoda中的Jupiter工具进行交互式讲解. 2. 版本和安装: NumPy从如下网站安装:http://sourceforge.net/projects/nu ...
- JavaScript之HTML DOM Event
当鼠标在button上点击时,会在button上触发一个click事件.但是button是div的一个子元素, 在button里点击相当于在div里点击,是否click事件也会触发在div上?如果cl ...
- == 与 equals区别(HashCode方法)
1:==分析 1.2:基本类型比较 判断基本类型的数值是不是相等 1.3:对象类型比较 判断两个引用是不是指向同一个对象,即内存地址是不是相等. 2:equals分析 来判断对象内容是不是相等,一般有 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 序列使用
MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 使用 AUTO_INCREMENT ...
- Okhttp 多次调用同一个方法出现错误java.net.SocketException: Socket closed
Okhttp 多次调用同一个方法出现错误java.net.SocketException: Socket closed https://blog.csdn.net/QQiqq1314/article/ ...
- Python语言基础与应用 (P16)上机练习:基本数据类型
本文是笔者在学习MOOC课程<Python语言基础与应用> (北京大学-陈斌)中根据上机课时的要求写下在代码 课程总链接: 中国大学MOOC B站 本节课链接 数值基本运算: 33和7+, ...
- Bezier曲线的实现——de Casteljau算法
这学期同时上了计算机图形学和计算方法两门课,学到这部分的时候突然觉得de Casteljau递推算法特别像牛顿插值,尤其递推计算步骤很像牛顿差商表. 一开始用伯恩斯坦多项式计算Bezier曲线的时候, ...