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 ...
随机推荐
- SUCTF 2019-EasySQL
0x00 知识点: 1:堆叠注入 2:sql_mode : 它定义了 MySQL 应支持的 SQL 语法,以及应该在数据上执行何种确认检查,其中的 PIPES_AS_CONCAT 将 || 视为字符串 ...
- jvm调优原则
合理规划jvm性能调优 JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考虑各方面的影响.但也有一些基础的理论和原则,理解这些理论并遵循这些原则会让你的性能调优任务将会更加轻松.为了 ...
- 利用mysecureshell搭建sftp服务
1.下载对应的mysecureshell-1.33-1.x86_64.rpm包 2.安装mysecureshell-1.33-1.x86_64.rpm 3.添加ftp用户 useradd ftp 4. ...
- Q4:Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...
- PL/SQL 连接oracle步骤
下面就将PL/SQL的配置说明一下. 一.安装Oracle客户端,让后配置 安装目录下面的C:\ORACLE\instantclient_11_2\NETWORK\ADMIN 的 tnsname ...
- 用PyQt5来即时显示pandas Dataframe的数据,附qdarkstyle黑夜主题样式(美美哒的黑夜主题)
import sys from qdarkstyle import load_stylesheet_pyqt5 from PyQt5.QtWidgets import QApplication, QT ...
- WordPress迁移服务器后报Nginx404的问题
Wordpress迁移服务器后,只有主页能打开,其它页面都显示404 页面无法访问. 出现这个问题是因为我的Wordpress之前用的服务器是apache+PHP组合,换了服务器后变成了Nginx+P ...
- pip速度慢解决办法
pip速度慢解决办法 sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 注意加不加sudo是 ...
- gitee上传下载代码命令
在想要下载的文件夹下,鼠标右键,git bash 1.输入git init 进行初始化 2.git remote add origin https://gitee.com/XXXXXXXXXXXXXX ...
- h5-立方体
1.制作一个立方体:首先要有6个面 <div class="box"> <div class="front">front</div ...