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的序 ...
随机推荐
- Codeforces 845A. Chess Tourney 思路:简单逻辑题
题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力. 实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢. 叫你把这2*n个选手分成2个有n个选手的队伍. ...
- Git 环境安装
本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.14.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...
- 【原创】JAVA word转html
import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; impor ...
- SpringCloud学习笔记(18)----Spring Cloud Netflix之服务网关Zuul原理
1. Zuul的工作机制 Zuul提供了一个框架,可以对过滤器进行动态的加载,编译,运行.过滤器之间没有直接的相互通信,他们是通过一个RequestContext的静态类来进行数据传递的.Requet ...
- Shiro结合Spring boot开发权限管理系统
前一篇文章说了,我从开始工作就想有一个属于自己的博客系统,当然了,我想的是多用户的博客,大家都可以发文章记笔记,我最初的想法就是这样. 博客系统搭建需要使用的技术: 1.基于Spring boot 2 ...
- 查看centos7启动项
[root@k8s-master ~]# chkconfig Note: This output shows SysV services only and does not include nativ ...
- Linux系统之间文件传输 scp 命令
个人使用记录 scp /home/liwm/Downloads/mysql-5.5.32-linux2.6-x86_64.tar.gz root@192.168.122.3:/home/oldboy/ ...
- Zabbix分布式配置
Zabbix是一个分布式监控系统,它可以以一个中心点.多个分节点的模式运行,使用Proxy能大大的降低Zabbix Server的压力,Zabbix Proxy可以运行在独立的服务器上,安装Zabbi ...
- 【Henu ACM Round#24 C】Quiz
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定是这样 先放k-1个,然后空1个,然后再放k-1个.然后再空1个.. 以此类推. 然后如果(n/k)*(k-1)+n%k> ...
- jsp-include 写法
被包含的页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...