POJ2104 K-th Number(线段树,二分,vector)
题意
不带修改区间第k小。(n<=100000)
题解
建立线段数和vector数组(vector为当前区间排列之后的序列)(归并)
然后对于每一个询问二分答案。
问题就转化为区间有多少数小于等于二分值。
对于我们每一个遍历的区间(线段数的节点)。
若与询问区间不相交return0。
若完全包含于询问区间则在此区间的vector上二分查找有多少数小于二分值(因为已经排好序,所以很好做)
若有相交部分则继续遍历子树。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
vector<int> d[N*];
struct tree{
int l,r;
}tr[N*];
const int INF=;
int a[N*];
int n,m;
void build(int l,int r,int now){
tr[now].l=l;tr[now].r=r;
if(l==r){
d[now].push_back(-INF);
d[now].push_back(a[l]);
return;
}
int mid=(l+r)>>;
build(l,mid,now*);
build(mid+,r,now*+);
int size1=d[now*].size()-;
int size2=d[now*+].size()-;
int i=,j=;
d[now].push_back(-INF);
while(i<=size1&&j<=size2){
if(d[now*][i]>d[now*+][j])d[now].push_back(d[now*+][j++]);
else d[now].push_back(d[now*][i++]);
}
while(i<=size1)d[now].push_back(d[now*][i++]);
while(j<=size2)d[now].push_back(d[now*+][j++]);
return;
}
int getnum(int now,int K){
int x=;int y=d[now].size()-;
int tmp=;
while(x<=y){
int mid=(x+y)>>;
// cout<<x<<" "<<y<<endl;
if(d[now][mid]<=K){
tmp=mid;
x=mid+;
}
else y=mid-;
}
return tmp;
}
int check(int l,int r,int now,int K){
// cout<<l<<" "<<r<<endl;
if(tr[now].l>r||tr[now].r<l)return ;
if(l<=tr[now].l&&tr[now].r<=r){
// cout<<l<<" "<<r<<" "<<getnum(now,K)<<endl;
return getnum(now,K);
}
int mid=(tr[now].l+tr[now].r)>>;
int tmp1=check(l,r,now*,K);
int tmp2=check(l,r,now*+,K);
return tmp1+tmp2;
}
int search(int l,int r,int k){
int tmp;
int x=-INF;
int y=INF;
while(x<=y){
int mid=(x+y)>>;
int num=check(l,r,,mid);
// cout<<mid<<" "<<num<<endl;
if(num>=k){
tmp=mid;
y=mid-;
}
else x=mid+;
}
return tmp;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
for(int i=;i<=m;i++){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=search(l,r,k);
printf("%d\n",ans);
}
return ;
}
POJ2104 K-th Number(线段树,二分,vector)的更多相关文章
- hdu4614 线段树+二分 插花
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)
我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...
- 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- luogu4422 [COCI2017-2018#1] Deda[线段树二分]
讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...
- bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...
- 2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串)
2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串) https://www.luogu.com.cn/problem/P2824 题意: 在 20 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- [BZOJ 2653] middle(可持久化线段树+二分答案)
[BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...
随机推荐
- System.IO.FileLoadException异常
未能加载文件或程序集“NHibernate, Version=4.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖 ...
- Windows 10 Mobile 演示:系统输入法功能演示
笔者近期会发布多篇<Windows 10 Mobile 演示>文章,帮助想买 Windows 10 手机的朋友了解 Windows 10 Mobile 系统特色.今天给大家带来 Windo ...
- ASP.NET 让ajax请求webform后台方法
$.ajax({ type: "POST", url: ".aspx/getSubjectDirection", data: JSON.stringify({ ...
- IE6 css fixed
.fixed-top{position:fixed;bottom:auto;top:0px;} .fixed-bottom{position:fixed;bottom:0px;top:auto;} . ...
- hiho 1564 - 简单dfs + 宏的危害!!!
题目链接 H公司有 N 台服务器,编号1~N,组成了一个树形结构.其中中央服务器处于根节点,终端服务器处于叶子节点. 中央服务器会向终端服务器发送消息.一条消息会通过中间节点,到达所有的终端服务器.消 ...
- HDU 1754 I Hate It【线段树 单点更新】
题意:给出n个数,每次操作修改它的第s个数,询问给定区间的数的最大值 把前面两道题结合起来就可以了 自己还是敲不出来------------- #include<iostream> #in ...
- shell基础编程
首先要注意的是,Ubuntu里的shell的sh和bash命令是有区别的 如下所示,Ubuntu下的sh指向的dash程序,而bash是dash的增强版,一些bash上能执行的程序在dash上不行 如 ...
- 虚拟机创建后该如何获取IP地址并访问互联网实用教程
之前在做项目的时候主机IP地址.网关.DNS.子网掩码等都是公司或者对方直接给提供的,但是如果我们自己想搭建一台虚拟机或者一台集群的话,手头又没有IP地址,该肿么办呢? 白慌,这里介绍一个小技巧, ...
- 版本号比较[versionCompare]
/*** * 版本号比较 * @param v1 版本号a * @param v2 版本号b * @return -1代表不是合格版本号:0代表一样大.1 代表版本号a大于版本号b.2代表版本号b大于 ...
- Vue中路由的使用
在Vue中动态挂载组件 首先需要安装 cnpm install vue-router --save 在main.js中引入VueRouter 并使用 定义一个路由 创建router实例 通过rout ...