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 ...
随机推荐
- 指针和引用的比较(P105)
指针和引用的比较? 虽然使用引用和指针都可间接访问另一个值,但它们之间有两个重要区别. 第一个区别在于引用总是指向某个对象:定义引用时没有初始化是错误的. 第二个重要区别则是赋值行为的差异:给引用赋值 ...
- [视频监控]用状态机图展示Layout切换关系
监控系统通常会提供多种Layout给用户,用于满足不同需求,如:高清显示单路视频或者同时观察多路监控情况. 文中系统只提供了单路.2x2(2行2列共4路).8路(4行4列布局,从左上角算起,有个核心显 ...
- PHP 实现下载文件的方法
方法一: header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); ...
- 【windows核心编程】一个API拦截的例子
API拦截 修改PE文件导入段中的导入函数地址 为 新的函数地址 这涉及PE文件格式中的导入表和IAT,PE文件中每个隐式链接的DLL对应一个IMAGE_IMPORT_DESCRIPTOR描述符结构, ...
- 机器学习真的可以起作用吗?(2)(以二维PLA算法为例)
一个问题:大多数情况下,M(hypothesis set的大小)是无穷大的,例如PLA算法.那么是不是我们的原则1就不能使用了? 我们试着做一些努力: Step1:寻找hypothesis set的e ...
- [转]使用CSS3 Grid布局实现内容优先
使用CSS3 Grid布局实现内容优先 http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...
- HTTP知识填补
1.HTTP协议 HTTP协议是计算机通信的一种协议 流程: 1.http客户端发起请求,例如手机访问baidu.com,创建端口,一般位80 2.http服务器在端口监听客户端请求 3.http接收 ...
- j2ee指导型框架或示例
springside appfuse springfuse
- iOS学习之基本概念
学习iOS最重要的是态度和兴趣,如果你对于学习始终抱有不断的热情和端正的态度,那么,无论是什么,你总会成功的! 有一句话与大家共勉:过程中跌倒多少次都没有关系,重要的是,跌倒后你能够站起来重新寻找正确 ...
- thymeleaf中的Literals
Literals即为文字 一.Text literals:文本文字 文本文字只是字符串指定的单引号之间.他们可以包含任何字符,但你应避免任何单引号里面\ ' <p> Now you are ...