Poj 2761 feed the dogs (treap calculates the K decimal number)

Source: Internet
Author: User

// Treap calculates the K decimal number <br/> // thanks to the guidance of jsh Daniel. <br/> // The reason why this question can be done with treap is that there is a condition in the meaning of the question, the query interval does not contain any information. Therefore, you can sort the query interval <br/> // and then insert or delete the query interval, <br/> // If the Balance Tree is used to maintain the K of the interval, the treap is the first choice. The more difficult part of the treap is to delete it, the deletion method is to rotate the nodes to be deleted to the leaf node while maintaining that the column is heap, delete <br/> # include <iostream> <br/> # include <cstdio> <br/> # include <algorithm> <br/> # include <ctime> <br /> using namespace STD; <br/> const int max = 100005; <br/> struct treap <br/> {<br/> int da Ta, level, num; <br/> int L, R; <br/>}tree [Max]; <br/> int size, root; <br/> void left (Int & X) // note that <br/> {<br/> int P = tree [x] is referenced here. r; <br/> tree [X]. R = tree [p]. l; // note that the order of the row and the next row cannot be reversed <br/> tree [p]. L = x; <br/> X = P; <br/> tree [X]. num = 1; // modify the number of children at the current node <br/> P = tree [X]. l; <br/> If (P! = 0) // The left-left subtree information must be updated <br/>{< br/> tree [p]. num = 1; <br/> If (tree [p]. l! = 0) tree [p]. Num + = tree [tree [p]. L]. Num; <br/> If (tree [p]. R! = 0) tree [p]. num + = tree [tree [p]. r]. num; <br/> tree [X]. num + = tree [p]. num; <br/>}< br/> If (tree [X]. r! = 0) tree [X]. num + = tree [tree [X]. r]. num; <br/>}< br/> void right (Int & X) <br/>{< br/> int P = tree [X]. l; <br/> tree [X]. L = tree [p]. r; <br/> tree [p]. R = x; <br/> X = P; <br/> tree [X]. num = 1; <br/> P = tree [X]. r; <br/> If (P! = 0) <br/>{< br/> tree [p]. num = 1; <br/> If (tree [p]. l! = 0) tree [p]. Num + = tree [tree [p]. L]. Num; <br/> If (tree [p]. R! = 0) tree [p]. num + = tree [tree [p]. r]. num; <br/> tree [X]. num + = tree [p]. num; <br/>}< br/> If (tree [X]. l! = 0) tree [X]. num + = tree [tree [X]. l]. num; <br/>}< br/> void insert (Int & X, int data) <br/>{< br/> If (x = 0) <br/>{< br/> X = ++ size; <br/> tree [X]. data = data; <br/> tree [X]. L = tree [X]. r = 0; <br/> tree [X]. level = rand (); <br/> tree [X]. num = 1; <br/>}< br/> else if (Data <tree [X]. data) <br/> {<br/> tree [X]. num ++; <br/> insert (tree [X]. l, data); <br/> If (tree [X]. level <tree [tree [X]. l]. level) Right (x); <br/>}< br/> Else <br/> {<br/> tree [X]. num ++; <br/> insert (tree [X]. r, data); <br/> If (tree [X]. level <tree [tree [X]. r]. level) Left (x); <br/>}< br/> void remove (Int & X, int data) <br/>{< br/> If (x = 0) return; <br/> If (Data <tree [X]. data) <br/> {<br/> tree [X]. num --; <br/> remove (tree [X]. l, data); <br/>}< br/> else if (data> tree [X]. data) <br/> {<br/> tree [X]. num --; <br/> remove (tree [X]. r, data); <br/>}< br/> else <br/> {<Br/> If (tree [X]. L = 0 & tree [X]. R = 0) x = 0; // at this time, there is no left and right subtree. When it reaches the leaf, delete it <br/> else if (tree [X]. L = 0) x = tree [X]. r; <br/> else if (tree [X]. R = 0) x = tree [X]. l; <br/> else <br/> {<br/> If (tree [tree [X]. l]. level> tree [tree [X]. r]. level) <br/> {<br/> right (x); // rotate the node to delete the node to the leaf, then delete <br/> tree [X]. num --; // after each rotation, X will be the root of the current subtree <br/> remove (tree [X]. r, data); <br/>}< br/> else <br/>{< br/> left (x); <br/> tree [X]. num --; <B R/> remove (tree [X]. l, data); <br/>}< br/> int n, m, ID; <br/> int A [Max]; <br/> struct query <br/> {<br/> int St, Ed, ID, K, ans; <br/>} Q [Max]; <br/> bool CMP1 (query a, query B) <br/>{< br/> If (. st = B. st) <br/> return. ed <B. ed; <br/> return. st <B. st; <br/>}< br/> bool cmp2 (query a, query B) <br/>{< br/> return. ID <B. ID; <br/>}< br/> int search (int x, int K) <br/>{< br/> int P = tree [X]. R; // subtract the num of the right subtree to determine the number of the row in the current node <br/> int K = tree [X]. Num-(P! = 0? Tree [p]. num: 0); <br/> If (k = k) return tree [X]. data; <br/> else if (k> K) return search (tree [X]. l, k); <br/> else return search (tree [X]. r, k-k); <br/>}< br/> void solve () <br/>{< br/> root = size = 0; <br/> sort (q + 1, q + ID + 1, CMP1); <br/> for (INT I = 1; I <= m; ++ I) <br/>{< br/> if (I! = 1) <br/>{< br/> for (Int J = Q [I-1]. st; j <= min (Q [I-1]. ed, Q [I]. ST-1); + + J) <br/> remove (root, a [J]); <br/>}< br/> Int J; <br/> if (I = 1) J = Q [I]. st; <br/> else J = max (Q [I-1]. ED + 1, Q [I]. st); <br/> for (; j <= Q [I]. ed; ++ J) <br/> insert (root, a [J]); <br/> q [I]. ans = search (root, Q [I]. k); <br/>}< br/> sort (q + 1, q + ID + 1, cmp2); <br/> for (INT I = 1; I <= m; ++ I) <br/> printf ("% d/N", Q [I]. ans); <br/>}< br/> int main () <br/> {<br/> srand (time (0 )); <br/> // freopen ("in.txt", "r", stdin); <br/> int St, Ed, K; <br/> scanf ("% d", & N, & M); <br/> for (INT I = 1; I <= N; ++ I) scanf ("% d", & A [I]); <br/> for (INT I = 1; I <= m; ++ I) <br/>{< br/> scanf ("% d", & St, & ED, & K); <br/> If (ST> ed) swap (St, Ed); <br/> q [I]. st = sT; <br/> q [I]. ed = Ed; <br/> q [I]. k = K; <br/> q [I]. id = I; <br/>}< br/> solve (); <br/>} 

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.