【HNOI2004】宠物收养所 平衡树

其实就是查找前驱和后继的问题, 因为绝对值最小的情况,一定是前驱和后继和k的绝对值。


这里处理有一个小方法, 一开始在树中加入maxnum和minnum2个值。 树有2个节点,就为空树。这样就不会有不存在前驱和后继的情况了。 


同时还用到一个技巧,flag。 flag=0和1.   如果读入的是0和1,和flag相同就插入,否则就删除。 如果树的节点数量为2,那么flag^=flag即可。


长期使用的ZKW splay时间效率还不错。

这题有几个细节:

1、答案要mod

2、在比较出前驱和后继谁更优之前不能MOD(我在这里卡了一会儿……WA掉5个数据)

3、想省事就long long吧……


Accepted 100 137 ms 1752 KB 3.2 KB G++ 


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

#define LL long long
const LL maxint = 120000000000LL;

struct node
{
	node *c[2];
	LL key, size;
	node()
	{
		key = 0, size = 0, c[0] = c[1] = this;	
	}
	node(LL 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)//找左儿子有k个size的节点
	{
		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(dd);
		}
		finish(0), finish(1);
		root -> rz();
	}
	inline void search(LL 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(LL x)
	{
		search(x);	
		node *oldroot = root;
		root = new node(x, oldroot -> c[0], oldroot);
		oldroot -> c[0] = null;
		oldroot -> rz();
		root -> rz();
	}
	
	void Tdel(LL x)
	{
		search(x);
		node *oldroot=root;
		root=root->c[1];
		select(0);
		root->c[0]=oldroot->c[0];
		root->rz();
		delete oldroot;
	}
	
	inline void del(LL x)
	{
		search(x);	
		node *oldroot = root;
		root = root -> c[1];
		select(0);
		root -> c[0] = oldroot -> c[0];
		root -> rz();
		delete oldroot;
	}
	inline LL sel(int k)	{return select(k - 1), root -> key;}
	inline int ran(LL x)	{return search(x), root -> c[0] -> size + 1;}
}sp;

LL flag , tmp, last_flag = -1, ans = 0, tmp_ans;
LL a, b, rank;
int n;

int main()
{
	scanf("%d", &n);
	sp.ins(-120000000000LL);
	while (n--)
	{
		scanf("%d%lld", &flag, &tmp);
		if (sp.root -> size == 2)	last_flag = flag;	
		if (flag == last_flag)	sp.ins(tmp);
		else{
			rank = sp.ran(tmp);
			a = sp.sel(rank - 1); //比tmp小的数字
			b = sp.sel(rank);//比tmp大的数字
			tmp_ans = tmp - a; //默认答案是比tmp小的
			if (tmp_ans <= b - tmp)	sp.del(a);
			else 
			{
				tmp_ans = b- tmp;
				sp.del(b);
			}
			ans = (tmp_ans + ans) % 1000000;
		}	
	}
	printf("%lld\n", ans);
	return 0;
}


你可能感兴趣的:(【HNOI2004】宠物收养所 平衡树)