[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 ...
随机推荐
- DEBUG(2)--函数的输入参数要做适当的检查
今天在调试程序时发现,在单步运行的情况下,程序执行没有问题,但是直接运行就会出问题.出问题的代码如下 for(int col=0;col<=9;++col) { int killid=Pos ...
- Unity --- 纹理为什么要设置为2的N次方
1.图片的纹理像素在Unity3D中需要遵循2的N次方,由图形学决定的,只识别2的N次方. 非2的N次方的图片会转化为2的N次方图片(500 x 500 → 512 x 512),是因为转化过程比 ...
- ssh服务及安全配置
1.清空防火墙 关闭 setenforcesetenforce 2 getenforce 3 setenforce 0 4 iptables -F 5 systemctl stop firewal ...
- (7)Pool进程池
(1)# 开启过多的进程并不一定提高你的效率 因为进程池可以实现并行的概念,比Process单核并发的速度要快 # 如果cpu负载任务过多,平均单个任务执行的效率就会低,反而降低执行速度. 1个人做4 ...
- spring cloud: Hystrix(一):简单使用
在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...
- English trip V1 - B 23. Nosy People 爱管闲事的人 Teacher:Parice Key: Be + Ving
In this lesson you will learn to talk about what happened. 谈论发生什么? 课上内容(Lesson) Nosy 好管闲事Noise 噪声 ...
- H.264开源解码器评测
转自:http://wmnmtm.blog.163.com/blog/static/38245714201142883032575/ 要播放HDTV,就首先要正确地解开封装,然后进行视频音频解码.所以 ...
- 完整的Django入门指南学习笔记3
前言 在本节课中,我们将深入理解两个基本概念: URLs 和 Forms.在这个过程中,我们还将学习其它很多概念,如创建可重用模板和安装第三方库.同时我们还将编写大量单元测试. 如果你是从这个系列教程 ...
- Oracle11g温习-第十八章:role管理
2013年4月27日 星期六 10:52 role 的功能:简化用户的权限管理 建立角色——给角色授权——将角色授予用户/角色 2.查看系统建立的role SYS @ prod > selec ...
- oracle 12c新特性 FETCH FIRST、WITH TIES 关键字详解
几乎都是官方文档上的内容. [ OFFSET offset { ROW | ROWS} ] [ FETCH { FIRST | NEXT }[ { rowcount | percent PER ...