题意

不带修改区间第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)的更多相关文章

  1. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  2. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...

  3. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  4. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  5. luogu4422 [COCI2017-2018#1] Deda[线段树二分]

    讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...

  6. bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...

  7. 2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串)

    2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串) https://www.luogu.com.cn/problem/P2824 题意: 在 20 ...

  8. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  9. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  10. [BZOJ 2653] middle(可持久化线段树+二分答案)

    [BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...

随机推荐

  1. 12.boost有向图无向图(矩阵法)

    #include <iostream> #include <boost/config.hpp> //图 #include <boost/graph/adjacency_m ...

  2. validform

    一.validform是什么?            validform是一款智能的表单验证js插件,它是基于jQuery库与css,我们只需要把表单对象放入,             就可以对整个表 ...

  3. 002.ActiveMQ的安装

    本安装说明基于CentOS7.1的版本,其他版本也基本可以参考. 1. 安装JDK1.8 CentOS7.1版本安装时忘记是默认还是自己选中的安装了OpenJDK,也是1.8的版本,因为ActiveM ...

  4. 如何在SQLServer中处理每天四亿三千万记录的

    项目背景 这是给某数据中心做的一个项目,项目难度之大令人发指,这个项目真正的让我感觉到了,商场如战场,而我只是其中的一个小兵,太多的战术,太多的高层之间的较量,太多的内幕了.具体这个项目的情况,我有空 ...

  5. Html表单提交到Servlet输出到页面乱码

    Html使用的编码是UTF-8编码显示页面,之后使用form表单提交字段到Servlet中,Servlet将利用getParamer方法获得form提交的字段,之后通过Respone中的writer将 ...

  6. XML文件基础,DTD校验文件编写,Schema文件的简单使用

    dtd <!-- <!ELEMENT 元素(子元素,...)> --> <!ELEMENT students (student+,cat*) > <!ELEM ...

  7. 解决无法启动“start web server”:

    1.提示1080端口被占用: Cmd——Netstat -ano——找到端口号为1080的pid——打开任务管理器——干掉pid 2.inter error:your request was unsu ...

  8. BZOJ 3790 神奇项链(manacher+贪心)

    3790: 神奇项链 Time Limit: 10 Sec  Memory Limit: 64 MB Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小 ...

  9. springboot ajax返回html

    因为拦截器 或者是 shiro  拦截登陆接口

  10. 双系统 windows引导项添加

    [root@MiWiFi-R2D-srv ~]# vi /etc/grub.d/40_custom #!/bin/sh exec tail -n +3 $0# This file provides a ...