题意

给出n个数字组成的数字序列,有m组询问。每次询问包含三个数字l,r,k。对于每个询问输出序列区间[l,r]中第k大的数字。

分析

这是主席树的模板题,套板子就可以

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=+;
struct Value{
int x;
int id;
bool operator <(const Value &rhs)const{
return x<rhs.x;
}
}value[maxn];
int n,m,num,cnt;
int lc[*maxn],rc[*maxn],sumv[*maxn];
int a[maxn],root[maxn],Rank[maxn];
void update(int num,int old,int &o,int L,int R){
o=++cnt;
lc[o]=lc[old],rc[o]=rc[old],sumv[o]=sumv[old];
sumv[o]++;
if(L==R)return ;
int M=L+(R-L)/;
if(num<=M)update(num,lc[o],lc[o],L,M);
if(num>M)update(num,rc[o],rc[o],M+,R);
}
int query(int i,int j,int k,int L,int R){
int lsum=sumv[lc[j]]-sumv[lc[i]];
if(L==R)return L;
int M=L+(R-L)/;
if(lsum>=k)return query(lc[i],lc[j],k,L,M);
else return query(rc[i],rc[j],k-lsum,M+,R);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
value[i].x=a[i];
value[i].id=i;
}
sort(value+,value++n);
for(int i=;i<=n;i++){
Rank[value[i].id]=i;
}
num=,cnt=;
for(int i=;i<=n;i++){
update(Rank[i],root[i-],root[i],,n);
}
int l,r,k;
for(int i=;i<=m;i++){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",value[query(root[l-],root[r],k,,n)].x);
}
return ;
}

poj2104 主席树模板题的更多相关文章

  1. 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Ca ...

  2. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  3. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  4. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  5. POJ 2104 K-th Number(主席树模板题)

    http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...

  6. hdu2665(主席树模板题)

    hdu2665 题意 求区间第 k 小. 分析 参考 这类题目做法挺多的,例如 划分树. 这里使用主席树再写一发,不得不说主席树相比而言要好写的多,比起普通线段树,主席树就是复用了线段树共有的信息. ...

  7. poj2104 主席树裸题

    空间大小:n*lgn 复杂度:建树n*lgn  查询lgn #include <cstdio> #include <iostream> #include <algorit ...

  8. 主席树 模板题 luogu([POI2014]KUR-Couriers)

    求区间内是否有个数大于二分之一的数,有的话输出这个数,没有的话输出0. 在询问的时候,如果左边有sum大于这个limit,就可以继续求,如果右边有sum大于limit  也递归, 如果都不行,返回 0 ...

  9. POJ 2104 主席树模板题

    #include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...

随机推荐

  1. c++ 霍夫变换检测直线

    通常这是一幅边缘图像,比如来自 Canny算子.cv:: Houghlines函数的输出是cV::Vec2f向量,每个元素都是一对代表检测到的直线的浮点数(p,0).在下例中我们首先应用 Canny算 ...

  2. 转载:将STM32的标准库编译成lib使用【图文】

    from:http://www.cnblogs.com/zyqgold/p/3189719.html 百度上边也有不少关于lib文件的文章,恰巧看到该博文,感觉该博文的条理清晰,步骤明确,故复制到这个 ...

  3. python之pandas数据筛选和csv操作

    本博主要总结DaraFrame数据筛选方法(loc,iloc,ix,at,iat),并以操作csv文件为例进行说明 1. 数据筛选 a b c (1)单条件筛选 df[df[] # 如果想筛选a列的取 ...

  4. 使用Audition录制自己的歌曲

    Audition专为在照相室.广播设备和后期制作设备方面工作的音频和视频专业人员设计,可提供先进的音频混合.编辑.控制和效果处理功能.最多混合 128 个声道,可编辑单个音频文件,创建回路并可使用 4 ...

  5. 【转】C#中的线程 入门

    Keywords:C# 线程 Source:http://www.albahari.com/threading/ Author: Joe Albahari Translator: Swanky Wu ...

  6. ansible playbook 使用

    ansible playbook 格式是json  yaml   1. 执行方法 ansible-playbook deply.yml 2. playbook 格式 关键字 hosts 主机ip 主机 ...

  7. 谷歌设置支持webgl

    浏览器报错: could not initialize WebGl 因为谷歌默认不支持WebGl 在浏览器器中输入 about:flags 然后开启:覆盖软件渲染列表,覆盖内置的软件渲染列表,并对不支 ...

  8. C# 判断程序是否已在运行

    方法一: Process[] processes = rocess.GetProcessesByName("ConDemo"); ) { MessageBox.Show(" ...

  9. [三卷天书]ajax请求时接收到乱码的处理方案

    $.ajax({ url: "getmore.ashx", type: "post", dataType: "text", data: { ...

  10. Does Windows have a limit of 2000 threads per process?

    http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx Often I see people asking why the ...