標籤:sort 第k小 裸題 更新 numbers 多少 mit accept val
K-th Number
| Time Limit: 20000MS |
|
Memory Limit: 65536K |
| Total Submissions: 51732 |
|
Accepted: 17722 |
| 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 31 5 2 6 3 7 42 5 34 4 11 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.
Source
Northeastern Europe 2004, Northern Subregion 原題大意:給一組數。對於每個查詢區間,找出第k小的數。解題思路:劃分樹或主席樹的裸題,POJ還有一個類似的題目 POJ 2761. 這兩道題的代碼一樣都可以過。 所謂劃分樹,類似於在已經知道區間中位元的情況下的快排,比中位元小的都放左邊,大的分右邊,相等的看看左邊有沒有空,有空放左邊,沒空放右邊,相對位置不變,並記錄每次的左右子樹劃分情況。 如果所求第k小的樹,每次我們看看有多少數在左邊,就可以知道該查左區間還是右區間。對查詢區間做了處理後遞迴下去即可得出唯一的數值。
#include<stdio.h>#include<algorithm>using namespace std;int tree[20][110001],sorted[110001],left[20][110001];void build(int dep,int l,int r) { int mid=(l+r)>>1,i,midnum=mid-l+1,sonl=l,sonr=mid+1; for(i=l;i<=r;++i) if(sorted[i]<sorted[mid]) --midnum; //尋找能放入左子樹中值的個數 for(i=l;i<=r;++i) { if(i==l) left[dep][i]=0; else left[dep][i]=left[dep][i-1]; //對不同的區間,進行左 //樹個數初始化 if(tree[dep][i]==sorted[mid]) { if(midnum) { --midnum; ++left[dep][i]; tree[dep+1][sonl++]=tree[dep][i]; //統計進入左子樹的個數,並更新//下一層樹 } else tree[dep+1][sonr++]=tree[dep][i]; } else if(tree[dep][i]<sorted[mid]) //比中間值小,入左子樹 { ++left[dep][i]; tree[dep+1][sonl++]=tree[dep][i]; } else tree[dep+1][sonr++]=tree[dep][i]; //比中間值小,入右子樹 } if(l==r) return; //如果是葉子結點,返回 build(dep+1,l,mid); //遞迴左右子樹 build(dep+1,mid+1,r); }int query(int dep,int l,int r,int ql,int qr,int k) { int l_ql,ql_qr,mid=(l+r)>>1; if(l==r) return tree[dep][l]; //如果找到值,返回 if(l==ql) //恰好是所求左區間為遞迴左區間 { // l_ql=0; ql_qr=left[dep][qr]; } else { l_ql=left[dep][ql-1]; //l到ql-1的入左區間數 ql_qr=left[dep][qr]-l_ql; //ql到qr的左區間數 } if(k<=ql_qr) return query(dep+1,l,mid,l+l_ql,l+l_ql+ql_qr-1,k); else //遞迴下一區間 return query(dep+1,mid+1,r,mid+1+ql-l_ql-l,mid+1+qr-ql_qr-l_ql-l,k-ql_qr); //右區間有點亂,ql-l-l_ql即l到ql-1中入右區間的個數依次類推 }int main() { int n,m,i,ql,qr,qk; scanf("%d%d",&n,&m); for(i=1;i<=n;++i) { scanf("%d",&tree[0][i]); sorted[i]=tree[0][i]; }sort(sorted+1,sorted+n+1);build(0,1,n);while(m--) { scanf("%d%d%d",&ql,&qr,&qk); printf("%d\n",query(0,1,n,ql,qr,qk)); } return 0; }
[劃分樹] POJ 2104 K-th Number