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 可持久化队列的更多相关文章

  1. 基于Berkeley DB实现的持久化队列

    转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...

  2. python rabbitMQ持久化队列消息

    import pika connection = pika.BlockingConnection( pika.ConnectionParameters('localhost'))#建立一个最基本的so ...

  3. java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用

    1.Enumeration容器使用: package com.etc; import java.util.Enumeration; import java.util.Vector; /* Enumer ...

  4. python3 Queue(单向队列)

    创建队列 import queue q = queue.Queue() empty(如果队列为空,返回True) import queue q = queue.Queue() print(q.empt ...

  5. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  6. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  7. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  8. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  9. collections之deque【双向队列】与Queue【单向队列】

    今天来向大家介绍两个队列,一个是deque,双向队列,另外一个是Queue,单向队列,队列和堆栈不同,队列为先进先出,大家还需要注意一下,双向队列为collections模块中的类,而Queue为qu ...

随机推荐

  1. 最简单的基于FFMPEG的转码程序

    本文介绍一个简单的基于FFmpeg的转码器.它可以将一种视频格式(包括封转格式和编码格式)转换为另一种视频格式.转码器在视音频编解码处理的程序中,属于一个比较复杂的东西.因为它结合了视频的解码和编码. ...

  2. 在web.config里使用configSource分隔各类配置

    转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...

  3. 130道ASP.NET面试题

    转:http://boke.25k5.com/kan88569.html 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . privat ...

  4. JAVA数据库处理(连接,数据查询,结果集返回)

    package john import java.io.IOException; import java.util.*; public class QueryDataRow { public Hash ...

  5. HDU 5433 Xiao Ming climbing

    题意:给一张地图,给出起点和终点,每移动一步消耗体力abs(h1 - h2) / k的体力,k为当前斗志,然后消耗1斗志,要求到终点时斗志大于0,最少消耗多少体力. 解法:bfs.可以直接bfs,用d ...

  6. IIS配置网站(WebServices),局域网都能访问

    IIS配置网站(WebServices),局域网都能访问 前言 上篇说到在本机创建一个WebServices,用控制台应用程序调用WebServices的SayHello方法. http://www. ...

  7. matlab 学习

    http://blog.sina.com.cn/s/blog_7086379501012pc5.html <a href = "http://blog.sina.com.cn/s/bl ...

  8. BBED的安装

    BBED是Block Browser EDitor的缩写,只有linux/unix版本,没有windows版本. 11g中默认是不带bbed的,如果要使用,可以在10g中拷贝过来,然后再进行编译使用. ...

  9. WCF启用Session

    1 服务类添加ASPNETSESSION兼容标记 [System.ServiceModel.Activation.AspNetCompatibilityRequirements(Requirement ...

  10. 数往知来C#面向对象准备〈二〉

    面向对象(OOP→Object-Oriented Programming) 1.什么是面向对象? 一种分析问题的方式. 2.面向对象三大特征: 封装(隐蔽代码实现/复用/修改方便).继承.多态. 3. ...