"Bzoj 1503" "NOI 2004" Depressed Teller

Source: Internet
Author: User

$Splay $ template title.

Review the template for the stretch tree.

For the reduction of wages after the deletion of staff operations, I choose to insert a $min+delta_{reduced wage}$ node, $splay$ it to the root, cut off its own and left subtree, preserving the right subtree, so that the staff will disappear from the world.

#include <cstdio> #include <cstring> #include <algorithm> #define READ (x) x = Getint () using namespace Std;struct Node {node *ch[2], *fa;int D, SZ, sum, Lazy;bool pl () {return this->fa->ch[1] = = this; void SetC (node *r, bool c) {Ch[c] = r; r->fa = this;} void count () {sum = ch[0]->sum + Ch[1]->sum + sz;} void push () {if (lazy! = 0) {Ch[0]->lazy + = Lazy;ch[1]->lazy + lazy;ch[0]->d + lazy;ch[1]->d + = Lazy;lazy = 0;}}} *root, *null;node pool[100003];int top = 0, M, n, gone = 0;namespace splay{void Build () {null = &pool[0];null->d = Null->sz = Null->sum = 0;null->ch[0] = null->ch[1] = NULL-&GT;FA = null; ROOT = null;} Node *newnode () {node *t = &pool[++top];t->d = T->sz = T->sum = 0;t->ch[0] = t->ch[1] = T-&GT;FA = null ; return t;} void Rotate (node *r) {Node *f = r->fa; if (r = = NULL | | f = = NULL) Return;bool c = R-&GT;PL (); if (F-&GT;FA = = null) ROO T = r, R->fa = Null;else f->fa->setc (r, F-&GT;PL ()); F->seTC (R-&GT;CH[!C], c); R->setc (f,!c); F->count ();} void Update (node *r) {if (r! = ROOT) update (R-&GT;FA); R->push ();} void splay (node *r, node *tar = null) {update (R); for (; R->fa! = tar; rotate (R)) if (R->fa->fa = tar) rotate (r-&gt ;p L () = = R-&GT;FA-&GT;PL ()? R-&GT;FA:R); R->count ();} void ins (int num) {if (root = = null) {root = NewNode (); Root->d = num; Root->sz = Root->sum = 1;return;} Node *r = root;while (1) {r->push (); int k = r->d; bool C;if (num < k) C = 0;else if (num > k) c = 1;else {++r- >sz; ++r->sum; Splay (R); return;} if (r->ch[c] = = null) {R->ch[c] = NewNode () r->setc (R->ch[c], c); R->ch[c]->sz = R->ch[c]->sum = 1 ; r->ch[c]->d = Num;splay (R->ch[c]); return;} else R = R->ch[c];}} void ins2 (int num) {node *r = Root;bool C;while (1) {R->push (); if (num <= r->d) c = 0;else c = 1;if (R->ch[c] = = null) {R->ch[c] = NewNode (); R->setc (R->ch[c], c); Splay (R->ch[c]); gone + = Root->ch[0]->sUm ROOT-&GT;CH[1]-&GT;FA = null; ROOT = Root->ch[1];return;} else R = R->ch[c];}} int kth (int k) {node *r = Root;while (r! = null) {R->push (); if (R->ch[1]->sum >= k) R = R->ch[1];else if (r ->ch[1]->sum + R->sz >= k) return r->d;else K-= R->ch[1]->sum + R->sz, r = r->ch[0];} return 0;}} inline int getint () {int k = 0, fh = 1; char c = GetChar (); for (; c < ' 0 ' | | c > ' 9 '; c = GetChar ()) if (c = = '-') FH = -1;for (; c >= ' 0 ' && C <= ' 9 '; c = GetChar ()) K = k * + C-' 0 '; return k * FH; int main () {splay::build (); Read (n); read (M); int num;for (int i = 1; I <= n; ++i) {char c = getchar (); while (C < ' A ' | | C > ' Z ') c = getchar (); switch (c) {case ' I ': Read (num); if (num >= M) splay::ins (num); Break;case ' A ': Read (num); Root->d + = num; Root->lazy + = num; Root->push (); Break;case ' S ': Read (num); SPLAY::INS2 (num + M); Root->d-= num; Root->lazy-= num; Root->push (); Break;case ' F ': Read (num), if (num > Root->sum) putS ("-1"); else printf ("%d\n", splay::kth (num)); break;}} printf ("%d\n", Gone); return 0;}

  

"Bzoj 1503" "NOI 2004" Depressed Teller

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.