【NOI2004】郁闷的出纳员 平衡树

5 篇文章 0 订阅
3 篇文章 0 订阅

PS:SPLAY依然不能一次写对…… 总会写出毛病,真是醉醉醉醉醉……


这题难点在于变动工资,我们只需要变动工资K,表示工资现在增加了K。 然后如果有员工进来,【并且进的来,初始工资比最低工资高】,那么我们就把这个员工的工资,减去K这个值,加入到平衡树中。


删人的的时候,只要查比【初始工资+工资变化量K】小的数字全删掉。 这里不需要一个一个删,大多数平衡树都会支持删整棵树的操作的~ 就直接把比那个值小的整棵树都删掉即可。




#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int maxint = 0x7fffffff;


struct node
{
	node *c[2];
	int key;
	int size;
	node()
	{
		key = size = 0;
		c[0] = c[1] = this;	
	}
	node(int KEY_, node *c0, node *c1)
	{
		key = KEY_;	
		c[0] = c0;
		c[1] = c1;
	}
	node*	rz(){return size = c[0]->size + c[1]->size + 1, this;}
}Tnull, *null = &Tnull;

 

struct splay
{
	node *root;
	splay ()
	{
		root = (new node(*null)) -> rz();	
		root -> key = maxint;
	}
	inline void zig(int d)
	{
		node *t = root -> c[d];
		root -> c[d] = null -> c[d];
		null -> c[d] = root;
		root = t;
	}
	inline void zigzig(int d)
	{
		node *t = root -> c[d]	-> c[d];
		root -> c[d] -> c[d] = null -> c[d];
		null -> c[d] = root -> c[d];
		root -> c[d] = null -> c[d] -> c[!d];
		null -> c[d] -> c[!d] = root -> rz();
		root = t;
	}
	inline void finish(int d)
	{
		node *t = null -> c[d], *p = root -> c[!d];	
		while (t != null)
		{
			t = null -> c[d] -> c[d];	
			null -> c[d] -> c[d] = p;
			p = null -> c[d] -> rz();
			null -> c[d] = t;
		}
		root -> c[!d] = p;
	}
	inline void select(int k)
	{
		int t;	
		while (1)
		{
			bool d = k > (t = root -> c[0] ->size);	
			if (k == t || root -> c[d] == null)	break;
			if (d)	k -= t+ 1;
			bool dd = k > (t = root -> c[d] -> c[0] -> size);
			if (k == t || root -> c[d] -> c[dd] == null)	{zig(d); break;}
			if (dd) k-= t+1;
			d != dd ? zig(d), zig(dd) : zigzig(d);
		}
		finish(0), finish(1);
		root -> rz();
	}

	inline void search(int x)
	{
		while (1)	
		{
			bool d = x > root -> key;
			if (root -> c[d] == null)	break;
			bool dd = x > root -> c[d] -> key;
			if (root -> c[d] -> c[dd] == null)	{zig(d); break;}
			d != dd? zig(d), zig(dd) : zigzig(d);
		}
		finish(0), finish(1);
		root -> rz();
		if (x > root -> key)	select(root -> c[0] -> size + 1);
	}
	inline void ins(int x)
	{
		search(x);	
		node *oldroot = root;
		root = new node(x, oldroot -> c[0], oldroot);
		oldroot -> c[0] = null;	
		oldroot -> rz();
		root -> rz();
	}
	inline void del(int x)
	{
		search(x);	
		node *oldroot = root;
		root = root -> c[1];
		select(0);
		root -> c[0] = oldroot -> c[0];
		root -> rz();
		delete oldroot;
	}
	int sel(int k)	{return select(k -1), root->key;}
	int ran(int x){return search(x), root -> c[0] -> size + 1;}
}sp;

 
int limit, n;

int main()
{
	scanf("%d%d", &n, &limit);
	char flag;
	int tmp, biandong=0;
	int away = 0;
	while (n--)
	{
		scanf("\n%c%d", &flag, &tmp);
		if (flag == 'I')
		{
			tmp -= limit;
			if (tmp < 0)//一开始给的工资就不足
			{
				/*尼玛!  为毛这不算离开公司? “他就会立刻气愤地离开公司”  这难道不算“离开公司的员工的总数”。  因为他不算员工么!?*/
				//++away;                     
				continue;	
			}			
			sp.ins(tmp + biandong);	
		}
		if  (flag == 'A')	biandong -= tmp;
		if (flag == 'S')
		{
			biandong += tmp;	
			sp.ran(biandong);
			away += sp.root -> c[0] -> size ;
			sp.root -> c[0] = null;
			sp.root -> rz();
		}
		if (flag == 'F')
		{
			if (tmp > sp.root -> size -1)	
			{
				printf("-1\n");
				continue;
			}
			printf("%d\n", sp.sel(sp.root -> size - tmp) + limit - biandong); 
		}
	}
	printf("%d\n", away);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值