[HDU3436]Queue-jumpers
Problem
有一个数列,从1排列到n,然后有Q个操作
- Top x:将第x个数放到序列的最前面
- Query x:询问x这个数在第几位
- Rank x:询问第x位数是什么
Solution
n非常的大,需要离散化:读入的Query操作和Top操作需要离散化
然后每当处理一个数时,用二分计算出离散化后的结果
对于Top操作,先把那个数删掉,然后加在splay的最左边。
Notice
离散化非常复杂
Code
#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 = 500000;
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 s[N + 5], e[N + 5], point = 0, root, now, id[N + 5];
struct node
{
int val[N + 5], Size[N + 5], num[N + 5], son[2][N + 5], parent[N + 5];
inline void up(int u)
{
Size[u] = Size[son[0][u]] + Size[son[1][u]] + num[u];
}
void Newnode(int &u, int from, int v)
{
u = ++point;
parent[u] = from, son[0][u] = son[1][u] = 0;
num[u] = Size[u] = e[v] - s[v] + 1;
val[u] = v, id[v] = u;
}
void Build(int &u, int l, int r, int from)
{
int mid = (l + r) >> 1;
Newnode(u, from, mid);
if (l < mid) Build(son[0][u], l, mid - 1, u);
if (mid < r) Build(son[1][u], mid + 1, r, u);
up(u);
}
int Find(int x)
{
int l = 1, r = now;
while (l <= r)
{
int mid = (l + r) >> 1;
if (x >= s[mid] && x <= e[mid]) return mid;
else if (x < s[mid]) r = mid - 1;
else l = mid + 1;
}
}
void Rotate(int x, int &rt)
{
int y = parent[x], z = parent[y];
int l = (son[1][y] == x), r = 1 - l;
if (y == rt) rt = x;
else if (son[0][z] == y) son[0][z] = x;
else son[1][z] = x;
parent[x] = z;
parent[son[r][x]] = y, son[l][y] = son[r][x];
parent[y] = x, son[r][x] = y;
up(y);
up(x);
}
void Splay(int x, int &rt)
{
while (x != rt)
{
int y = parent[x], z = parent[y];
if (y != rt)
{
if ((son[0][z] == y) ^ (son[0][y] == x))
Rotate(x, rt);
else Rotate(y, rt);
}
Rotate(x, rt);
}
}
void Insert(int &u, int x, int last)
{
if (u == 0)
{
Newnode(u, last, x);
return;
}
else Insert(son[0][u], x, u);
up(u);
}
void Delete(int x)
{
Splay(x, root);
if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
else
{
int t = son[1][x];
while (son[0][t] != 0) t = son[0][t];
Splay(t, root);
son[0][t] = son[0][x], parent[son[0][x]] = t;
up(t);
}
parent[root] = 0;
}
int Find_rank(int x)
{
int t = id[Find(x)];
Splay(t, root);
return Size[son[0][root]] + 1;
}
int Find_num(int u, int k)
{
if (k <= Size[son[0][u]]) return Find_num(son[0][u], k);
else if (k <= Size[son[0][u]] + num[u]) return s[val[u]] + k - Size[son[0][u]] - 1;
else return Find_num(son[1][u], k - Size[son[0][u]] - num[u]);
}
void Top(int x)
{
int t = Find(x);
int y = id[t];
Delete(y);
Insert(root, t, 0);
Splay(point, root);
}
}Splay_tree;
int Q[N + 5], T[N + 5];
char st[N + 5][10];
int sqz()
{
int H_H = read();
rep(cas, 1, H_H)
{
int n = read(), q = read(), num = 0;
Q[0] = 0;
rep(i, 1, q)
{
scanf("%s%d", st[i], &T[i]);
if (st[i][0] == 'T' || st[i][0] == 'Q') Q[++num] = T[i];
}
Q[++num] = n;
sort(Q + 1, Q + num + 1);
now = 0;
rep(i, 1, num)
{
if (Q[i] == Q[i - 1]) continue;
if (Q[i] - Q[i - 1] > 1)
{
s[++now] = Q[i - 1] + 1;
e[now] = Q[i] - 1;
}
s[++now] = e[now] = Q[i];
}
point = 0;
Splay_tree.son[0][0] = Splay_tree.son[1][0] = Splay_tree.parent[0] = Splay_tree.Size[0] = Splay_tree.val[0] = Splay_tree.num[0] = 0;
Splay_tree.Build(root, 1, now, 0);
printf("Case %d:\n", cas);
rep(i, 1, q)
if (st[i][0] == 'T') Splay_tree.Top(T[i]);
else if (st[i][0] == 'Q') printf("%d\n", Splay_tree.Find_rank(T[i]));
else printf("%d\n", Splay_tree.Find_num(root, T[i]));
}
}
[HDU3436]Queue-jumpers的更多相关文章
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
随机推荐
- Ajax请求导出Excel的问题
文章转载自: http://yuwenlin.iteye.com/blog/2275289 Ajax请求导出Excel的问题描述: 前端发起Ajax请求get或post,后台使用Poi生成excel文 ...
- C# : 泛型的继承关系实现的一个可以存放不同数据类型的链表
以下定义的是一个链表结点类型: internal sealed class Node<T> { public T m_data; public Node<T> m_next; ...
- Pytorch Visdom可视化工具
2018-12-04 14:05:49 Visdom是Facebook专门为PyTorch开发的一款可视化工具,其开源于2017年3月.Visdom十分轻量级,但却支持非常丰富的功能,能胜任大多数的科 ...
- (转+整理)C# BinaryFormatter进行序列化与反序列化
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形式使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种种串行化的方式:1. ...
- Oracle DB , 计算各个用户/schema 的磁盘占用空间
http://www.dba-oracle.com/t_find_size_schema.htm Question: How do I find the size of a schema in my ...
- Github的简易操作
一.初涉Github 1.github官网:https://github.com/ 2.三步走完成账号的创建 3.进入Github主页,选择[Create a repository](创建一个仓库用来 ...
- 用ActionController::Renderer的render方法渲染模版
使用Cable进行pub: ActionCable.server.broadcast "call", {address: AddressesController.render(@a ...
- PaaS平台型IT运维&运营模式能给企业带来什么?
关注嘉为科技,获取运维新知 什么是PaaS平台型IT自动化运维&运营模式 PaaS平台型IT运维和运维模式是指:将通用的运维能力与具体的运维场景解耦合,将能够复用的,具备独立功能的通用能力纳入 ...
- mandatory and advisory文件锁(File Lock)
http://blog.csdn.net/elfprincexu/article/details/43564425 文件锁(File Lock)是一种在特定的时间内只允许一个进程进行访问文件的机制,通 ...
- css 选择器二
2.4 盒模型 2.4.1 定义 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型 ...