zoukankan      html  css  js  c++  java
  • _bzoj1208 [HNOI2004]宠物收养所【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1208

    以后在空间限制允许的情况下我绝对不纠结内存占用问题啦!就因为不舍得用long long而用unsigned,爆掉了我好几个小时。

    #include <cstdio>
    
    const int maxn = 80005, delta[2] = {-1, 1}, mod = 1000000;
    
    int n, t1, t2, s, ans;
    int ch[maxn][2], fa[maxn], root, cnt = 2;
    long long key[maxn], smaller, larger;
    
    inline void rotate(int x) {
    	int y = fa[x];
    	if (y == ch[fa[y]][0]) {
    		ch[fa[y]][0] = x;
    	}
    	else {
    		ch[fa[y]][1] = x;
    	}
    	fa[x] = fa[y];
    	int dir = x == ch[y][1];
    	ch[y][dir] = ch[x][dir ^ 1];
    	fa[ch[x][dir ^ 1]] = y;
    	ch[x][dir ^ 1] = y;
    	fa[y] = x;
    }
    inline void splay(int x) {
    	int p;
    	while (fa[x]) {
    		p = fa[x];
    		if (!fa[p]) {
    			rotate(x);
    		}
    		else {
    			if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
    				rotate(x);
    			}
    			else {
    				rotate(p);
    			}
    			rotate(x);
    		}
    	}
    	root = x;
    }
    inline void ist(int x, long long val) {
    	int p = 0, dir = 0;
    	while (x) {
    		p = x;
    		dir = val > key[x];
    		x = ch[x][dir];
    	}
    	++cnt;
    	ch[p][dir] = cnt;
    	fa[cnt] = p;
    	key[cnt] = val;
    	splay(cnt);
    }
    inline void del(long long val) {
    	int x = root;
    	while (key[x] != val) {
    		if (val < key[x]) {
    			x = ch[x][0];
    		}
    		else {
    			x = ch[x][1];
    		}
    	}
    	splay(x); 
    	//注意:此题splay完这个x后,x绝对有两个孩子。
    	fa[ch[x][0]] = fa[ch[x][1]] = 0;
    	int i;
    	for (i = ch[x][0]; ch[i][1]; i = ch[i][1]); 
    	splay(i);
    	ch[i][1] = ch[x][1];
    	fa[ch[x][1]] = i;
    }
    inline long long dayudengyu(long long val) {
    	int x = root;
    	long long rt = 0;
    	while (x && key[x] != val) {
    		if (val < key[x]) {
    			rt = key[x];
    			x = ch[x][0];
    		}
    		else {
    			x = ch[x][1];
    		}
    	}
    	return x? val: rt;
    }
    inline long long xiaoyu(long long val) {
    	int x = root;
    	long long rt = 0;
    	while (x) {
    		if (val <= key[x]) {
    			x = ch[x][0];
    		}
    		else {
    			rt = key[x];
    			x = ch[x][1];
    		}
    	}
    	return rt;
    }
    
    int main(void) {
    	//freopen("in.txt", "r", stdin);
    	scanf("%d", &n);
    	key[1] = -999999999999999LL;
    	ch[1][1] = 2;
    	key[2] = 999999999999999LL;
    	fa[2] = 1;
    	root = 1;
    	while (n--) {
    		scanf("%d%d", &t1, &t2);
    		if (!s) {
    			ist(root, t2);
    		}
    		else {
    			if (t1 ^ (s < 0)) {
    				ist(root, t2);
    			}
    			else {
    				larger = dayudengyu(t2);
    				smaller = xiaoyu(t2);
    				if (larger - (long long)t2 < (long long)t2 - smaller) {
    					ans = (ans + ((larger - (long long)t2) % mod)) % mod;
    					del(larger);
    				}
    				else {
    					ans = (ans + (((long long)t2 - smaller) % mod)) % mod;
    					del(smaller);
    				}
    			}
    		}
    		s += delta[t1];
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    SVN库迁移整理方法----官方推荐方式
    SVN跨版本库迁移目录并保留提交日志
    微信公众号 发送图文消息
    Egret白鹭开发微信小游戏排行榜功能
    双滑动列表实现
    unity之资深工程师
    unity之高级工程师
    lua踩坑系列之浅拷贝与深拷贝
    lua之table.remove你不知道的坑
    unity之Layout Group居中显示
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6164246.html
Copyright © 2011-2022 走看看