poj3468 A simple problem with integers pointer version splay

Source: Internet
Author: User

Title Link: Poj 3468

#include <iostream>#include <fstream>#include <string>#include <time.h>#include <vector>#include <map>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <set>#include <vector>usingnamespace Std;template <class T>inlineBOOLRD (T &ret) {CharCintSgnif(c = GetChar (), c = = EOF)return 0; while(c! ='-'&& (c<' 0 '|| C>' 9 ')) C = GetChar (); SGN = (c = ='-') ? -1:1; ret = (c = ='-') ?0: (C-' 0 '); while(c = GetChar (), C >=' 0 '&&c <=' 9 ') ret = RET *Ten+ (C-' 0 '); RET *= SGN;return 1;} Template <class T>inlinevoidPT (T x) {if(X <0) {Putchar ('-');    x =-X; }if(x>9) PT (X/Ten); Putchar (x%Ten+' 0 ');} typedefLong Longll#define lsum (ch[0]->sum)#define rsum (ch[1]->sum)#define lkey (Ch[0]->key)#define rkey (Ch[1]->key)#define lsiz (ch[0]->size)#define rsiz (ch[1]->size)#define Lson ch[0]#define Rson ch[1]#define llazy (ch[0]->lazy)#define rlazy (ch[1]->lazy)Const intN =1e5+Ten;Const intMAXN =50010;structNode *NULL;structnode{Node *ch[2], *FA;intSize, CNT;//cnt indicates that the current point has a node and the virtual node is 0ll lazy, sum, key; InlinevoidSETC (Node *p,intD) {//p is the D son of thisCH[D] = p; P-&GT;FA = This; } inlineBOOLD () {//This node is the right son        returnfa->ch[1] == This; } inlinevoidPush_up () {size = Lsiz + Rsiz + cnt;    sum = lsum + rsum + key; } inlinevoidPush_down () {if(!lazy)return;if(Lson! =NULL{Lkey + = lazy; Lsum + = Lsiz * LAZY; Llazy + = lazy;}if(Rson! =NULL{Rkey + = lazy; Rsum + = Rsiz * LAZY; Rlazy + = lazy;} Lazy =0; }voidClearint_key) {size = CNT =1;        sum = key = _key; ch[0] = ch[1] = FA =NULL; Lazy =0; } inlineBOOLIsRoot () {returnFA = =NULL|| This! = fa->ch[0] && This! = fa->ch[1]; }}; Node POOL[MAXN * the], *tail; Node *BC[MAXN];intBc_top;//Memory RecoveryvoidInit () {tail = pool; Bc_top =0;NULL= tail++;NULL->size =NULL->cnt =0;NULL->ch[0] =NULL->ch[1] =NULL-&GT;FA =NULL;} InlinevoidRotate (node *x) {node *f = x->fa, *ff = x->fa->fa; F->push_down (); X->push_down ();intc = x->d (), CC = F->d ();    F->setc (X->ch[!c], c); X->setc (f,!c);if(FF-&GT;CH[CC] = = f) ff->setc (x, CC);ElseX-&GT;FA = FF; F->push_up ();} InlinevoidSplay (node* &root, node* x, node* goal) {X->push_down (); while(X-&GT;FA! = goal) {if(X-&GT;FA-&GT;FA = = goal) rotate (x);Else{BOOLf = x->fa->d (); X->d () = = f?            Rotate (X-&GT;FA): Rotate (x);        Rotate (x); }} x->push_up ();if(Goal = =NULL) root = x;}//Find the leftmost one in the R subtreenode* Get_left (node* r) {node* x = r; while(x->ch[0] !=NULL) x = x->ch[0];returnx;}//delete x in root treevoidErase (node* &root, node* x) {splay (root, X,NULL); node* t = root;if(t->ch[1] !=NULL) {root = t->ch[1]; Splay (Root, Get_left (t->ch[1]),NULL); ROOT-&GT;SETC (t->ch[0],0); }Elseroot = root->ch[0];    bc[bc_top++] = x; ROOT-&GT;FA =NULL;if(Root! =NULL) root->push_up ();} node* NewNode (intKey) {node* p;if(bc_top) p = bc[--bc_top];Elsep = tail++; P->clear (key);returnP;} node* get_kth (node* x,intK) {//Search for the K after XX->push_down (); Node*u = x; while(u->ch[0]->size+1! = k) {if(u->ch[0]->size >= k) u = u->ch[0];Else{k-= u->ch[0]->size +1; U = u->ch[1];    } u->push_down (); }returnU;}//Insert a value keyvoidInsert (node* &root,intKey) {if(Root = =NULL{root = NewNode (key);return;    } node* now = root; node* pre = root->fa; while(Now! =NULL){if(Now->key = = key)            {now->cnt++; Splay (Root, now,NULL);return;        } pre = now;    now = Now->ch[key >= now->key];    } Node *x = NewNode (key);    PRE-&GT;SETC (x, key >= Pre->key); Splay (root, X,NULL);}//Delete a value keyvoidErase (node* &root,intKey) {node* now = root; while(Now->key! = key)    {now = Now->ch[key >= now->key]; } now->cnt--;if(now->cnt = =0) Erase (Root, now);ElseSplay (Root, now,NULL);}voidTravel (node* R) {if(r = =NULL)return; Travel (r->ch[0]);    bc[bc_top++] = r; Travel (r->ch[1]);}voidCLEAR (node* &root) {travel (root); Root =NULL;}//query is less than or equal to the number of ValintQuery (Node *root,intVal) {intAns =0; Node *x = root; while(X! =NULL){if(Val < x->key) x = x->ch[0];Else{ans + = x->ch[0]->size + x->cnt; x = x->ch[1]; }    }returnAns;} node* Build (intLintRint*a) {if(L > R)return NULL;intMid = (L + r) >>1;    node* u = NewNode (A[mid]); U-&GT;SETC (Build (L, mid-1, a),0); U-&GT;SETC (Build (Mid +1, R, a),1); U->push_up ();returnU;}voidDebug (node* R) {if(r = =NULL)return; printf"key:%i64d lson:%i64d rson:%i64d\n", R->key, r->ch[0]->key, r->ch[1]->key); Debug (r->ch[0]); Debug (r->ch[1]);}intN, Q;intA[n];intMain () { while(CIN >> n >> Q) { for(inti =1; I <= N;        i++) Rd (A[i]);        Init (); node* root = Build (0, n +1, a); node* lnode,* Rnode, *tmp;Charstr[2]; while(q--) {intL, R;            ll Val; scanf'%s ', str); RD (L);            RD (R); Splay (Root, Get_left (root),NULL);            Lnode = get_kth (root, L); Rnode = get_kth (Root, r+2); Splay (Root, Lnode,NULL);//debug (root);Splay (Root, Rnode, Lnode);//debug (root);            if(str[0] ==' Q ') {PT (root->ch[1]->ch[0]->sum); Putchar (' \ n '); }Else{Rd (Val); TMP = root->ch[1]->ch[0]; Tmp->sum + = val * (r-l +1);                Tmp->lazy + = val;                Tmp->key + = val; root->ch[1]->push_up (); Root->push_up ();//debug (root);}        }    }return 0;}/**/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

poj3468 A simple problem with integers pointer version splay

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.