Hdu 1754 I Hate It (splay tree extension tree)

Source: Internet
Author: User

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; const int maxn = 222222; int son [2] [maxn], col [maxn], fa [maxn], size [maxn], val [maxn]; int tot; int num [maxn]; void new_node () {size [tot] = 1; fa [tot] = son [0] [tot] = son [1] [tot] =-1; val [tot] = col [tot] = 0; tot ++;} void update (int rt) {size [rt] = 1; col [rt] = val [rt]; if (son [0] [rt]! =-1) size [rt] + = size [son [0] [rt], col [rt] = max (col [rt], col [son [0] [rt]); if (son [1] [rt]! =-1) size [rt] + = size [son [1] [rt], col [rt] = max (col [rt], col [son [1] [rt]);} int build (int l, int r) {if (l> r) return-1; int mid = (l + r)> 1; new_node (); int temp = tot-1; val [temp] = num [mid]; son [0] [temp] = build (l, mid-1); if (son [0] [temp]! =-1) fa [son [0] [temp] = temp; son [1] [temp] = build (mid + 1, r ); if (son [1] [temp]! =-1) fa [son [1] [temp] = temp; update (temp); return temp;} void rot (int rt, int c) {int y = fa [rt], z = fa [y]; son [! C] [y] = son [c] [rt]; if (son [c] [rt]! =-1) fa [son [c] [rt] = y; fa [rt] = z; if (z! =-1) {if (y = son [0] [z]) son [0] [z] = rt; else son [1] [z] = rt ;} son [c] [rt] = y, fa [y] = rt; update (y);} void splay (int rt, int to) {while (fa [rt]! = To) {if (fa [fa [rt] = to) {if (rt = son [0] [fa [rt]) rot (rt, 1); else rot (rt, 0);} else {int y = fa [rt], z = fa [y]; if (rt = son [0] [y]) {if (y = son [0] [z]) rot (y, 1), rot (rt, 1); else rot (rt, 1), rot (rt, 0);} else {if (y = son [1] [z]) rot (y, 0), rot (rt, 0); else rot (rt, 0), rot (rt, 1) ;}} update (rt ); /* why do I only need to update the rt node at the end? In the course of rotation, fa [rt] Always rotates under rt, So we constantly update fa [rt] without updating rt all the time, update rt after splay. */} int find (int rt, int key) {int cnt = 0; if (son [0] [rt]! =-1) cnt = size [son [0] [rt]; if (cnt + 1 = key) return rt; if (cnt> = key) return find (son [0] [rt], key); return find (son [1] [rt], key-cnt-1);} int main () {int n, m, I, j, k, a, B; char op [11]; while (scanf ("% d", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) scanf ("% d", & num [I]); tot = 0; int root = build (0, n + 1), temp; while (m --) {scanf ("% s % d", op, & a, & B ); a ++, B ++; // when creating a table, 0 is also created, but 0 is not in the original array, one if (op [0] = 'U') {temp = find (root, a); splay (temp,-1); root = temp; val [root] = B-1; // update (root); // Why not update rt here? Because the size value will not change, but the val value has been updated, and for the col value, the next time it is splay, will certainly be updated} else {temp = find (root, a-1); splay (temp,-1); root = temp; temp = find (root, B + 1); splay (temp, root); printf ("% d \ n", col [son [0] [temp]) ;}}}

 

Related Article

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.