(splay) NOI2004 depressed Teller __splay

Source: Internet
Author: User
Tags mul
The topic is relatively simple, with splay dynamic maintenance of a can be deleted collectively,
Do not need to lay the mark, to make a superscript maintenance size for kth,
All operations can be done with Delta record offsets.

Commissioning for two hours, and then found that the initial salary of the staff to walk is not a walk of the staff, the title did not say, too pit.
In Splay, the INF Sentinel, but I did not find anyone with a null sentry, I found that the Null Sentinel can greatly simplify the code, while with a certain degree of fault tolerance.
The fault tolerance of the program is very important, even if the parameters are normal and it is best to write a pointer check, do not care about this constant, when access to NULL or even NULL to do special treatment, even if this is not possible, in many cases can save your life.
If my splay code has a place to optimize, please contact me.

#include <cstdio> #include <cstdlib> #include <climits> using namespace std;
    #define INF (INT_MAX/2) struct Node {INT val,size,mul;
    Node *ch[2],*father;
    Node (int _val,int _mul,node *_null): Val (_val), size (_mul), Mul (_mul) {ch[0]=ch[1]=father=_null;
        } void Setch (int w,node *v) {ch[w]=v;
    v->father=this;
        } void Rot () {Node *u=father;
        int w= (u->ch[0]==this);
        U->setch (!w,ch[w]);
        U->father->setch (U->father->ch[1]==u,this);
        Setch (W,u);
    u->size=u->ch[0]->size+u->ch[1]->size+u->mul;
} *null=new Node (0,0, (node*) 0);
 
Node *root=null; void splay (node *v,node *goodfather) {for (node *u; (U=v->father)!=goodfather;v->rot ()) if (u->father!=
Goodfather) ((u->ch[0]==v) = = (U->father->ch[0]==u->father) u:v)->rot ();
    v->size=v->ch[0]->size+v->ch[1]->size+v->mul; if (goodfather==NULL) Root=v;
    node *s_lower (int x) {node *ans=null;
        For (Node *u=root;u!=null;u=u->ch[u->val<x]) {if (u->val==x) return u;
    if (u->val>x) ans=u;
return ans;
    node *s_upper (int x) {node *ans=null;
        For (Node *u=root;u!=null;u=u->ch[u->val<x]) {if (u->val==x) return u;
    if (u->val<x) ans=u;
return ans;
    } void S_insert (int goodval) {if (root==null) {root=new Node (goodval,1,null);
    Node *res=null,*u=root;
        while (true) {int w=u->val<goodval;
            if (u->val==goodval) {+ + (Res=u)->mul;
        Break
            else if (u->ch[w]==null) {U->setch (w,res=new Node (goodval,1,null));
        Break
    else u=u->ch[w];
} splay (Res,null);
    int s_delete (int l,int r) {splay (S_upper (L), NULL);
    Splay (S_lower (R), root);
    Node *seg=root->ch[1]->ch[0];
    if (Seg==null) return 0; Root->ch[1]->ch[0]=null; (Root->ch[1])->size= (root->ch[1])->ch[0]->size+ (root->ch[1))->ch[1]->size+ (root->ch [1])
    ->mul;
    root->size=root->ch[0]->size+root->ch[1]->size+root->mul;
Return seg->size;
    Node *s_kth (int k) {if (k<1) return null;
    Node *u=root;
        while (k<=u->size) {int q1=u->ch[0]->size,q2=q1+u->mul;
        if (K&LT;=Q1) u=u->ch[0];
        else if (K&GT;Q2) {k-=q2;u=u->ch[1];} 
    else {splay (u,null); return u;}
return null;
    int main () {int case_cnt,minval,num_go=0,delta=0;
    scanf ("%d%d", &case_cnt,&minval);
    S_insert (-inf); S_insert (INF);
        while (case_cnt--) {char cmd[6];
        int k;
        scanf ("%s%d", cmd,&k);
            if (cmd[0]== ' I ') {K-=delta; 
            if (K<minval-delta);//++num_go;>o< how can this?!!!
        else S_insert (k);
      else if (cmd[0]== ' A ') {delta+=k;  else if (cmd[0]== ' S ') {delta-=k;
        Num_go+=s_delete (-inf,minval-delta);//!!!!!!
            else if (cmd[0]== ' F ') {Node *v=s_kth (root->size-2-k+1+1);
            if (v==null | | v->val>=inf | | v->val<=-inf) printf (" -1\n");
        else printf ("%d\n", V->val+delta);
    } printf ("%d\n", Num_go);
return 0; }


Related Keywords:

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.