zoukankan      html  css  js  c++  java
  • [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;
    }
    
  • 相关阅读:
    Creating A Simple Web Server With Golang
    go-import下划线的作用
    golang 查询数据库操作
    golang 跨平台编译——go 在windows上编译Linux平台的程序(Cross Compilation from Windows to Linux/Ubuntu)
    golang convert integer to float number
    AngularJS $q 和 $q.all 单个数据源和多个数据源合并(promise的说明)
    golang中关闭http server
    如何快速掌握plc或工控机与其他设备的modbus通讯协议?包括格式与实际过程 RT,本人从事工控行业多年,对于PLC与触摸屏也算比较熟悉,唯独对这个通讯协议比较难理解,请教高人指导,从什么地方开始下手,或者是说如何正确理解报文格式或正确写入
    网络适配器是啥
    路由器与交换机
  • 原文地址:https://www.cnblogs.com/WizardCowboy/p/7631387.html
Copyright © 2011-2022 走看看