主席树经典题目 区间k大值 poj2104
来源:互联网 发布:环评做题软件 编辑:程序博客网 时间:2024/05/01 11:31
年前就开始写的一个题。。。一直没调。。
中间贡献无数wa原因是忘了函数式的数据结构要开30倍空间。。。。
至今对主席树的理解还是不算很深
主席树大概就是对每个前缀或者后缀都单独的维护一颗线段树,然后由于主席树的可加减性,可以减出来区间的情况
用丽杰姐姐的话来说就是划分树已经成为时代的眼泪。。。。
后来稍微懂了。。。
大概就是首先闹一个 rank
这样我们就知道每个数在现在区间的排名了,相当于是一个离散的过程,当然有重复就麻烦了
然后我们每个节点保存的 就是一个数字而已,大概意思是这段范围内一共出现了几次
但是我们并不保留具体的值
因为主席树是可以减的,所以一直减,一直到叶节点,就显而易见了。。。
这是主席树的精髓。。
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#define MAX 100010using namespace std;int t[MAX*30];int n,c[MAX],a[MAX],child[MAX*30][2],size,tree[MAX*30];int tot=0,m;bool cmp(int a1,int a2){return a1<a2;}void before(){for(int i=1;i<=n;i++)c[i]=a[i];sort(c+1,c+1+n,cmp);size=unique(c+1,c+1+n)-(c+1);}int build(int la,int ra){int root=tot++;tree[root]=0;if(la!=ra){int mid=(la+ra)/2; child[root][0]=build(la,mid); child[root][1]=build(mid+1,ra);}return root;}int find(int x){return lower_bound(c+1,c+1+size,x)-c;}int update(int root,int pos,int value){int newnode=tot++,tmp=newnode;tree[newnode]=tree[root]+value;int l=1,r=size;while(l<r){int mid=(l+r)>>1;if(pos<=mid){child[newnode][0]=tot++;child[newnode][1]=child[root][1];newnode=child[newnode][0];root=child[root][0];r=mid;}else{child[newnode][1]=tot++;child[newnode][0]=child[root][0];newnode=child[newnode][1];root=child[root][1];l=mid+1;}tree[newnode]=tree[root]+value;}return tmp;}int ask(int l,int r,int k){int L=1,R=size;while(L<R){int mid=(L+R)>>1;if(tree[child[l][0]]-tree[child[r][0]]>=k){R=mid;l=child[l][0];r=child[r][0];}else{L=mid+1;k-=tree[child[l][0]]-tree[child[r][0]];l=child[l][1];r=child[r][1];}}return L;}int main(){while(scanf("%d%d",&n,&m)==2){for(int i=1;i<=n;i++)scanf("%d",&a[i]);before();tot=0;t[n+1]=build(1,size);for(int i=n;i>=1;i--){int pos=find(a[i]);t[i]=update(t[i+1],pos,1);}while(m--){int l,r,k;scanf("%d %d %d",&l,&r,&k);printf("%d\n",c[ask(t[l],t[r+1],k)]);}}return 0;}
1 0
- 主席树经典题目 区间k大值 poj2104
- poj2104区间K大 主席树
- poj2104 主席树区间第k大
- poj2104 K-th Number(静态区间k大,主席树)
- poj2761&&poj2104 主席树(静态区间第K大)
- POJ2104 POJ2761 区间第K大 主席树
- POJ2104-K-th Number-区间第k大-可持久化线段树/主席树
- 可持久化线段树(主席树)(图文并茂详解)【poj2104】【区间第k大】
- 【poj2104】不带修改的区间第k大 主席树
- poj2104 区间第K大
- 主席树---求第k大的数 poj2104
- [POJ2104] K-th Number(区间K小数,主席树模板)
- hdu 2665(主席树查询区间k大值)
- 主席树 --- 求区间第k大值
- 区间第k大值(主席树入门)
- 主席树解决区间第k大
- 区间第k大(主席树)
- poj2104 (线段树求区间第k大)
- [ICM ] 2014全美交叉学科竞赛原题及翻译
- HTC谷歌再续前缘:将生产新款Nexus平板
- 设计模式(二)之装饰模式(Decorator Pattern)
- SPT的全称为sql pass through
- yii DAO层
- 主席树经典题目 区间k大值 poj2104
- Java--MyCalculator -- --
- 发一个支持任意地点hook的类(包含驱动hook和应用层hook)
- 链表的 基本应用
- java 反编译资料
- ios7中在一个viewController中显示隐藏状态栏
- warning C4819: 该文件包含不能在当前代码页(936)中表示的字符
- 制造业车间作业计划与调度研究
- The CB Passive Income