HDU 2665.kth number-K smaller in no modification interval-persistence line segment tree (Chair tree) template

Source: Internet
Author: User

 

 

Kth number

Time Limit: 15000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 16941 accepted submission (s): 5190


Problem descriptiongive you a sequence and ask you the kth big number of a inteval.

 

Inputthe first line is the number of the test cases.
For each test case, the first line contain two integer N and M (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains N integers, describe the sequence.
Each of following M lines contains three integers S, T, K.
[S, T] indicates the interval and K indicates the kth big number in interval [S, T]

 

Outputfor each test case, output M lines. Each line contains the kth big number.

 

Sample input1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2

 

Sample output2

 

Sourcehdu boys' open session-the question from whu is that the query interval is K small, and the line segment tree can be persisted. Before learning the data structure that can be persisted, I felt that this was a very big and amazing thing. After learning about it, I found that the problem was caused, So I updated it on the basis of the original line segment tree, supports querying historical versions. If you create a whole line segment tree every time you update it, it will be crazy, so you can simply open a new node and save the updated chain. This kind of thing still needs to be realized by yourself, come on, salted fish. Code:
1 // the k-th smaller part of the no-modification interval-persistent line segment tree (weight line segment tree + persistent) 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <algorithm> 6 # include <bitset> 7 # include <cassert> 8 # include <cctype> 9 # include <cmath> 10 # include <cstdlib> 11 # include <ctime> 12 # include <deque> 13 # include <iomanip> 14 # include <list> 15 # include <map> 16 # include <queue> 17 # include <set> 18 # include <stack> 19 # include <vector> 20 using namespace s TD; 21 typedef long ll; 22 typedef pair <int, int> PII; 23 24 const double Pi = ACOs (-1.0); 25 const double EPS = 1e-6; 26 const ll mod = 1e9 + 7; 27 const int INF = 0x3f3f3f; 28 const int maxn = 1e5 + 10; 29 const int maxm = 100 + 10; 30 # define IOS IOs:: sync_with_stdio (false); cin. tie (0); cout. tie (0); 31 # define lson L, M 32 # define rson m + 1, R 33 34 int A [maxn], B [maxn], sum [maxn <5], L [maxn <5], R [maxn <5]; // sum Line Segment Values saved in the tree: l left son, r right son 35 int n, m, SZ = 0; 36 37 void build (Int & RT, int L, int R) // create an empty tree 38 {39 RT = ++ SZ; sum [RT] = 0; // dynamic start point, initial value 0, empty tree 40 if (L = r) {41 return; 42} 43 44 int M = (L + r)> 1; 45 build (L [RT], lson); 46 build (R [RT], rson); 47} 48 49 void Update (int pr, Int & RT, int L, int R, int X) 50 {51 RT = ++ SZ; sum [RT] = sum [pr] + 1; // insert sequence, inherit the previous line segment tree, and then directly use single point + 1 to perform 52 l [RT] = L [pr]; R [RT] = R [pr]; 53 If (L = r) {54 return; 55} 56 57 Int M = (L + r)> 1; 58 If (x <= m) Update (L [pr], L [RT], lson, X ); // because no update is required on the right side, the 59 else Update (R [pr], R [RT], rson, x) on the left is overwritten ); 60} 61 62 int query (int pr, int RT, int L, int R, int X) // query the line segment tree between l and R in the R insertion minus L-1-1 insertion {64 if (L = r) {65 return l; 66} 67 // because we have created a weight line segment tree, you can directly search for 68 int M = (L + r)> 1; 69 int now = sum [L [RT]-sum [L [pr]; // now is the left subtree new tree-old tree 70 If (now> = X) return query (L [pr], L [RT], lson, x); 71 else return query (R [pr], R [RT], rson, X-now); 72} 73 74 int RT [maxn]; 75 76 int main () 77 {78 int T; 79 scanf ("% d", & T); 80 while (t --) {81 scanf ("% d", & N, & M ); 82 SZ = 0; 83 for (INT I = 1; I <= N; I ++) {84 scanf ("% d", & A [I]); 85 B [I] = A [I]; 86} 87 sort (B + 1, B + 1 + n); // sort all values first to remove duplicates, used to create a weight line segment tree. The content stored in the weight line segment tree is the number of values. 88 int d = unique (B + 1, B + 1 + n)-(B + 1); 89 build (RT [0], 1, D ); 90 for (INT I = 1; I <= N; I ++) {// insert a 91 int x = lower_bound (B + 1, B + 1 + D, a [I])-B; 92 Update (RT [I-1], RT [I], 1, D, X ); 93} 94 for (INT I = 1; I <= m; I ++) {95 int L, R, K; 96 scanf ("% d ", & L, & R, & K); 97 printf ("% d \ n", B [query (RT L-1], RT [R], 1, D, k)]); 98} 99} 100 return 0; 101}

 

 

 

You come to the world to see the sun and the interesting world outside.

 

 

 

HDU 2665.kth number-K smaller in no modification interval-persistence line segment tree (Chair tree) template

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.