"BZOJ1503" "NOI2004" depressed teller, splay, tune me the whole person is not good.

Source: Internet
Author: User
Tags printf

Test Instructions Note: Those employees who have not yet entered the company because of initial wage shortfalls do not leave employees.

Code Note: Delay tags can open a global variable for better writing, no need to add an add to each node, and then Pushdown.

No, it is not difficult to write, test instructions a understand immediately AC ToT.

Paste the code slightly disgusting, many functions are not used, a little bit of a template feel. (The comment-out del function is wrong.) )

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm
> #define N 1000005 #define INF 0x7fffffff #define LL Long Long #define IS (x) (son[fa[x]][1]==x) using namespace std;
/* int digit[n];
*/int ans,flag;
	struct node {int root,n,top,limit;
int val[n],fa[n],son[n][2],size[n],num[n],snum[n];
int add[n],num[n];
	Long Long sum[n],snum[n];
		inline void pushup (int x) {//size[x]=size[son[x][0]]+size[son[x][1]]+1;
		SUM[X]=SUM[SON[X][0]]+SUM[SON[X][1]]+VAL[X]*NUM[X];
	SNUM[X]=SNUM[SON[X][0]]+SNUM[SON[X][1]]+NUM[X];
			}/* inline void pushdown (int x) {if (add[x]) {sum[x]+=snum[x]*add[x];
			VAL[X]+=ADD[X];
			ADD[SON[X][0]]+=ADD[X];
			ADD[SON[X][1]]+=ADD[X];
		add[x]=0;
			}}-inline void Build (int l,int r,int mid) {based on sequence contribution if (l<mid) {int lmid=l+mid-1>>1;
			Build (L,mid-1,lmid);
			Fa[lmid]=mid;
		Son[mid][0]=lmid;
			} if (mid<r) {int rmid=mid+1+r>>1; Build (mid+1,r, Rmid);
			Fa[rmid]=mid;
		Son[mid][1]=rmid;
	} val[mid]=digit[mid],pushup (mid);
	} */inline void link (int x,int y,int d) {son[y][d]=x;fa[x]=y;}
		inline void Rotate (int x) {int y=fa[x],z=fa[y],id=is (x), T=son[x][!id];
		if (t) fa[t]=y;son[y][id]=t;
		Link (x,z,is (y));
		Link (y,x,!id);
	Pushup (y);
		} inline void splay (int x,int k=0) {int y,z;
			while (fa[x]!=k) {y=fa[x];
			Z=fa[y];
			if (z==k) {Rotate (x); break;}
			if (Is (x) ==is (y)) Rotate (y), Rotate (x);
		else Rotate (x), Rotate (x);
		} pushup (x);
	if (!k) root=x;
		} inline int getmin () {int x=root;
		while (Son[x][0]) x=son[x][0];
	return val[x];
		} inline int Getmax () {int x=root;
		while (Son[x][1]) x=son[x][1];
		Splay (x);
	return val[x];
		} inline void findpred (int w) {int x=root;
			while (Son[x][w>val[x]]) {if (w==val[x]) break;
		x=son[x][w>val[x]];
		} if (Val[x]<w) splay (root);
	else splay (x); } inline bool Find (int w,int k=0) {if (!n| | Getmax () <w| |
		Getmin () >w) return 0; int X=rOot
			while (Son[x][w>val[x]]) {if (val[x]==w) {splay (x,k); return 1;}
		x=son[x][w>val[x]];
		} splay (X,k);
		if (val[x]==w) return 1;
	return 0; } inline int Select (int rank,int k=0) {/* for number of rank large, spin below K */if (rank<=0| |
		Snum[root]<rank) return-1;
		int x=root; while (!) (
			Snum[son[x][0]]<rank&&rank<=snum[son[x][0]]+num[x]) {if (Snum[son[x][0]]+1>rank) x=son[x][0];
		else rank=rank-snum[son[x][0]]-num[x],x=son[x][1];
		} splay (X,k);
	return val[x];
		} inline void NewNode (int &x,int y,int W) {x=++top;n++;
		son[x][0]=son[x][1]=0;
		Val[x]=w;
		Fa[x]=y;
	Snum[x]=num[x]=size[x]=1;
		} void Insert (int w,int k=0) {int x=root;
				while (Son[x][w>val[x]]) {if (w==val[x]) {n++;
				num[x]++;
				snum[x]++;
				Splay (X,K);
			return;
		} x=son[x][w>val[x]];
		} newnode (SON[X][W&GT;VAL[X]],X,W);
	Splay (Son[x][w>val[x]);
		} inline void del (int x,int y) {if (!x) return; if (val[x]>=limit) del (son[x][0],x), Pushup (x);
			else {int t=snum[son[x][0]]+num[x];
			ans+=t;n-=t;
			Link (son[x][1],y,0);
		Del (son[x][1],y);
		}}/* void del (int x,int y) {if (!x) return;
			if (val[x]<limit) {ans+=snum[son[x][0]]+num[x];
			Link (son[x][1],y,0);
		Del (son[x][1],y);
		} else del (SON[X][0],X);
	Pushup (y);
		} inline void Insert (int x,int p) {//For sequence insertion, insert a new node after X (can be inserted into a tree) int l=select (x,0), R=select (x+1,l);
		NewNode (SON[R][0],R,P);
	Splay (n,0); } inline void changes (int x,int p) {x=select (x,0), Val[x]=p,splay (x);}
Change the x of the sequence to p */}tree;
	void handle () {int i,j,k;
	int n,m;
	Char a[5];
	scanf ("%d%d", &n,&tree.limit);
	Tree.newnode (Tree.root,0,inf);
		for (i=1;i<=n;i++) {scanf ("%s%d", a,&k);
			if (a[0]== ' I ') {if (k<tree.limit+flag);//ans++;
		else Tree.insert (K-flag);
			} else if (a[0]== ' a ') {flag+=k;
		Tree.limit-=k;
			} else if (a[0]== ' S ') {flag-=k;
			Tree.limit+=k;
			Tree.getmax ();
		Tree.del (tree.root,0); } else {J=tree. Select (tree.N-K);
			if (J==-1) puts ("-1");
		else printf ("%d\n", J+flag);
}} printf ("%d\n", ans);
	} int main () {handle ();
return 0;
 }


Copy to Google Translate translation results

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.