Codeforces Gym 100431G Persistent Queue 可持久化队列
Problem G. Persistent Queue
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88258#problem/G
Description
Persistent data structures are designed to allow access and modication of any version of data structure. In this problem you are asked to implement persistent queue. Queue is the data structure that maintains a list of integer numbers and supports two operations: push and pop. Operation push(x) adds x to the end of the list. Operation pop returns the rst element of the list and removes it. In persistent version of queue each operation takes one additional argument v. Initially the queue is said to have version 0. Consider the i-th operation on queue. If it is push(v, x), the number x is added to the end of the v-th version of queue and the resulting queue is assigned version i (the v-th version is not modied). If it is pop(v), the front number is removed from the v-th version of queue and the resulting queue is assigned version i (similarly, version v remains unchanged). Given a sequence of operations on persistent queue, print the result of all pop operations.
Input
The rst line of the input le contains n the number of operations (1 ≤ n ≤ 200 000). The following n lines describe operations. The i-th of these lines describes the i-th operation. Operation push(v, x) is described as 1 v x, operation pop(v) is described as -1 v. It is guaranteed that pop is never applied to an empty queue. Elements pushed to the queue t standard signed 32-bit integer type.
Output
For each pop operation print the element that was extracted.
Sample Input
10
1 0 1
1 1 2
1 2 3
1 2 4
-1 3
-1 5
-1 6
-1 4
-1 8
-1 9
Sample Output
1
2
3
1
2
4
HINT
题意
让你维护可持久化队列
1 v x 给状态v下的队列的尾部插入x
-1 v pop状态v的队列首部
题解:
dfs离线搞一搞就好了
按照状态建成一个图
然后blabla就吼了……
一开始拿rope写的在线版本,无限TLE= =
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200051
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** vector<pair<int,int> > ans;
vector<int> E[maxn];
vector<int> Q;
struct node
{
int x,y,z;
};
node op[maxn];
void dfs(int x,int l)
{
if(x==)
{
for(int i=;i<E[x].size();i++)
dfs(E[x][i],l);
}
else if(op[x].x==)
{
Q.push_back(op[x].z);
for(int i=;i<E[x].size();i++)
dfs(E[x][i],l);
Q.erase(Q.end()-);
}
else
{
ans.push_back(make_pair(x,Q[l]));
for(int i=;i<E[x].size();i++)
dfs(E[x][i],l+);
}
}
int main()
{
freopen("queue.in","r",stdin);
freopen("queue.out","w",stdout);
int n=read();
for(int i=;i<=n;i++)
{
scanf("%d",&op[i].x);
if(op[i].x==)
{
scanf("%d%d",&op[i].y,&op[i].z);
E[op[i].y].push_back(i);
}
else
{
scanf("%d",&op[i].y);
E[op[i].y].push_back(i);
}
}
dfs(,);
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++)
printf("%d\n",ans[i].second);
}
Codeforces Gym 100431G Persistent Queue 可持久化队列的更多相关文章
- 基于Berkeley DB实现的持久化队列
转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...
- python rabbitMQ持久化队列消息
import pika connection = pika.BlockingConnection( pika.ConnectionParameters('localhost'))#建立一个最基本的so ...
- java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用
1.Enumeration容器使用: package com.etc; import java.util.Enumeration; import java.util.Vector; /* Enumer ...
- python3 Queue(单向队列)
创建队列 import queue q = queue.Queue() empty(如果队列为空,返回True) import queue q = queue.Queue() print(q.empt ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- collections之deque【双向队列】与Queue【单向队列】
今天来向大家介绍两个队列,一个是deque,双向队列,另外一个是Queue,单向队列,队列和堆栈不同,队列为先进先出,大家还需要注意一下,双向队列为collections模块中的类,而Queue为qu ...
随机推荐
- Oracle 课程三之表设计
完成本课程的学习后,您应该能够: •普通堆表优点和缺点 •理解rowid •全局临时表优点.缺点和适用场景 •分区表的类型和原理.优点和缺点.适用场景 •表字段的高效设计 •sequence的设计 ...
- 六款最佳Linux教育应用
导读 对教育行业的用户来说,有好几款专门的Linux发行版是专门面向教育行业的.本文将介绍适合教育领域的几款顶级发行版. 1.Edubuntu 位居榜首的是Edubuntu.顾名思义,Edubuntu ...
- 叉积判断 POJ1696
// 叉积判断 POJ1696 #include <iostream> #include <algorithm> #include <cstring> #inclu ...
- SQL Server 2000的并发连接数是多少
开始->管理工具->性能(或者是运行里面输入 mmc)然后通过 添加计数器添加 SQL 的常用统计(MSSQL General Statistics) 然后在下面列出的项目里面选择 用户连 ...
- Hadoop上路-02_Hadoop FS Shell
一.上传文件/目录 1)put 从本地文件系统中复制N个源路径到目标文件系统. 2)copyFromLocal 源路径须是一个本地文件. 二.下载文件/目录 1)get 复制文件到本地文件系统. 2) ...
- Apache Hadoop 镜像地址
HTTP http://apache.fayea.com/ http://apache.opencas.org/ http://mirror.bit.edu.cn/apache/ http://mir ...
- redis.conf的配置
daemonize yes : redis server 实例是否以后台方式运行 , no:不以后台方式运行(默认) , yes:以后台方式运行. requirepass 密码 : 密码最好长 ...
- 搭建Titanium开发环境
轻松制作 App 再也不是梦! Titanium Mobile 让你能够使用你所熟悉的 web 技术,制作出如同使用Objective-C 或 Java 写出的 Native App. 除了有多达三百 ...
- 搭建linux下teamspeak3多人语音服务器
最近项目中新的需求,需要支持多人在线实时通话.就安装测试一下teamspeak.http://www.teamspeak.com/ 主页有服务器版本和客户端版本供下载安装.软硬件环境: melot@m ...
- php 判断图片类型
脚本之家 <?php $imgurl = "http://www.jb51.net/images/logo.gif"; //方法1 echo $ext = strrchr($ ...