例题:poj2104 http://poj.org/problem?id=2104

讲解,推荐博客:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html

http://seter.is-programmer.com/posts/31907.html

http://www.cnblogs.com/shenben/p/5598371.html

数组版:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int l_child[N*],r_child[N*],sum[N*];
int n,m,a[N],hash[N],cnt,root[N];
int x,y,k;
void discrete()
{
sort(hash+,hash+n+);
cnt=unique(hash+,hash+n+)-(hash+);
for(int i=;i<=n;i++) a[i]=lower_bound(hash+,hash+cnt+,a[i])-hash;
}
int init()
{
int x=,f=;char c=getchar();
while(c<''||c>'') {if(c=='-') f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int query(int x,int y,int l,int r,int k)
{
if(l==r) return l;
int mid=l+r>>,tmp=sum[l_child[y]]-sum[l_child[x]];
if(tmp>=k) return query(l_child[x],l_child[y],l,mid,k);
else return query(r_child[x],r_child[y],mid+,r,k-tmp);
}
int tot=;
void build(int x,int &y,int l,int r,int v)
{
sum[y=++tot]=sum[x]+;
if(l==r) return;
int mid=l+r>>;
if(v<=mid)
{
r_child[y]=r_child[x];
build(l_child[x],l_child[y],l,mid,v);
}
else
{
l_child[y]=l_child[x];
build(r_child[x],r_child[y],mid+,r,v);
}
}
int main()
{
n=init();m=init();
for(int i=;i<=n;i++) a[i]=init(),hash[i]=a[i];
discrete();
//for(int i=1;i<=n;i++) printf("%d ",a[i]);
for(int i=;i<=n;i++) build(root[i-],root[i],,cnt,a[i]);
for(int i=;i<=m;i++)
{
x=init();y=init();k=init();
printf("%d\n",hash[query(root[x-],root[y],,cnt,k)]);
}
}

指针版:TLE

#include<cstdio>
#include<algorithm>
#define N 100001
using namespace std;
int n,m,a[N],hash[N];
int tot,cnt;
struct node
{
node * l,* r;
int sum;
};
node * root[N];
void discrete()
{
sort(a+,a+n+);
tot=unique(a+,a+n+)-(a+);
for(int i=;i<=n;i++) hash[i]=lower_bound(a+,a+n+,hash[i])-a;
}
inline node * build(node * pre,int l,int r,int w)
{
node *neww=new node();
//node * neww=(node *)malloc(sizeof(node));
neww->sum=pre->sum+;
if(l==r) return neww;
int mid=l+r>>;
if(w<=mid)
{
neww->r=pre->r;
neww->l=build(pre->l,l,mid,w);
}
else
{
neww->l=pre->l;
neww->r=build(pre->r,mid+,r,w);
}
return neww;
}
inline int query(node * x,node * y,int k,int l,int r)
{
if(l==r) return l;
int mid=l+r>>,tmp=y->l->sum-x->l->sum;
if(k<=tmp) return query(x->l,y->l,k,l,mid);
else return query(x->r,y->r,k-tmp,mid+,r);
}
node * null(int ll,int rr)
{
node *neww=new node();
//node * neww=(node *)malloc(sizeof(node));
neww->l=neww->r=NULL;
neww->sum=;
if(ll==rr) return neww;
int mid=ll+rr>>;
neww->l=null(ll,mid);
neww->r=null(mid+,rr);
return neww;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {scanf("%d",&a[i]);hash[i]=a[i];}
discrete();
root[]=null(,n);
for(int i=;i<=n;i++) root[i]=build(root[i-],,n,hash[i]);
int x,y,k;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",a[query(root[x-],root[y],k,,n)]);
}
}

主席树——求静态区间第k大的更多相关文章

  1. 主席树(静态区间第k大)

    前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...

  2. 可持久化线段树(主席树)——静态区间第k大

    主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...

  3. 主席树 - 查询某区间第 K 大

    You are working for Macrohard company in data structures department. After failing your previous tas ...

  4. 主席树入门——询问区间第k大pos2104,询问区间<=k的元素个数hdu4417

    poj2104找了个板子..,但是各种IO还可以进行优化 /* 找区间[l,r]第k大的数 */ #include<iostream> #include<cstring> #i ...

  5. POJ 2104 【主席树】【区间第K大】

    #include<stdio.h> #include<algorithm> #include<string.h> #define MAXN 100010 #defi ...

  6. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  7. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

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

  8. HDU3473--Minimum Sum(静态区间第k大)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. 主席树学习笔记(静态区间第k大)

    题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出 ...

随机推荐

  1. YunCart电商网站支付宝接口出现500错误

    yuncart 是一套易与集成的php开源商城系统,方便多人同时经行二次开发,Yuncart 可以以非常方便的方式切换到sql server,oracle等数据库,大小1.9MB,感兴趣的朋友,可以去 ...

  2. 如何修复VUM在客户端启用之后报数据库连接失败的问题

    在上一篇随笔中介绍了关于重新注册VMware Update Manager(VUM)至vCenter Server中的方法,最近有朋友反应,原本切换过去好好的更新服务为什么某次使用一下就不灵了? 当时 ...

  3. linux mount/umount挂载命令解析。

    如果想在运行的Linux下访问其它文件系统中的资源的话,就要用mount命令来实现. 2.      mount的基本用法是?格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有: ...

  4. 【分布式】Zookeeper使用--命令行

    一.前言 在学习了Zookeeper相关的理论知识后,下面接着学习对Zookeeper的相关操作. 二.Zookeeper部署 Zookeeper的部署相对来说还是比较简单,读者可以在网上找到相应的教 ...

  5. CSS 基础篇、绝对有你想要

    本章内容: 简介 CSS 定义 四种引入方式 样式应用的顺序 选择器(Selector) * 通用元素选择器 标签选择器 class 类选择器 # ID选择器 , 多元素选择器 后代元素选择器 > ...

  6. 关于css清除浮动,解决内容溢出的问题

    以前在布局的时候总会遇到这样的问题,比如我想让整体的内容居中,所以会这样写, .main-content{ width:960px:height:300px;margin:0px auto; } 然后 ...

  7. 学习javascript数据结构(三)——集合

    前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...

  8. C#测试题

    阅读下面的程序,程序运行后hovertree值为( ) int x = 3, y = 4, z = 5;String s = "xyz";string hovertree = s ...

  9. Gate Of Babylon bzoj 1272

    Gate Of Babylon (1s 128MB) babylon [问题描述] [输入格式] [输出格式] [样例输入] 2 1 10 13 3 [样例输出] 12 [样例说明] [数据范围] 题 ...

  10. Python 历遍目录

    Automate the Boring Stuff 学习笔记 01 使用 os 模块的 walk() 函数可以实现历遍目录的操作,该函数接收一个绝对路径字符串作为必选参数,返回三个参数: 当前目录—— ...