划分树(模板)poj2104
2014-08-07 21:45
411 查看
Language:
Default
K-th Number
Time Limit: 20000MS
Memory Limit: 65536K
Total Submissions: 37013
Accepted: 11911
Case Time Limit: 2000MS
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
Sample Output
裸地划分树
Default
K-th Number
Time Limit: 20000MS
Memory Limit: 65536K
Total Submissions: 37013
Accepted: 11911
Case Time Limit: 2000MS
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 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
裸地划分树
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=100010; int n,m; int a[maxn]; struct P_TREE { int n,order[maxn]; int val[20][maxn],num[20][maxn]; void init(int len) { n=len; for(int i=1;i<=n;i++)order[i]=a[i]; for(int i=0;i<20;i++){val[i][0]=num[i][0]=0;} for(int i=1;i<=n;i++)val[0][i]=order[i]; sort(order+1,order+1+n); build(0,1,n); } void build(int ind,int l,int r) { if(l==r)return ; int mid=(l+r)>>1; int ln=l,rn=mid+1,same=r-l+1; for(int i=l;i<=r;i++) if(val[ind][i]<order[mid])same--; for(int i=l;i<=r;i++) { int flag=0; if(val[ind][i]<order[mid]||(val[ind][i]==order[mid]&&same)) { flag=1; val[ind+1][ln++]=val[ind][i]; if(val[ind][i]==order[mid])same--; } else val[ind+1][rn++]=val[ind][i]; num[ind][i]=num[ind][i-1]+flag; } build(ind+1,l,mid); build(ind+1,mid+1,r); } int query(int ind,int l,int r,int q1,int q2,int k) { if(k==0)return -1; if(q2-q1+1<k)return (1<<30); if(l==r)return val[ind][l]; int mid=(l+r)>>1; int lx=num[ind][q1-1]-num[ind][l-1]; int ly=num[ind][q2]-num[ind][q1-1]; int rx=q1-1-l+1-lx; int ry=q2-q1+1-ly; if(ly>=k)return query(ind+1,l,mid,l+lx,l+lx+ly-1,k); else { int st=mid+1+rx; int en=mid+1+rx+ry-1; return query(ind+1,mid+1,r,st,en,k-ly); } } }tree; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); tree.init(n); while(m--) { int x,y,k; scanf("%d%d%d",&x,&y,&k); int ans=tree.query(0,1,n,x,y,k); printf("%d\n",ans); } } return 0; }
相关文章推荐
- poj 2104 (划分树模板)
- POJ2104 K-th Number (划分树模板)
- poj 2104 and hdu 2665 划分树模板入门题
- poj 2104 划分树模板
- POJ2104 K-th Number (子区间内第k大的数字)【划分树算法模板应用】
- POJ 2104/TYVJ 1253 划分树模板
- POJ 2104 划分树模板题
- poj 2104 K-th Number (划分树模板题)
- poj 2104 K-th Number (主席树模板)
- 主席树模板poj 2104
- poj 2104 K-th Number - 经典划分树
- poj 2104 or poj2761 or hdu2665 划分树
- hdu 4417,poj 2104 划分树(模版)归并树(模版)
- poj2104 K大数 划分树
- POJ 2104 K-th Number (划分树)
- 区间第K大数——划分树(POJ2104解题报告)
- POJ 2104 K-th Number 归并树与划分树
- POJ 2104 | HDU 2665 - K-th Number (划分树)
- poj 2104 K-th Number(划分树)
- [POJ 2104]K-th Number【模板】(主席树)