HDU 1754 splay tree stretching tree (Single Point update, interval attribute query)

Source: Internet
Author: User

Tag: OS Io for Re C time

Question: updated with the interval query point. There are 20 million query points and the maximum value of the query interval. We used a line segment tree, 1000 + Ms. Today's stretching tree is about 890 MS-less.

The first time I learned how to stretch a tree, I spent a total of two units of time. I felt that it was really useful and fun to stretch a tree. Now I only learned a little bit. Try updating the selected vertex.

General idea: Number (array) is used as the key value building of the tree. Each number is inserted, update the maximum value of a node along the route (each node has an additional information mark as the maximum value of all points of the tree in the subtree ). So, when the Query [I, j], as long as the I-1 stretched to the root, J + 1 stretched to the I-1, then J + 1 left subtree is to the range of! You can query the sub-tree root value (the specific terminal )! Similarly, for an update operation, you only need to extend the process to the root, update it, and maintain the information at the same time.

# Include <iostream> # include <cstdio> using namespace STD; const int Maxx = 200010; int A [Maxx]; int child [Maxx] [2]; int Fa [Maxx]; int maxo [Maxx]; // A [I] is the original array value, child ), maxo [I] is the maximum value of the subtree (including itself) with serial number I as the root. Int root = 0; void inline maintain (int n) // maintenance of the point value {maxo [N] = maxo [N]> A [n]? Maxo [N]: A [n];} void inline rotate (int x, int f) // F = 1 right hand, f = 0 left {int y = Fa [X]; maxo [x] = maxo [y]; // maxo [y] = maxo [child [x] [f]> maxo [child [y] [f]? Maxo [child [x] [f]: maxo [child [y] [f]; maintain (y); child [y] [! F] = Child [x] [f]; // three-way reconnection. Pay attention to the sequence. Fa [child [x] [f] = y; If (Fa [y]) {If (y = Child [Fa [y] [0]) child [Fa [y] [0] = x; else child [Fa [y] [1] = x;} else {root = x ;} fa [x] = Fa [y]; child [x] [f] = y; Fa [y] = x;} void splay (int n, int goal) // convert the vertex with serial number I to the child under goal. {While (Fa [N]! = Goal) // always rotate until or after {int y = Fa [N]; rotate (n, child [y] [0] = n? 1:0) ;}} void inline insert (int n) // insert to build {int temp = root; If (root = 0) // root node {root = N; maxo [N] = N; return;} else {While (1) {maxo [temp] = maxo [temp] <A [n]? A [n]: maxo [temp]; // maintain the maximum value during insertion if (n <temp) // {If (child [temp] [0] = 0) on the left) {Child [temp] [0] = N; Fa [N] = temp; maxo [N] = A [n]; splay (n, 0 ); // note that this step should be stretched. Otherwise, the result is a general binary sorting tree, which will time out return;} temp = Child [temp] [0];} else // {If (child [temp] [1] = 0) {Child [temp] [1] = N; Fa [N] = temp; maxo [N] = A [n]; splay (n, 0); return;} temp = Child [temp] [1] ;}}} void Update (int n, int X) // update the value of N to X {splay (n, 0); A [n] = x; maxo [N] = maxo [Child [N] [0]> maxo [child [N] [1]? Maxo [child [N] [0]: maxo [child [N] [1]; maintain (n);} void clear () // initialize {root = 0; for (INT I = 0; I <Maxx; I ++) {maxo [I] = Child [I] [0] = Child [I] [1] = Fa [I] = 0 ;}} int main () {int N, m; while (~ Scanf ("% d", & N, & M) {clear (); For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]); insert (I) ;}char Q; int I, j; while (M --) {getchar (); scanf ("% C % d", & Q, & I, & J); If (q = 'q') {int ans = 0; if (I = 1 & J! = N) // specifies the interval endpoint {splay (J +); ans = maxo [child [J + 1] [0];} else if (I! = 1 & J = N) {splay (I-1, 0); ans = maxo [child [I-1] [1];} else if (I = 1 & J = N) {ans = maxo [root];} else {splay (I-1, 0); splay (J + 1, i-1); ans = maxo [child [J + 1] [0];} printf ("% d \ n", ANS);} else {Update (I, j) ;}}return 0 ;}


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.