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 ...
随机推荐
- [OFBiz]开发 三
1. Debug不要在Eclipse中使用Ant来启动ofbiz, 因为在Eclipse中无法kill掉Ant的进程,而ofbiz又没有提供stop的方法.(有一个hook shutdown的方法,但 ...
- PHP 系统命令函数
function execute($cmd) { $res = ''; if ($cmd) { if(function_exists('system')) { @ob_start(); @system ...
- ado.net(class0503)
ado.net组成 数据提供程序 connection //连接对象 command executeNonQuery //执行增删改 executeScalar //执行查询返回首行首列 execut ...
- PHP+Apache+MySQL+phpMyAdmin在win7系统下的环境配置
配置方法在网上可以搜到很多,一步步来就好了,但是由于步骤比较多,需要耐心仔细一点点,这是我自己记录的成功步骤: 1.PHP+Apache+MySQL的安装:PHP网站开发 2.phpMyAdmin的配 ...
- sf空间配置
1.创建VHost 记住Homepage,打开VHost DNS标签页,创建Virtual Host,如下图: 2.Wcp上传文件 用户名是"sf用户名,sf项目名" ...
- 【JAVA版】Storm程序整合Kafka、Mongodb示例及部署
一.环境 一台Centos6.5主机 Mongo 3.0 Kafka_2.11-0.8.2.1 Storm-0.9.5 Zookeeper-3.4.6 java 1.7 (后因在mac上打包的jar由 ...
- Unity3d自定义脚本模板
这是一个小技巧,打开Unity安装目录,如: C:\Program Files (x86)\Unity\Editor\Data\Resources\ScriptTemplates /* * * Tit ...
- C#中的堆和栈
看到一篇讲堆和栈的文章,是我目前为止见到讲的最易懂,详细和深入的.我翻译成中文.以此总结. 原文=>C#Heap(ing) Vs Stack(ing) in .NET 在net framewor ...
- 【Hadoop学习】Super用户以其他用户的名义执行操作
Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ...
- openstack domain serverID connect uri