• [HDU2475]Box


    Problem

    先告诉你每个盒子在哪个盒子的内部
    接下来有M个操作:
    可以把一个盒子及里面的盒子移到另外一个盒子的内部
    或者询问你某个盒子最外面的盒子是哪个

    Solution

    首先可以建成一个图,然后先dfs一遍,用dfs序加入多棵splay
    然后移动操作就是区间切割,询问操作就是这棵splay中最小的值

    Notice

    注意,因为有多棵splay,用0这个虚点连接起来。所以细节非常难处理

    Code

    #pragma GCC optimize(2)
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define sqz main
    #define ll long long
    #define reg register int
    #define rep(i, a, b) for (reg i = a; i <= b; i++)
    #define per(i, a, b) for (reg i = a; i >= b; i--)
    #define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
    const int INF = 1e9, N = 100000;
    const double eps = 1e-6, phi = acos(-1);
    ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
    ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
    void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
    int Left[N + 5], Right[N + 5], T[N * 2 + 5], head[N + 5];
    int point, num, root, time;
    struct Node
    {
    	int vet, next;
    }edge[N + 5];
    void add(int u, int v)
    {
    	edge[++num].vet = v;
    	edge[num].next = head[u];
    	head[u] = num;
    }
    struct node
    {
    	int val[N + 5], parent[N + 5], son[2][N + 5];
    	void Newnode(int &u, int from, int v)
    	{
    		u = ++point;
    		val[u] = v, parent[u] = from;
    		son[0][u] = son[1][u] = 0;
    		if (v > 0) Left[v] = u;
    		else Right[-v] = u;
    	}
    	void Build(int &u, int l, int r, int from)
    	{
    		int mid = (l + r) >> 1;
    		Newnode(u, from, T[mid]);
    		if (l < mid) Build(son[0][u], l, mid - 1, u);
    		if (r > mid) Build(son[1][u], mid + 1, r, u);
    	}
    	
    	void Rotate(int x)
    	{
    		int y = parent[x], z = parent[y];
    		int l = (son[1][y] == x), r = l ^ 1;
    		if (z != 0) son[son[1][z] == y][z] = x;
    		parent[x] = z;
    		parent[son[r][x]] = y, son[l][y] = son[r][x];
    		parent[y] = x, son[r][x] = y;
    	}
    	void Splay(int x, int rt)
    	{
    		if (x == rt)  return;
    		while (parent[x] != rt)
    		{
    			int y = parent[x], z = parent[y];
    			if (z != rt)
    				if ((son[0][y] == x) ^ (son[0][z] == y)) Rotate(x);
    				else Rotate(y);
    			Rotate(x);
    		}
    	}
    	
    	int Find_pre(int x)
    	{
    		x = son[0][x];
    		while (son[1][x]) x = son[1][x];
    		return x;
    	}
    	int Find_suf(int x)
    	{
    		x = son[1][x];
    		while (son[0][x]) x = son[0][x];
    		return x;
    	}
    	
    	int Query(int x)
    	{
    		x = Left[x];
    		Splay(x, 0);
    		while (son[0][x]) x = son[0][x];
    		return val[x];
    	}
    	void Move(int x, int y)
    	{
    		if (x == y) return;
    		int xx = Left[x], yy = Right[x];
    		Splay(xx, 0);
    		Splay(yy, xx);
    		int xxx = son[0][xx], yyy = son[1][yy], ttt = Find_pre(xx);
    		son[0][xx] = 0, son[1][yy] = 0;
    		parent[xxx] = parent[yyy] = 0;
    		if (ttt) 
    		{
    			son[1][ttt] = yyy;
    			parent[yyy] = ttt;
    		}
    		if (y == 0) return;
    		if (Query(y) == x)
    		{
    			son[0][xx] = xxx, son[1][yy] = yyy;
    			parent[xxx] = xx, parent[yyy] = yy;
    			son[1][ttt] = 0; return;
    		}
    		Splay(Left[y], 0);
    		ttt = Find_suf(Left[y]);
    		Splay(ttt, Left[y]);
    		son[0][son[1][Left[y]]] = xx;
    		parent[xx] = son[1][Left[y]];
    	}
    }splay_tree;
    void dfs(int u)
    {
    	T[time++] = u;
    	travel(i, u)
    	{
    		int v = edge[i].vet;
    		dfs(v);
    	}
    	T[time++] = -u;
    }
    
    int sqz()
    {
    	int n, flag = 0;
    	while (~scanf("%d", &n))
    	{
    		if (flag) puts("");
    		else flag = 1;
    		point = num = time = 0;
    		memset(head, 0, sizeof head);
    		rep(i, 1, n)
    		{
    			int x = read();
    			add(x, i);
    		}
    		dfs(0);
    		int now = 0, last = 1;
    		rep(i, 1, 2 * n)
    		{
    			if (T[i] > 0) now++;
    			else now--;
    			if (!now)
    			{
    				splay_tree.Build(root, last, i, 0);
    				last = i + 1;
    			}
    		}
    		int q = read(); char st[10];
    		while(q--)
    		{
    			scanf("%s", st);
    			if (st[0] == 'Q')
    			{
    				int x = read();
    				printf("%d
    ", splay_tree.Query(x));
    			}
    			else
    			{
    				int x = read(), y = read();
    				splay_tree.Move(x, y);
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    产品管理:启示录 特约客户、产品验证、原型测试
    我对敏捷个人培训的“三不原则”
    《敏捷个人》周刊 第2期 (可下载)
    《敏捷个人》周刊 第7期 (可下载)
    敏捷个人2012.6月份线下活动报道:与北邮学子交流职业和成长
    《敏捷个人》周刊 第11期 (可下载)
    敏友的【敏捷个人】有感(15): 初探敏捷个人和敏捷开发的感想
    敏友的【敏捷个人】有感(14): 敏捷个人管理的历程
    从0开始在Android下开发生活方向盘应用(自绘雷达图)
    OpenExpressApp:OEA框架 2.9 PreAlpha 源码公布
  • 原文地址:https://www.cnblogs.com/WizardCowboy/p/7631387.html
Copyright © 2020-2023  润新知