noi2004 Depressed Storage Clerk Treap

Source: Internet
Author: User
Tags int size printf

Yesterday there was a splay, this morning when the toilet (= =) suddenly think of the original 90 points of the program may be due to the use of CIN caused a timeout .... And then came home at noon, 100 points.

The program is slightly longer. Look at it. The optimization of that priority queue is useless.

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <
 
Cstdlib> #include <queue> using namespace std;
	struct WBYSR {WBYSR *l,*r;
	int size,dui,num;
	 	WBYSR (int x) {l=null;
	 	R=null;
	 	Num=x;
	 Dui=rand ()%100000+1;
 
}}*root;
int n,i,m,ans=0,tot=0;
 
char c;
	int size_l (WBYSR *t) {if (t->l) return t->l->size;
else return 0;
	} int Size_r (WBYSR *t) {if (t->r) return t->r->size;
else return 0;  
} void Rotate_left (WBYSR *&t)//??,??????,??????
	{WBYSR *p=t->r;
	t->r=p->l;
	p->l=t;
	p->size=t->size;
	T->size=size_l (t) +size_r (t) +1;
t=p;
	} void Rotate_right (WBYSR *&t) {WBYSR *p=t->l;
    T->l = p->r;
    P->r = t;
    P->size = t->size;
    T->size=size_l (t) +size_r (t) +1;
t=p;
	} int Ask (WBYSR *t,int d) {if (t==root&&d>root->size) return-1;
	int p=size_l (t);
	if (d==p+1) return t->num; else if (d< =p) return Ask (T-&GT;L,D);
else return ask (T-&GT;R,D-1-P);
	  	} void Insert (WBYSR *&t,int D) {if (!t) {t=new WBYSR (d);
	  t->size=1;
	 	} else if (d<=t->num) {t->size++;
	 	Insert (T-&GT;L,D);
	 if (T->l->dui < T->dui) Rotate_right (t);
	  	} else//if (d>t->num) {t->size++;	  	  
	  	Insert (T-&GT;R,D);
	  if (T->r->dui < T->dui) Rotate_left (t);
	}} void Delete_ (WBYSR *&t,int D) {if (t==null) return;
		else if (D < t->num) {t->size--;
	Delete_ (T-&GT;L,D);
		} else if (d > T->num) {t->size--;
	Delete_ (T-&GT;R,D);
   	     } else {if (t->l==null&&t->r==null)//delete t;
   	     T=null;
   	   tf->l=null,tf->r=null;
   	   t->num=0;
   	   else if (t->l==null) t=t->r;
   	   else if (t->r==null) t=t->l;
  else if (T->l->dui > T->r->dui) { 	     	Rotate_right (t);
   	     	t->size--;
   	     Delete_ (T-&GT;R,D);
   	     	} else {rotate_left (t);
   	     	t->size--;
   	     Delete_ (T-&GT;L,D);
	}}} void Delete_2 (WBYSR *&t,int D) {if (t==null) return;
		else if (D < t->num) {t->size--;
	Delete_ (T-&GT;L,D);   	  
   	   } else {if (t->l==null&&t->r==null) t=null;   	   else if (t->l==null) t=t->r;   	  
   	   ;
   	     	/* Else {rotate_left (t);
   	     	t->size--;
   	     Delete_ (T-&GT;L,D);
	}*/}} void Find (WBYSR *&t) {if (t==null) return;
	 	if (t->l==null) {if (t->r==null) t=null;
	 	 else {t=t->r;
}} else find (t->l);
	} int main () {priority_queue<int,vector<int>,greater<int> > Q;
	cin>>n>>m;
	int u=0;
	 	for (i=1;i<=n;i++) {GetChar ();
	 	scanf ("%c", &c); Switch(c) {case ' I ': {int y;
	 		  	  	scanf ("%d", &y);
	 		  	  	if (y<m) break;
	 		  	  	Y-=u;
	 		  	  	Insert (root,y);
	 		  	  	Q.push (y);
	 		  	  	tot++;
	 		  	  Break
			     	} case ' A ': {int y;
			     	scanf ("%d", &y);
			     	U+=y;
			     	Add (root,y);
			     Break
			    	} case ' S ': {int y;
			    	scanf ("%d", &y);							
					U-=y;
					System ("pause");	    				    	
					int Temp=q.top ();
			    	 	while (temp+u<m&&tot>1&&root!=null) {ans++;
			    	 	tot--;
						 Find (root);
						Delete_2 (root,temp);
						 Find (root);
						 Q.pop ();	
						Temp=q.top ();		    	 	
			    	 Cout<<ask (root,1) << ' <<u<< ' <<tot<<endl;	    	 
			    	  	} if (tot==1&&root->size==1&&root->num+u<m) {q.pop ();
			 Root=null;   	  	tot--;
			    	  	ans++;
			    	  cout<<tot<<endl; }/*for (int k=1;k<=2;k++) cout<<ask (root,k) << ' <<k<< ' <<root->si 
			    	ze<<endl;
			    	Delete_ (root,6); for (int k=1;k<=2;k++) cout<<ask (root,k) << ' <<k<< ' <<root->size<<end l;//*//*while (root!=null) {cout<<ask (root,1) << ' <<root->size<<e
			    	  	Ndl			    	  	
			    	  Delete_ (Root,ask (root,1));
			    	}*///add (root,-y);
			    Break
			   	  } case ' F ': {int y;
			   	  scanf ("%d", &y);
						if (Y>tot) {printf ("%d\n",-1);
			   	    Break
			   	  } printf ("%d\n", U+ask (root,tot-y+1));
			   Break
	}}} printf ("%d\n", ans);
return 0; }

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.