Bzoj 1503 Depressed Teller

Source: Internet
Author: User

A very clever place is: since all the people increase wages, then can be directly in the overall change.

However, it is not right to use Value+delta in this form, because later people have not withstood the test of the front.

So we're going to save the Value-delta in the splay and then add the current delta when the weights are output.

#include <iostream>#include<cstdio>#include<cstring>#defineMAXN 100500#defineINF 99999999using namespacestd;intDelta=0, n,minn,tree[maxn][3],W[MAXN],FATH[MAXN],SIZE[MAXN],VALUE[MAXN];intx,ans=0, tot=0, re,root=0;Chartype[5];voidPushup (intNow ) {    intls=tree[now][1],rs=tree[now][2]; Size[now]=size[ls]+size[rs]+W[now];}intInsertintXintNowintfather) {    intRegis; intls=tree[now][1],rs=tree[now][2]; if(now==0) { now=++tot; Fath[now]=father; Size[now]=1; w[now]=1; if(X<value[father]) tree[father][1]=Now ; Elsetree[father][2]=Now ; Value[now]=x; returnNow ; }    if(X<value[now]) regis=Insert (X,ls,now); Else if(X>value[now]) regis=Insert (X,rs,now); Else{size[now]++;w[now]++;regis=Now ;}    Pushup (now); returnRegis;}voidRotateintXint&k) {    inty=fath[x],z=Fath[y],l,r; if(tree[y][1]==X) l=1;ElseL=2; R=3-l; if(y==k) k=x; Else    {        if(tree[z][1]==y) tree[z][1]=x; Elsetree[z][2]=x; } Fath[x]=z;fath[y]=x;fath[tree[x][r]]=y; TREE[Y][L]=tree[x][r];tree[x][r]=y; Pushup (y);p ushup (x);}voidSplay (intXint&k) {     while(x!=k) {inty=fath[x],z=Fath[y]; if(y!=k) {if((tree[y][1]==x) ^ (tree[z][1]==y)) rotate (x,k); Elserotate (y,k);    } rotate (x,k); }}voidPreintXintNow ) {    if(now==0)return; intls=tree[now][1],rs=tree[now][2]; if(value[now]<x) {re=Now ;    Pre (X,RS); }    ElsePre (X,LS);}intRankintXintNow ) {    if(now==0)return 0; intls=tree[now][1],rs=tree[now][2]; if(X<=size[rs])returnrank (X,RS); Else if(X>size[rs]+w[now])returnRank (x-size[rs]-w[now],ls); Else returnValue[now];}voidWork1 () {intRegis; scanf ("%d",&x); if((tot==0) || (root==0)) {tot++;root=tot;value[root]=x-delta;size[root]=1; w[root]=1;} Else if(X>=minn) {Regis=insert (X-delta,root,root); splay (Regis,root);}}voidWork2 () {scanf ("%d",&x); Delta=delta+x;}voidWork3 () {scanf ("%d",&x); Delta=delta-x; PRE (Minn-delta,root); if(re!=0) {splay (re,root); Ans=ans+size[tree[root][1]]+W[root]; Root=tree[root][2]; } Re=0;}voidWork4 () {intRegis; scanf ("%d",&x); Regis=rank (x,root); if(regis==0) printf ("-1\n"); Elseprintf"%d\n", regis+delta);}intMain () {scanf ("%d%d",&n,&Minn);  for(intI=1; i<=n;i++) {scanf ("%s", type); if(type[0]=='I') Work1 (); Else if(type[0]=='A') Work2 (); Else if(type[0]=='S') Work3 (); ElseWork4 (); } printf ("%d\n", ans); return 0;}

Bzoj 1503 Depressed Teller

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.