Bzoj 1208 pet adoption Institute splay

Source: Internet
Author: User

Link: http://www.lydsy.com/JudgeOnline/problem.php? Id = 1208

Find the successor to delete a node
Directly rotate the node to be deleted to the root node, and then delete the root node.

/*************************************** * *********************** Problem: 1208 User: wuyiqi language: C ++ result: accepted time: 216 MS memory: 5500 kb find the precursor to delete a node directly to be deleted node rotation to the root, then, you can delete the root node *********************************** * ****************************/# include <cstdio> # include <cstdlib> const int mod = 1000000; const int INF = ~ 0u> 2; # define kt (CH [CH [RT] [1] [0]) const int maxn = 200010; int Lim; struct splaytree {int SZ [maxn]; int ch [maxn] [2]; int pre [maxn]; int RT, top; inline void up (int x) {SZ [x] = CNT [x] + SZ [CH [x] [0] + SZ [CH [x] [1];} inline void rotate (int x, int f) {int y = pre [X]; ch [y] [! F] = CH [x] [f]; Pre [CH [x] [f] = y; Pre [x] = pre [y]; if (pre [x]) CH [pre [y] [CH [pre [y] [1] = y] = X; ch [x] [f] = y; Pre [y] = x; up (y);} inline void splay (int x, int goal) {// rotate X under goal while (pre [x]! = Goal) {If (pre [pre [x] = goal) rotate (x, CH [pre [x] [0] = X ); else {int y = pre [X], Z = pre [y]; int F = (CH [Z] [0] = y ); if (CH [y] [f] = x) rotate (x ,! F), rotate (x, f); else rotate (Y, F), rotate (x, f) ;}} up (x); If (Goal = 0) RT = x;} inline void RTO (int K, int goal) {// rotate the K-digit to the bottom of Goal int x = RT; while (SZ [CH [x] [0]! = K-1) {If (k <SZ [CH [x] [0] + 1) x = CH [x] [0]; else {k-= (SZ [CH [x] [0] + 1); X = CH [x] [1] ;}} splay (x, goal );} inline void vist (int x) {If (x) {printf ("Node % 2D: Left son % 2D right son % 2D % 2D SZ = % d \ n", X, ch [x] [0], CH [x] [1], Val [X], SZ [x]); vist (CH [x] [0]); vist (CH [x] [1]) ;}} inline void newnode (Int & X, int c) {x = ++ top; ch [x] [0] = CH [x] [1] = pre [x] = 0; SZ [x] = 1; CNT [x] = 1; val [x] = C;} inline void Init () {ans = 0; type = -1; ch [0] [0] = CH [0] [1] = pre [0] = SZ [0] = 0; RT = Top = 0; CNT [0] = 0; newnode (RT,-INF); newnode (CH [RT] [1], INF); Pre [Top] = RT; SZ [RT] = 2;} inline void insert (Int & X, int key, int f) {If (! X) {newnode (x, key); Pre [x] = f; splay (x, 0); return;} If (Key = Val [x]) {CNT [x] ++; SZ [x] ++; return;} else if (Key <Val [x]) {insert (CH [x] [0], key, x) ;}else {insert (CH [x] [1], key, x) ;}up (x) ;}void del () {int T = RT; if (CH [RT] [1]) {RT = CH [RT] [1]; RTO (1, 0 ); ch [RT] [0] = CH [T] [0]; If (CH [RT] [0]) Pre [CH [RT] [0] = RT ;} else RT = CH [RT] [0]; Pre [RT] = 0; up (RT);} void findpre (int x, int key, Int & ANS) {If (! X) return; If (Val [x] <= Key) {ans = x; findpre (CH [x] [1], key, ANS );} else findpre (CH [x] [0], key, ANS);} void findsucc (int x, int key, Int & ANS) {If (! X) return; If (Val [x]> = Key) {ans = x; findsucc (CH [x] [0], key, ANS );} else findsucc (CH [x] [1], key, ANS);} void solve () {int A, B, X, Y; scanf ("% d ", & A, & B); if (a = type | SZ [RT] = 2) {insert (RT, B, 0), type =; // printf ("type = % d \ n", type);} else {findpre (RT, B, x); findsucc (RT, B, y ); if (ABS (Val [x]-B) <= ABS (Val [y]-B) {ans + = ABS (Val [x]-B ); ans % = MOD; splay (x, 0); del () ;}else {ans + = ABS (Val [y]-B); ans % = MOD; splay (Y, 0); del () ;}// SPT. vist (RT);} int CNT [maxn]; int Val [maxn]; int type; int ans;} SPT; int main () {int N; scanf ("% d", & N); SPT. init (); While (n --) SPT. solve (); printf ("% d \ n", SPT. ans); 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.