Mergeable Stack


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Given  initially empty stacks, there are three types of operations:

  • s v: Push the value  onto the top of the -th stack.

  • s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.

  • s t: Move every element in the -th stack onto the top of the -th stack in order.

    Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .

    After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

There are  operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the number of stacks and the number of operations.

The first integer of the following  lines will be  (), indicating the type of operation.

  • If , two integers  and  (, ) follow, indicating an operation of the first type.
  • If , one integer  () follows, indicating an operation of the second type.
  • If , two integers  and  (, ) follow, indicating an operation of the third type.

It's guaranteed that neither the sum of  nor the sum of  over all test cases will exceed .

Output

For each operation of the second type output one line, indicating the answer.

Sample Input

2
2 15
1 1 10
1 1 11
1 2 12
1 2 13
3 1 2
1 2 14
2 1
2 1
2 1
2 1
2 1
3 2 1
2 2
2 2
2 2
3 7
3 1 2
3 1 3
3 2 1
2 1
2 2
2 3
2 3

Sample Output

13
12
11
10
EMPTY
14
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],top[maxn],bottom[maxn],Next[maxn];
int cnt;
void Push(int s,int v)
{
a[++cnt]=v;
if(bottom[s]==)
{
bottom[s]=cnt;
}
Next[cnt]=top[s];
top[s]=cnt;
}
void Pop(int s)
{
if(top[s]==)printf("EMPTY\n");
else
{
printf("%d\n",a[top[s]]);
top[s]=Next[top[s]];
if(top[s]==)bottom[s]=;
}
}
void swapp(int s,int v)
{
if(bottom[s]==)bottom[s]=bottom[v];
Next[bottom[v]]=top[s];
top[s]=top[v];
bottom[v]=top[v]=;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
memset(bottom,,sizeof(bottom));
memset(top,,sizeof(top));
memset(Next,,sizeof(Next));
int n,Q;scanf("%d%d",&n,&Q);
cnt=;
while(Q--)
{
int op,x,y;scanf("%d",&op);
if(op==)
{
scanf("%d%d",&x,&y);
Push(x,y);
}
else if(op==)
{
scanf("%d",&x);
Pop(x);
}
else
{
scanf("%d%d",&x,&y);
if(bottom[y]==)continue;
swapp(x,y);
}
}
}
return ;
}

ZOJ 4016 Mergeable Stack(栈的数组实现)的更多相关文章

  1. ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, ther ...

  2. ZOJ - 4016 Mergeable Stack 【LIST】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...

  3. ZOJ 4016 Mergeable Stack 链表

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  4. ZOJ - 4016 Mergeable Stack (STL 双向链表)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...

  5. ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...

  6. [ZOJ 4016] Mergable Stack

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...

  7. Mergeable Stack ZOJ - 4016(list)

    ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...

  8. Mergeable Stack(链表实现栈)

    C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 1.栈在内存很大需要扩容 ...

  9. 基于数组实现Java 自定义Stack栈类及应用

    栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则.java本身是有自带Stack类包,为了达到学习目的已经更好深入了解sta ...

随机推荐

  1. Java Hibernate 5.3.x

    SchemeExport Hibernate根据实体类和实体类映射文件自动生成表结构. 示例代码: <?xml version='1.0' encoding='utf-8'?> <! ...

  2. Windows 7安装PHP运行环境和开发环境

    1. 安装Apache 下载地址:http://www.apache.org/dyn/closer.cgi/httpd/binaries/win32 如需更改端口:打开Apache安装目录下conf目 ...

  3. linux基础(10)-导航菜单

    导航菜单实战 例:编写一个shell脚本,包含多个菜单,其中需要一个退出选项:可单选也可多选:根据序号选择后,显示所选菜单名称. #!/bin/bash ####################### ...

  4. JavaWeb -- sevlet 监听器

    1.三个域对象的监听(创建和销毁) servletContext,  session, request 监听器 ServletContext监听器: public class MyServletCon ...

  5. 执行安装redis报错undefined reference to `__sync_add_and_fetch_4'

    执行make命令时报错:   zmalloc.o: In function `zmalloc_used_memory': /var/lib/tcommsvr/redis-2.8.0-rc4/src/z ...

  6. phpPgAdmin (win)配置安装及远程访问

    phpPgAdmin (win)配置安装 [1]     通过PostgreSQL的Application Stack Builder配置安装phpPgAdmin 1.确保PostgreSQL安装并正 ...

  7. js中介者模式

    中介者模式(Mediator),用一个中介者对象来封装一系列对象交互.中介者使各对象不需要显示地相互引用,从而使其松散,而且可以独立地改变它们之间的交互. 从生活中例子自然知道,中介者模式设计两个具体 ...

  8. array_merge函数的注意事项

    array_merge — 合并一个或多个数组 array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组 如果输入的数组中有相同的字符串键名 ...

  9. ADO.NET异步操作测试

    配置文件: <?xml version="1.0"?> <configuration> <startup> <supportedRunti ...

  10. 常用T-CODE ,快捷键

    RSA1 --主界面. RSA8 --后台处理数据源和层次结构,很少使用 RSRT --QUERY 测试  .输入技术名称查询即可 RSRV--分析修复BW对象 RSMO --信息包监测. 检查处理链 ...