[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\n", splay_tree.Query(x));
}
else
{
int x = read(), y = read();
splay_tree.Move(x, y);
}
}
}
return 0;
}
[HDU2475]Box的更多相关文章
- Virtual Box配置CentOS7网络(图文教程)
之前很多次安装CentOS7虚拟机,每次配置网络在网上找教程,今天总结一下,全图文配置,方便以后查看. Virtual Box可选的网络接入方式包括: NAT 网络地址转换模式(NAT,Network ...
- Linux监控工具介绍系列——OSWatcher Black Box
OSWatcher Balck Box简介 OSWatcher Black Box (oswbb)是Oracle开发.提供的一个小巧,但是实用.强大的系统工具,它可以用来抓取操作系统的性能指标,用 ...
- 使用packer制作vagrant centos box
使用packer制作vagrant box:centos 制作vagrant box,网上有教程,可以自己step by step的操作.不过直接使用虚拟在VirtualBox中制作vagrant b ...
- 快速打造跨平台开发环境 vagrant + virtualbox + box
工欲善其事必先利其器,开发环境 和 开发工具 就是 我们开发人员的剑,所以我们需要一个快并且好用的剑 刚开始做开发的时候的都是把开发环境 配置在 自己的电脑上,随着后面我们接触的东西越来越多,慢慢的电 ...
- CSS3伸缩盒Flexible Box
这是一种全新的布局,在移动端非常实用,IE对此布局的相关的兼容不是很好,Firefox.Chrome.Safrai等需要加浏览器前缀. 先说说这种布局的特点: 1)移动端由于屏幕宽度都不一样,在布局的 ...
- CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset
一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border ...
- 解析opencv中Box Filter的实现并提出进一步加速的方案(源码共享)。
说明:本文所有算法的涉及到的优化均指在PC上进行的,对于其他构架是否合适未知,请自行试验. Box Filter,最经典的一种领域操作,在无数的场合中都有着广泛的应用,作为一个很基础的函数,其性能的好 ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 一个简单的CSS3+js 实现3D BOX
<!doctype html><html><head> <meta charset="UTF-8"> <title>Do ...
随机推荐
- json包
1.官网下载 2.pom文件下载: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId&g ...
- 怎样在winform中上传图片
http://jingyan.baidu.com/article/b7001fe157d6b60e7382dd7f.html 因为WinForm都是运行在本地的,而我们的网站一般都是布署在服务器上,运 ...
- LeetCode--012--整数转罗马数字(java)
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- 纯js无缝滚动
HTML代码 <!--父容器要使用overflow: hidden;--> <div id="imgsList" style="height:150px ...
- Vue-router中的导航钩子
vue-router中的导航钩子,主要用来作用是拦截导航,让他完成跳转或取消.(路由守卫) 原文指路:https://blog.csdn.net/weixin_41399785/article/det ...
- yii框架中使用gii的用法
首先在config文件中的 main-local.php中添加一句 'allowedIPs' => ['*'],如下图所示:
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- mysql命令查询
含义 命令 查看gtid是否开启 show variables like '%gtid%'; 查看只读信息 show global variables like "%read_only%& ...
- java骰子求和算法
//扔 n 个骰子,向上面的数字之和为 S.给定 Given n,请列出所有可能的 S 值及其相应的概率public class Solution { /** * @param n an intege ...
- ajax代码整理
$.ajax({ type: "post", [以POST或GET的方式请求.默认GET.PUT和DELETE也可以用,有的浏览器不支持] url: url, [请求的目的地址,须 ...