主席树。。板子HDU2665
来源:互联网 发布:ubuntu 强制删除用户 编辑:程序博客网 时间:2024/06/11 23:01
//主席树真是。。。。。。简称函数式线段树。。。可以用来代替划分树。。。
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=100000+10;int a[maxn],b[maxn];int sum[maxn<<5],lson[maxn<<5],rson[maxn<<5];int root[maxn];int tot;void pushdown(int x){ sum[x]=sum[lson[x]]+sum[rson[x]];}int build(int l,int r){ int now=++tot; if(l==r) { sum[now]=0; lson[now]=0; rson[now]=0; return now; } int mid=(l+r)>>1; lson[now]=build(l,mid); rson[now]=build(mid+1,r); pushdown(now); return now;}int update(int rt,int pos,int l,int r){ int now=++tot; if(l==r) { sum[now]=sum[rt]+1; lson[now]=rson[now]=0; return now; } int m=(l+r)>>1; if(pos<=m) lson[now]=update(lson[rt],pos,l,m),rson[now]=rson[rt]; else rson[now]=update(rson[rt],pos,m+1,r),lson[now]=lson[rt]; pushdown(now); return now;}int ask(int rt1,int rt2,int k,int l,int r){ if(l==r) return b[l]; int num=sum[lson[rt2]]-sum[lson[rt1]]; int m=(l+r)>>1; if(num>=k) return ask(lson[rt1],lson[rt2],k,l,m); else return ask(rson[rt1],rson[rt2],k-num,m+1,r);}int main(){ int t; scanf("%d",&t); while(t--) { int n,m; tot=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+n+1); int d=unique(b+1,b+n+1)-b-1; root[0]=build(1,d); for(int i=1;i<=n;i++) { int pos=lower_bound(b+1,b+d+1,a[i])-b; root[i]=update(root[i-1],pos,1,d); } for(int i=1;i<=m;i++) { int l,r,k; scanf("%d%d%d",&l,&r,&k); printf("%d\n",ask(root[l-1],root[r],k,1,d)); } } return 0;}
0 0
- 主席树。。板子HDU2665
- hdu2665 主席树
- HDU2665 主席树入门
- 主席树(hdu2665)
- 主席树 hdu2665 Kth number
- hdu2665 主席树模板题
- HDU2665 Kth number(主席树入门)
- HDU2665-主席树&模板-Kth number
- hdu2665 Kth number(主席树模板)
- HDU2665 POJ2104 K-th Number(主席树)
- POJ2104 hdu2665 主席树入门 Kth-number
- (主席树)hdu2665 Kth number
- poj 2104 主席树 板子
- 主席树水题 HDU2665
- hdu2665主席树(可持久化线段树)
- 主席树模板(区间第k大!)hdu2665
- hdu2665区间第K大+主席树解题报告
- [POJ2104/HDU2665]Kth Number-主席树-可持久化线段树
- 自定义---DialogCellEditor 一个完整的实例 3
- win7系统如何调整网页字体太小?调整网页字体的方法
- Linux命令小技巧
- [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第十二章 类别
- C语言函数参数传递之痛
- 主席树。。板子HDU2665
- 字符串旋转-回文字符串
- 加快网站访问速度
- Git右键菜单去除办法
- [LeetCode]Recover Binary Search Tree
- NOI2015酱油记
- 【Leetcode】Remove Duplicates from Sorted Array
- 硬盘不足挂载新的硬盘
- java反射详解