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. ASP.NET 经典60道面试题

    转:http://bbs.chinaunix.net/thread-4065577-1-1.html ASP.NET 经典60道面试题 1. 简述 private. protected. public ...

  2. angular+rails集成实战

    http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/ 1. 添加gemgem 'sprockets ...

  3. bzoj 1001: [BeiJing2006]狼抓兔子 平面图最小割

    平面图跑最大流 可以转换为其对偶图跑最短路 一个环对应一个割  找到最小环(即最短路)极为所求,注意辅助边的建立 加入读入优化  不过时间还是一般  估计是dij写的不好   大神勿喷~~~ /*** ...

  4. mysql EF

    使用 mysql-installer-community-5.6.26.0.msi visual studio 2013 update 4版 Install-Package EntityFramewo ...

  5. linux下mysql数据库的学习

    转载博客:http://freedomljtt.blog.163.com/blog/static/72294949201210145441701/ ubuntu12.04 卸载和安装mysql 卸载m ...

  6. aggregation(2):adaptive Boosting (AdaBoost)

    给你这些水果图片,告诉你哪些是苹果.那么现在,让你总结一下哪些是苹果? 1)苹果都是圆的.我们发现,有些苹果不是圆的.有些水果是圆的但不是苹果, 2)其中到这些违反"苹果都是圆的" ...

  7. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  8. [学习笔记] Web设计过程中该做和不该做的

    原文网址: http://www.javascriptstyle.com/the-dos-and-donts-of-web-design -该做的: QR代码QR代码即快速响应代码,这是矩阵条形码的一 ...

  9. document.write("\x3c\x54")?是加密了吗?

    <script>document.writeln("\x3C\x73\x63\x72\x69\x70\x74\x20\x73\x72\x63\x3D\x22\x48\x54\x5 ...

  10. javap 可以打印出用于jni调用的java函数的签名信息

    javap可以打印出java的字节码: -c     Prints out disassembled code, i.e., the instructions that comprise the Ja ...