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的序 ...
随机推荐
- 13.boost有向无向图邻接表表示
#include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...
- IEEE Access的模板的问题
这个模板果然问题还是有一些,比如caption换行得自己改class文件.首先感谢一下CSDN的一位网友的经验https://blog.csdn.net/baidu_21381705/article/ ...
- Android 对话框黑色边框的解决
代码解决 : Dialog dialog = new Dialog(this); Window win = dialog.getWindow(); win.setBackgroundDrawableR ...
- Twilio介绍和使用
1.Twilio是?需要如何才能通过Twilio打国际网络电话 http://uuxn.com/twilio-toll-free-sms介绍了通过网页来收取和发送信息 需求:通过TWILIO拨打国外座 ...
- The Structure of an App-ios应用架构-MVC
During startup, the UIApplicationMain function sets up several key objects and starts the app runnin ...
- C语言基础 (3) C语言介绍
01回顾 02 语言介绍 语言是人和人交流,C语言是人和机器交流. 03_为什么学C语言 04_第一个C代码编译运行 #include <stdio.h> int main() { // ...
- 拓展Lucas小结
拓展Lucas是解决大组合数取模非质数(尤其是含平方因子的合数)问题的有力工具... 首先对模数质因数分解,把每个质因子单独拎出来处理答案,然后用中国剩余定理(excrt)合并 问题转化为,对于每个质 ...
- 纯净版linux (debian)挂载VirtualBox共享文件夹
使用的虚拟机版本是:VirtualBox-5.2.8-121009 使用的linux版本是:Linux debian 4.9.0-7-amd64 tty 1. 开始配置 1.1:打开虚拟机设置,打开你 ...
- DBCA创建数据库ORA-01034 ORACLE not available
SYMPTOMS 在利用dbca创建数据库时,当设置完毕全部參数.開始装时 跑到2% 就报错 ORA-01034 ORACLE not available, 例如以下图 watermark/2/tex ...
- ADT+NDK搭建jni编译环境
jni是android调用C++编写的库的接口.C++和java的差别不在此文的讨论范畴,另外这里也仅仅是记录下.怎样搭建好开发环境. 首先是下载ADT包和NDK包. ADT包包括了eclipse.所 ...