k-th number
Time Limit: 20000MS |
|
Memory Limit: 65536K |
Total Submissions: 40920 |
|
Accepted: 13367 |
Case Time Limit: 2000MS |
Description
You is working for Macrohard company in data Structures Department. After failing your previous task on key insertion you were asked to write a new data structure that would being able to re Turn quickly k-th order statistics in the array segment.
That's, given an array A[1...N] of different integer numbers, your program must answer a series of questions Q (I, J, K) I n the form: "What would is the k-th number in A[I...J] segment, if this segment is sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question is 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 was 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 <=, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values---the array for which th E answers should be given.
The following m lines contain question descriptions, each description consists of three numbers:i, J, and K (1 <= i &L T;= j <= N, 1 <= k <= j-i + 1) and represents the question Q (I, J, K).
Output
For each question output of 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 have huge input,so please use C-style input (scanf,printf), or your may got time limit exceed.
algorithm and topic Analysis: give you an array with n numbers, do m this query. Every time you ask, you will be given a sub-range, asking you what is the K-large number in this interval? For example: Array sequence (subscript starting from 1) is: 1 5 2 6 3 7 4, ask: [2, 5] the 3rd largest number. [2, 5] The number of intervals is: 5 2 6 3, after sorting: 2 3 5 6, the 3rd-largest number should be 5. But when the amount of data is large, when the number of queries is more, this conventional algorithm realizes that the star efficiency is lower. So a tree-type structure algorithm came into being: "Partition Tree Algorithm" (based on the idea of line-segment tree) (Partition tree Implementation details to be supplemented). "can refer to: Programming problem solving Strategy FAI Wang Jiande P2"
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <string> #include <iostream> #include <algorithm> #define N 100000+10using namespace Std;//poj 2104int Tree[50][n];//tree[p][i] represents the value of position I in the P-layer int sorted[n];int toleft[50][n];//toleft[p][i] Represents the P-layer from 1 to I have//number of numbers are divided into the next layer of the left sub-range//partition tree achievement of the implementation process void build (int ll, int r, int dep) {if (ll==r) return;//recursive exit if divided into leaves, backtracking int mid= (LL+R) >>1; Intermediate pointer int same=mid-ll+1 equal to (l+r)/2 calculation interval; Calculation [L,r] is divided into the lower left interval of the number for (int i=ll; i<=r; i++) {if (Tree[dep][i] < sorted[mid]) same--; } int llpos=ll; int rpos=mid+1; for (int i=ll; i<=r; i++) {if (Tree[dep][i] < Sorted[mid]) tree[dep+1][llpos++]=tree[dep][i]; else if (Tree[dep][i]==sorted[mid] && same>0) {tree[dep+1][llpos++]=tree[dep][i]; same--; } else Tree[dep+1][rpos++]=treE[dep][i]; TOLEFT[DEP][I]=TOLEFT[DEP][LL-1] +llpos-ll; } build (ll, Mid, dep+1);//recursion calculates the next layer of the left sub-interval build (mid+1, R, dep+1);//recursively computes the right sub-interval of the next layer}int query (int L, int r, int ll, int r, int dep, int k)//starting from the DEP layer of the division number top-down in the large interval [l,r] Query sub-interval//[ll,r], the number of K {if (ll==r) return TREE[DEP][LL]; int mid= (L+R) >>1; int CNT=TOLEFT[DEP][R]-TOLEFT[DEP][LL-1]; if (CNT >= k) {int newll=l+toleft[dep][ll-1]-toleft[dep][l-1]; int newr = newll+cnt-1; return query (L, Mid, Newll, NEWR, dep+1, K); } else {int newr=r+toleft[dep][r]-toleft[dep][r]; int newll=newr-(R-LL-CNT); return query (mid+1, R, Newll, NEWR, dep+1, k-cnt); }}int Main () {int n, m; int i; while (~SCANF ("%d%d", &n, &m)) {for (i=1; i<=n; i++)//n {scanf ("%d", &tree[0][ I]);//sorted[i]=tree[0][i]; } sort (sorted+1,sorted+n+1); Build (1, n, 0);//Build a partition tree while (m--) { int U, V, W; scanf ("%d%d%d", &u, &v, &w); printf ("%d\n", query (1, N, u, V, 0, W)); }} return 0;}
POJ2104 k-th Number (K-large numbers in the sub-range) "Tree-based algorithm template application"