[NOI2004] Depressed teller sbt__delay

Source: Internet
Author: User

Reprint please indicate the source, thank http://blog.csdn.net/ACM_cxlove?viewmode=contents by---Cxlove

Splay Tree wrote 4, 5 hours, Debug can't, only kneel lick ah.

Switch to SBT, or SB tree easy to use ...

Use a variable as a delay tag in a tree that resembles a line, without having to update it every time you add or subtract. In later nodes inserted, the delay should be counted, if the initial is less than the lower limit of the addition, and does not count as the last to leave the number.

At the time of deletion, if the root node is less than the lower limit, the Zuozi and the root node are less than the lower limit, and the right subtree is the root of the tree directly. The new tree is processed.

If the root node is greater than the lower limit, the right subtree is all greater than the lower bound, processing the Zuozi, and finally updating the size of the root.

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define N 100005
using namespace Std;
	struct sbt{//Zoozi tree pointer, right subtree pointer, size, key value int left,right,size,key;
		void Init () {left=right=key=0;
	size=1;
}}t[n]; int Root,tot;
	The location of the root and the number of nodes//left rotation handles void Left_rot (int &x) {int k=t[x].right;
	T[x].right=t[k].left;
	T[k].left=x;
	T[k].size=t[x].size;
	t[x].size=t[t[x].left].size+t[t[x].right].size+1;
X=k;
	//Right rotation handles void Right_rot (int &x) {int k=t[x].left;
	T[x].left=t[k].right;
	T[k].right=x;
	T[k].size=t[x].size;
	t[x].size=t[t[x].left].size+t[t[x].right].size+1;
X=k;
			//Adjust processing void maintain (int &r,bool flag) {if (flag) {//Update right subtree if (t[t[t[r].right].right].size>t[t[r].left].size)
		Left_rot (R);
			else if (t[t[t[r].right].left].size>t[t[r].left].size) {Right_rot (t[r].right);
		Left_rot (R);
	else return;
		else{//Update in left subtree if (t[t[t[r].left].left].size>t[t[r].right].size) Right_rot (R); else if (t[t[t[r].left).Right].size>t[t[r].right].size) {Left_rot (t[r].left);
		Right_rot (R);
	else return;
	//update subtree, then update root until balance maintain (t[r].left,false);
	Maintain (t[r].right,true);
	Maintain (R,FALSE);
Maintain (r,true);
		}//Insert new node void Insert (int &r,int k) {if (r==0) {r=++tot; T[R].
		Init ();
	T[r].key=k;
		} else{t[r].size++;
		if (K<t[r].key) Insert (t[r].left,k);
		else Insert (t[r].right,k);
	After inserting to adjust, guarantee balance maintain (r,k>=t[r].key);
	}///delete node, using the predecessor to replace int Remove (int &r,int k) {int d_key;
	if (!r) return 0;
	t[r].size--; The former description is the node to be deleted, and the latter indicates that there is no such node if (t[r].key==k| | (T[r].left==0&&k<t[r].key) | | (T[r].right==0&&k>t[r].key))
		{D_key=t[r].key;
		if (t[r].left&&t[r].right) t[r].key=remove (t[r].left,k+1);
	else R=t[r].left+t[r].right; else Remove (K<t[r].key?)
T[R].LEFT:T[R].RIGHT,K);
	} void Delete (int &r,int delay,int min_val) {if (!r) return;
		if (t[r].key+delay<min_val) {r=t[r].right;
	Delete (R,delay,min_val);} else{Delete (T[r].left,delay,min_val);
	t[r].size=t[t[r].right].size+t[t[r].left].size+1;
	}//Get the maximum value, that is, to traverse to the right node int get_max (int &r) {while (t[r].right) r=t[r].right;
return R;
	//Get the minimum value, that is, to traverse the leftmost node int get_min (int &r) {while (t[r].left) R=t[r].left;
return R;
	//Get the precursor int get_pre (int &r,int y,int k) {if (r==0) return y;
	if (K>t[r].key) Get_pre (t[r].right,r,k);
else Get_pre (t[r].left,y,k);
	}//obtain subsequent int get_next (int &r,int y,int k) {if (r==0) return y;
	if (K<t[r].key) Get_next (t[r].left,r,k);
else Get_next (t[r].right,y,k);
	//Get a small number of K, note: Can not solve the number of duplicate int get_min_kth (int &r,int k) {int t=t[t[r].left].size+1;
	if (t==k) return t[r].key;
	if (t<k) return get_min_kth (T[R].RIGHT,K-R);
else return get_min_kth (t[r].left,k);
	//Get the K-large number int get_max_kth (int &r,int k) {int t=t[t[r].right].size+1;
	if (t==k) return t[r].key;
	if (t<k) return get_max_kth (T[R].LEFT,K-T);
else return get_max_kth (t[r].right,k); //Get the rank of the node int get_rank (int; R,int k) {if (K<t[r].key) return Get_rank (T[R].LEFT,K);
	else if (K>t[r].key) return Get_rank (t[r].right,k) +t[t[r].left].size+1;
else return t[t[r].left].size+1;
	}//Sort void inorder (int &r) {if (r==0) return;
	Inorder (T[r].left);
	printf ("%d\n", T[r].key);
Inorder (T[r].right);
	int main () {int q,delay,miv_val;
		while (scanf ("%d%d", &q,&miv_val)!=eof) {tot=delay=root=0;
			while (q--) {char str[10];
			int k;
			scanf ("%s%d", str,&k);
				if (str[0]== ' I ') {if (k<miv_val) continue;
			Insert (Root,k-delay);
			else if (str[0]== ' A ') delay+=k;
			else if (str[0]== ' F ') printf ("%d\n", T[root].size<k?-1: (get_max_kth (ROOT,K) +delay));
				else{delay-=k;
			Delete (Root,delay,miv_val);
	} printf ("%d\n", tot-t[root].size);
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.