poj 2104 查询区间第k小 主席树 (递归和非递归)模板
来源:互联网 发布:apache json 配置 编辑:程序博客网 时间:2024/06/03 21:02
题目链接:http://poj.org/problem?id=2104
K-th Number
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
Sample Input
7 31 5 2 6 3 7 44 4 12 5 31 7 3
Sample Output
563
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
题意:区间第k大。思路:主席树(裸模板)
以前听说过,觉得很难就不敢学,毕竟线段树我基础也不扎实,最开始看了一个视频才了解到主席树,这个视频把主席树的思想讲了出来,在看代码的时候省了不少事,https://www.bilibili.com/video/av10796614/ 对主席树建的n颗树不懂的的话最好看看。
http://blog.csdn.net/htt_h/article/details/47704209这个是我代码的来源,
非递归AC代码:(我先看着模板打了一遍,然后按自己理解又打了一遍,感觉还不算太难)
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cctype>#include <string>#include <vector>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <algorithm>using namespace std;const int maxn=1e5+10;const int M=maxn*30;int n,q,m,tot;int a[maxn], t[maxn];int T[maxn], lson[M], rson[M], c[M];void init_hash(){ for(int i=1;i<=n;i++) t[i]=a[i]; sort(t+1,t+n+1); m=unique(t+1,t+1+n)-t-1;}int hash(int x){ return lower_bound(t+1,t+1+m,x)-t;}int build(int l,int r){ int rt=tot++; c[rt]=0; if(l<r){ int mid=(l+r)>>1; lson[rt]=build(l,mid); rson[rt]=build(mid+1,r); } return rt;}int update(int rt,int pos,int val){ int newrt=tot++,tmp=newrt; c[newrt]=c[rt]+val; int l=1,r=m; while(l<r){ int mid=(l+r)>>1; if(pos<=mid){ lson[newrt]=tot++; rson[newrt]=rson[rt]; newrt=lson[newrt]; rt=lson[rt]; r=mid; } else{ rson[newrt]=tot++; lson[newrt]=lson[rt]; newrt=rson[newrt]; rt=rson[rt]; l=mid+1; } c[newrt]=c[rt]+val; } return tmp;}int query(int lrt,int rrt,int k){ int l=1,r=m; while(l<r){ int mid=(l+r)>>1; if(c[lson[lrt]]-c[lson[rrt]]>=k){ r=mid; lrt=lson[lrt]; rrt=lson[rrt]; } else{ l=mid+1; k-=c[lson[lrt]]-c[lson[rrt]]; lrt=rson[lrt]; rrt=rson[rrt]; } } return l;}int main(){ while(~scanf("%d%d",&n,&q)){ for(int i=1;i<=n;i++) scanf("%d",a+i); tot=0; init_hash(); T[n+1]=build(1,n); for(int i=n;i;i--){ int pos=hash(a[i]); T[i]=update(T[i+1],pos,1); } while(q--){ int l,r,k; scanf("%d%d%d",&l,&r,&k); printf("%d\n",t[query(T[l],T[r+1],k)]); } } return 0;}
递归AC代码:(看了俩小时终于懂了是怎么递归的)代码来源(里面有讲解): http://www.cnblogs.com/oyking/p/3230296.html
#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 100010;struct Node { int L, R, sum;//L为左区间节点的编号,R为右区间节点的编号。注意:L,R是节点编号,不是区间两端点值,};Node T[MAXN * 20];int T_cnt;void insert(int &num, int &x, int L, int R) {//x为引用,所以最后的x值就是它引用对象的值。***先复制x节点***再更改x值去回溯***!!! T[T_cnt++] = T[x]; x = T_cnt - 1; ++T[x].sum; if(L == R) return ; int mid = (L + R) >> 1; if(num <= mid) insert(num, T[x].L, L, mid); else insert(num, T[x].R, mid + 1, R);}int query(int i, int j, int k, int L, int R) { if(L == R) return L; int t = T[T[j].L].sum - T[T[i].L].sum; int mid = (R + L) >> 1; if(k <= t) return query(T[i].L, T[j].L, k, L, mid); else return query(T[i].R, T[j].R, k - t, mid + 1, R);}struct A { int x, idx; bool operator < (const A &rhs) const { return x < rhs.x; }};A a[MAXN];int rank[MAXN], root[MAXN];int n, m;int main() { T[0].L = T[0].R = T[0].sum = 0;//初始节点。 root[0] = 0; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 1; i <= n; ++i) { scanf("%d", &a[i].x); a[i].idx = i; } sort(a + 1, a + n + 1); for(int i = 1; i <= n; ++i) rank[a[i].idx] = i;//离散化 T_cnt = 1; for(int i = 1; i <= n; ++i) { root[i] = root[i - 1]; insert(rank[i], root[i], 1, n); } while(m--) { int i, j, k; scanf("%d%d%d", &i, &j, &k); printf("%d\n", a[query(root[i - 1], root[j], k, 1, n)].x); } }}
阅读全文
0 0
- poj 2104 查询区间第k小 主席树 (递归和非递归)模板
- 主席树模板 区间第k小
- poj 2104 区间第K小 主席树入门题
- 主席树查询区间第K大模板
- poj 2104 主席树(区间第k大)
- 主席树静态查找区间第k小模板(并没有杰少详细)即讲解
- HDU 2665 / POJ 2104 静态区间第k小 主席树
- 主席树模板(区间第k大!)hdu2665
- poj 2104 区间第k大 主席树
- 主席树详解 zoj 2112 (单点更新)+ poj 2104 区间第k大
- bzoj3524 [Poi2014]Couriers(主席树查询第k小)
- poj 2104 划分树(查询区间第k大数)
- 区间第k大(主席树)
- 【主席树入门 && 区间内第k小的数】POJ
- POJ 2104(K-th Number-区间第k大-主席树)
- POJ 2104(K-th Number-区间第k大-主席树)
- POJ 2104 K-th Number 主席树 静态区间第K大
- 【POJ 2104】(K-th Number-区间第k大-主席树)
- C#——面向对象——继承——密闭类
- responseText||responseXML获取的数据
- echarts下工资收入、五险一金、个人所得税走势图表
- Dockerfile中的COPY和ADD指令详解与比较
- ubuntu安装maven
- poj 2104 查询区间第k小 主席树 (递归和非递归)模板
- Dreamweaver制作ASP网页用DS…
- C#检查TCP端口号是否被占用
- c# 在光标位置插入控件
- Windows下Apache HTTP Server安装
- Apache+Tomcat+mod_jk负载均衡配置
- Apache+https
- Effective c++ 学习笔记(五)
- Visua Assist X V10.9.2074.0 官方原版