Problem

有一个数列,从1排列到n,然后有Q个操作

  1. Top x:将第x个数放到序列的最前面
  2. Query x:询问x这个数在第几位
  3. 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的更多相关文章

  1. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  2. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  3. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  4. 初识Message Queue之--基础篇

    之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...

  5. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  6. PriorityQueue和Queue的一种变体的实现

    队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...

  7. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  8. [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 ...

  9. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. 源码之Queue

    看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...

随机推荐

  1. Java中的包扫描(工具)

    在现在好多应用场景中,我们需要得到某个包名下面所有的类, 包括我们自己在src里写的java类和一些第三方提供的jar包里的类,那么怎么来实现呢? 今天带大家来完成这件事. 先分享代码: 1.这个类是 ...

  2. cookie知识点

    1.springmvc框架中,cookie例子 jsp: <%-- Created by IntelliJ IDEA. User: 44262 Date: 2019/2/28 Time: 18: ...

  3. ubuntu解压和压缩文件

    .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...

  4. 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  噪声 ...

  5. java开学第一周测试自我感想

    开学第一周,王建民老师就对我们进行了java测试,对我们说测试题目是基于期末考试的基础难度来出的.我们的考试完全是靠暑假在家自学的基础,如果在家没有自学java,那完全就是看不懂试卷到底要考什么.由于 ...

  6. find_first_zero_bit在使用gcc 4.2.4 编译时,需要保护%eax

    1.3.100 find_first_zero_bit在使用gcc 4.2.4 编译时,需要保护%eax find_first_zero_bit 修订后: /* * Find-bit routines ...

  7. python记录_day02 while循环 格式化 基本运算符

    一.流程控制之while循环 语法: while 条件: 循环体 else: else语句(当条件不成立的时候执行这里 和break没关系) 判断条件是否成立. 如果成立执行循环体.然后再次判断条件, ...

  8. 『OpenCV3』Mat简介

    Mat属性方法介绍:OpenCV2:Mat属性type,depth,step 推荐一套OpenCV入门博客:OpenCV探索 一.Mat Mat类用于表示一个多维的单通道或者多通道的稠密数组.能够用来 ...

  9. 第二阶段——个人工作总结DAY08

    1.昨天做了什么:昨天就时间轴的问题,已经实现了界面的显示. 2.今天打算做什么:打算继续学习<第一行代码>中关于异步任务,多线程,访问网络等后台的知识. 3.遇到的困难:还不太懂具体的步 ...

  10. prometheus远程连接m3db实现存储

    如果是prometheus server配置文件添加如下: remote_read: - url: "http://m3coordinator.m3db.svc.cluster.local: ...