BZOJ 1503 郁闷的出纳员【NOI2004】

题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=1503

很明显,因为修改工资是对所有人操作的,因此我们可以对于每一个新员工,先减去之前的修改,就可以认为他与前面的人经过了相同的修改,再将修改后的值插入Splay即可。有一点特别坑:题目上明明说刚来就嫌待遇太差(100块都不给我!)的算离开的员工,但答案中并没有统计这种情况。。。害得我WA了好久。。。

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

const int inf = 0x3f3f3f3f;

struct Node {
	int val,sz,num;
	Node *fa,*ch[2];
}*root,*null,*L,*R,T[100000+10];

int n,minn,ans,k,cnt;

Node* Newnode(Node *f,int val) {
	T[cnt].fa = f;T[cnt].val = val;T[cnt].sz = 0;T[cnt].ch[0] = T[cnt].ch[1] = null;T[cnt].num = 1;return &T[cnt++];
}

void Update(Node *u) {
	u -> sz = u -> num;
	if(u -> ch[0] != null)u -> sz += u -> ch[0] -> sz;
	if(u -> ch[1] != null)u -> sz += u -> ch[1] -> sz;
}

void Rotate(Node *u) {
	Node *x = u -> fa,*y = x -> fa;int v = u == x -> ch[1];
	u -> fa = y;if(y != null)y -> ch[x == y -> ch[1]] = u;
	x -> ch[v] = u -> ch[v^1];if(u -> ch[v^1] != null)u -> ch[v^1] -> fa = x;
	x -> fa = u;u -> ch[v^1] = x;Update(x);
}

void Splay(Node *u,Node *f) {
	Node *x;
	for(;(x = u -> fa) != f;Rotate(u))if(x -> fa != f)Rotate((u == x -> ch[1]) == (x == x -> fa -> ch[1]) ? x : u);
	Update(u);if(f == null)root = u;
}

void init() {
	null = Newnode(null,0);L = root = Newnode(null,-inf);R = root -> ch[1] = Newnode(root,inf);Update(root -> ch[1]);Update(root);
}

void Insert(int x) {
	Node *u = root;int t;
	while(u != null) {
		if(u -> val == x) {u -> num++;Splay(u,null);return;}
		if(x < u -> val) {
			if(u -> ch[0] == null) {
				Splay((u -> ch[0] = Newnode(u,x)),null);return;
			}
			u = u -> ch[0];
		}
		else {
			if(u -> ch[1] == null) {
				Splay((u -> ch[1] = Newnode(u,x)),null);return;
			}
			u = u -> ch[1];
		}
	}
}

Node* Find(int x) {
	Node *u = root,*ret = R;int rr = inf;
	while(u != null) {
		if(u -> val == x)return u;
		if(u -> val < x)u = u -> ch[1];
		else {
			if(u -> val < rr) {
				rr = u -> val;ret = u;
			}
			u = u -> ch[0];
		}
	}
	return ret;
}

void Delete(int x) {
	Node *now = Find(x);
	Splay(now,null);Splay(L,root);
	ans += root -> ch[0] -> ch[1] -> sz;
	root -> ch[0] -> ch[1] = null;
	Splay(root -> ch[0],null);
}

int query(int x) {
	if(x > root -> sz-2)return -1;
	int r = root -> sz - x;
	Node *u = root;
	while(u != null) {
		int ret1 = u -> ch[0] -> sz,ret2 = ret1 + u -> num;
		if(r > ret1 && r <= ret2)return u -> val + k;
		if(r <= ret1)u = u -> ch[0];
		else {
			r -= ret2;
			u = u -> ch[1];
		}
	}
}

char s[5];

int main() {
	int x;init();
	scanf("%d%d",&n,&minn);
	for(int i = 1; i <= n; i++) {
		scanf("%s%d",s,&x);
		if(s[0] == 'I') {
			if(x >= minn)Insert(x-k);
		}
		else if(s[0] == 'A') {
			k += x;
		}
		else if(s[0] == 'S'){
			k -= x;
			Delete(minn-k);//把小于min-k的都删去 
		}
		else if(s[0] == 'F'){
			printf("%d\n",query(x));
		}
	}
	printf("%d\n",ans);
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值