Number of k in 51nod p1175 interval

Source: Internet
Author: User

1175 the number of k in the interval limit: 1 seconds space limit: 131072 KB score: 160 Difficulty: 6-level algorithm problem a sequence of integers of length N, numbering 0-n-1. For the Q-query, the number of the number I to J in all the number, K-size is how much. For example: 1 7 6 3 1. i = 1, j = 3,k = 2, the corresponding number is 7 6 3, the 2nd-largest number is 6. Input
Line 1th: 1 number N, indicating the length of the sequence. (2 <= N <= 50000) 2-n + 1 lines: 1 numbers per line, corresponding to the elements in the sequence. (0 <= S[i] <= 10^9) nth + 2 line: 1 number Q, indicating the number of queries. (2 <= Q <= 50000) nth + 3-n + q + 2 lines: 3 digits per line, starting number I and ending number J for the corresponding query, and K. (0 <= I <= J <= n-1,1 <= k <= j-i + 1)
Output
A total of Q lines, corresponding to the number of k in each query interval.
Input example
51763130 1 11 3 23 4 2
Output example
761

Section K, chairman of the tree write a pitch, using Java written, by the way review
Spit Groove---Java is really slow, must be discretized, C + + casually write all over
And there is no sort, no map, only hashmap, really sucks ....
1  Packagep1175;2 3 ImportJava.io.*;4 ImportJava.util.*;5 6 ImportJavax.management.Query;7 8  Public classMain9 {Ten      Public Static classNode One     { A         intsum; - Node LC, RC; -          Publicnode (node T) the         { -             if(T = =NULL) -             { -LC = RC =NULL; +sum = 0; -             } +             Else A             { atLC =t.lc; -rc =t.rc; -sum =t.sum; -             } -         } -     } in      -     Static Final intN = 50010; to      +      Public Static voidMain (string[] args)throwsIOException -     { the         //TODO auto-generated Method Stub *BufferedReader reader =NewBufferedReader (NewInputStreamReader (system.in)); $BufferedWriter writer =NewBufferedWriter (NewOutputStreamWriter (System.out));Panax Notoginseng         intn =getInt (reader); -         int[] arr =New int[n], store =New int[n]; the          for(inti = 0; I < n; i++) Store[i] = arr[i] =getInt (reader); +MergeSort (store, 0, n-1); A         intLen = 1; the          for(inti = 1; I < n; i++) +             if(Store[i]! = Store[i-1]) store[len++] =Store[i]; -Hashmap<integer, integer> MyMap =NewHashmap<integer, integer>(); $          for(inti = 0; i < Len; i++) $ Mymap.put (Store[i], i); -          for(inti = 0; I < n; i++) -Arr[i] =Mymap.get (Arr[i]); the          -         Wuyinode[] Root =NewNode[n + 1]; theNode last =NULL; -          for(inti = 0; I < n; i++) Wu         { -Root[i] =NewNode (last); AboutBuildtree (Root[i], Arr[i], 0, Len); $Last =Root[i]; -         } -          -          for(intm = getInt (reader); M > 0; m--) A         { +             intL = getInt (reader), r = getInt (reader), k =getInt (reader); the             intAns = querykth (L > 0? root[l-1]:NULL, Root[r], K, 0, Len); -Writer.write (Store[ans] + "\ r \ n"); $ Writer.flush (); the         } the     } the      the      Public Static intquerysum (Node t) -     { in         if(T = =NULL)return0; the         returnt.sum; the     } About      the      Public Static intQUERYKTH (node L, node R,intKthintLeftintRight ) the     { the         if(left = right)returnLeft ; +         intMid = (left + right) >> 1, ret = 1; -         intlcnt = L = =NULL? 0:querysum (l.rc), rcnt = r = =NULL? 0: Querysum (r.rc); the         if(rcnt-lcnt >= kth) ret = querykth (L = =NULL?NULL: l.rc, r = =NULL?NULL: R.rc, KTH, Mid + 1, right);Bayi         Elseret = querykth (L = =NULL?NULL: l.lc, r = =NULL?NULL: R.LC, KTH-(RCNT-lcnt), left, mid); the         returnret; the     } -      -      Public Static voidBuildtree (Node x,intValintLeftintRight ) the     { the         if(Left <Right ) the         { the             intMid = (left + right) >> 1; -             if(Val <=mid) the             { theX.LC =NewNode (X.LC); the Buildtree (X.LC, Val, left, mid);94             } the             Else the             { theX.rc =NewNode (x.rc);98Buildtree (X.rc, Val, mid + 1, right); About             } -         }101x.sum++;102     }103     104     Static int[] tmp =New int[N]; the      Public Static voidMergeSort (int[] arr,intLintR)106     {107         if(L >= R)return;108         intMid = (L + r) >> 1;109 mergesort (arr, L, mid); theMergeSort (arr, mid + 1, R);111         intItL = L, ItR = mid + 1, now =l; the          while(ItL <= mid && ItR <=R)113         { the             if(Arr[itl] < ARR[ITR]) tmp[now++] = arr[itl++]; the             Elsetmp[now++] = arr[itr++]; the         }117          while(ItL <= mid) tmp[now++] = arr[itl++];118          while(ItR <= r) tmp[now++] = arr[itr++];119          for(inti = l; I <= R; i++) Arr[i] =Tmp[i]; -     }121     122      Public Static intGetInt (BufferedReader Reader)throwsIOException123     {124         CharCH = "; the          for( ; ! (Ch >= ' 0 ' && ch <= ' 9 '); CH = (Char) Reader.read ());126         intRET = 0;127          for(; Ch >= ' 0 ' && ch <= ' 9 '; ch = (Char) Reader.read ()) -RET = RET * + CH-' 0 ';129         returnret; the     }131  the}
View Code

Number of k in 51nod p1175 interval

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.