题意:求区间第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 线段树做法的更多相关文章

  1. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

  2. POJ2104 K-th Number(线段树)

    题目链接 K-th Number #include <cstdio> #include <cstring> #include <iostream> #include ...

  3. K-th Number 线段树的区间第K大

    http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...

  4. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  5. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  6. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

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

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

  8. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  9. [POJ2104/HDU2665]Kth Number-主席树-可持久化线段树

    Problem Kth Number Solution 裸的主席树,模板题.但是求k大的时候需要非常注意,很多容易写错的地方.卡了好久.写到最后还给我来个卡空间. 具体做法参见主席树论文<可持久 ...

随机推荐

  1. Java XSSF 导出excel 工具类

    参数解释: title:导出excel标题.headers 导出到excel显示的列头. columns 对应数据库字段 .list 导出数据1.pox中添加依赖 <dependency> ...

  2. LINUX 中 python 版本升级

    首先下载源tar包 可利用linux自带下载工具wget下载,如下所示: 1 # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tg ...

  3. POJ——T2117 Electricity

     http://poj.org/problem?id=2117 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5459   ...

  4. 【Hibernate学习】 —— 抓取策略(注解方式)

    当应用程序须要在关联关系间进行导航的时候.hibernate怎样获取关联对象的策略. 抓取策略的方式: FetchType.LAZY:懒载入.载入一个实体时.定义懒载入的属性不会立即从数据库中载入. ...

  5. [MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change

    n this lesson, we will set up Hot Module Reloading(HMR), making it possible to load new definitions ...

  6. 兔子--Android Support v4包丢失的解决的方法

    在开发中,Android Support v4包丢失的解决的方法: Project->properties->Java Build Path->Libraries->Add E ...

  7. GridView中使用CheckBox

    asp.net中checkbox是向server提交时才干触发事件 把该控件的autopostback设置为true.才干响应事件  protected void Checkbox_CheckedCh ...

  8. 在shell脚本中使用代理

    设置所有的代理走socks5 export ALL_PROXY="socks5://127.0.0.1:1080" 取消代理 unset ALL_PROXY  

  9. 34.share_ptr智能指针共享内存,引用计数

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  10. Android--Fragment与Activity通信

    package com.example.testfragment; import com.example.testfragment.MainFargment.BackString; import an ...