HDU2665 kth number 线段树做法
题意:求区间第k小
思路:
线段树 每个节点上保存 当前区间已经排序好的序列
(归并一下就好了嘛 复杂度 O(l)的)
这样建树的时空复杂度都是 O(nlogn)的
对于 每次询问 二分一个答案
在树上upper_bound一下 判断一下
这样 查询的复杂度 就成O(m*log(inf) * log(n) * log(n))的了
就酱~
但是POJ死也卡不过去……桑心
//By SiriusRen
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 100010
vector<int>tree[N*4];
int n,m,q,xx,yy,zz,Mid,b[N],c[N],cases;
void insert(int l,int r,int pos){
if(l==r){tree[pos].push_back(lower_bound(c+1,c+1+n,b[l])-c);return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
insert(l,mid,lson),insert(mid+1,r,rson);
}
void add(int l,int r,int pos){
if(l==r)return;
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
add(l,mid,lson),add(mid+1,r,rson);
tree[pos].resize(tree[lson].size()+tree[rson].size());
merge(tree[lson].begin(),tree[lson].end(),tree[rson].begin(),tree[rson].end(),tree[pos].begin());
}
int query(int l,int r,int pos){
if(l>=xx&&r<=yy)return upper_bound(tree[pos].begin(),tree[pos].end(),Mid)-tree[pos].begin();
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid<xx)return query(mid+1,r,rson);
else if(mid>=yy)return query(l,mid,lson);
else return query(l,mid,lson)+query(mid+1,r,rson);
}
int main(){
scanf("%d",&cases);
while(cases--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&b[i]),c[i]=b[i];
sort(c+1,c+1+n);
insert(1,n,1),add(1,n,1);
while(m--){
scanf("%d%d%d",&xx,&yy,&zz);
int l=0,r=100000,answer;
while(l<=r){
Mid=(l+r)>>1;
int T=query(1,n,1);
if(T>=zz)answer=Mid,r=Mid-1;
else l=Mid+1;
}
printf("%d\n",c[answer]);
}
for(int i=1;i<=4*N;i++)tree[i].clear();
}
}
HDU2665 kth number 线段树做法的更多相关文章
- K-th Number 线段树(归并树)+二分查找
K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...
- POJ2104 K-th Number(线段树)
题目链接 K-th Number #include <cstdio> #include <cstring> #include <iostream> #include ...
- K-th Number 线段树的区间第K大
http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- [POJ2104/HDU2665]Kth Number-主席树-可持久化线段树
Problem Kth Number Solution 裸的主席树,模板题.但是求k大的时候需要非常注意,很多容易写错的地方.卡了好久.写到最后还给我来个卡空间. 具体做法参见主席树论文<可持久 ...
随机推荐
- finger---用于查找并显示用户信息
finger finger命令用于查找并显示用户信息.包括本地与远端主机的用户皆可,帐号名称没有大小写的差别.单独执行finger指令,它会显示本地主机现在所有的用户的登陆信息,包括帐号名称,真实姓名 ...
- python基础6(函数 Ⅰ)
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段 定义 def function_name(args...): function_body #例子 def print_somethin ...
- 紫书 习题 11-12 UVa 1665 (并查集维护联通分量)
这道题要逆向思维 反过来从大到小枚举, 就是在矩阵中一点一点加进去数字,这样比较 好操作, 如果正着做就要一点一点删除数字, 不好做. 我们需要在这个过程中维护联通块的个数, 这里用到了并查集. 首先 ...
- Echache整合Spring缓存实例讲解
摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCache 介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中 ...
- 国庆 day 7 上午
思路:模拟,set记录一下. #include<set> #include<cstdio> #include<cstring> #include<iostre ...
- Could not load the FreeMarker template named 'select'
眼下项目使用struts2, 所以页面中就使用到了struts2的标签,可是今天在做新的功能的时候突然出现 "Could not load the FreeMarker template n ...
- nyoj--1011--So Easy[II](数学几何水题)
So Easy[II] 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 这是一道基础的计算几何问题(其实这不提示大家也都看的出).问题描述如下: 给你一个N边形.且N边形 ...
- Vue简单用法目录总结 以及 前端基础总结传送门:
Vue官方网址:https://cn.vuejs.org/ Vue 第三方组件:Element:http://element-cn.eleme.io/#/zh-CN Vue 基础指令以及自定义指令:h ...
- 【AngularJS学习笔记】AngularJS表单验证
AngularJS表单验证 AngularJS提供了一些自带的验证属性 1.novalidate:添加到HTML的表单属性中,用于禁用浏览器默认的验证. 2.$dirty 表单有填写记录 3.$v ...
- css inline-block列表布局
一.使用inline-block布局 二.多列布局方法二 <html><head> <meta charset="utf-8"> <tit ...