Mergeable Stack


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

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

  • 2 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.

  • 3 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

Author: WENG, Caizhi
Source: The 18th Zhejiang University Programming Contest Sponsored by TuSimple

分析:

/*
有n个栈,q次操作
1 s t:将t压入第s个栈
2 s:第s个栈pop一个元素并打印
3 s t:栈t从底到顶压入s栈,并将t栈清空
注意用list模拟栈的操作,特别是栈的合并操作,采用的是splice函数,学习了!!!
*/
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}}; list<int> li[];
int main()
{
int t,n,q,op;
int index1,index2,v;
cin>>t;
while(t--)
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
li[i].clear();
while(q--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d %d",&index1,&v);
li[index1].push_back(v);
}else if(op==)
{
scanf("%d",&index1);
if(li[index1].empty())
{
printf("EMPTY\n");
}
else
{
printf("%d\n",li[index1].back());
li[index1].pop_back();
}
}else if(op==)
{
scanf("%d %d",&index1,&index2);
li[index1].splice(li[index1].end(),li[index2]);
}
}
}
return ;
}
/*
有n个栈,q次操作
1 s t:将t压入第s个栈
2 s:第s个栈pop一个元素并打印
3 s t:栈t从底到顶压入s栈,并将t栈清空 注意用list模拟栈的操作,特别是栈的合并操作,采用的是splice函数,学习了!!!
*/

ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)的更多相关文章

  1. ZOJ 4016 Mergeable Stack(栈的数组实现)

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

  2. ZOJ 4016 Mergeable Stack 链表

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

  3. ZOJ - 4016 Mergeable Stack 【LIST】

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

  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. Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))

    F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. [ZOJ 4016] Mergable Stack

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

  8. 利用正则表达式模拟计算器进行字符串的计算实现eval()内置函数功能

    代码感觉有点绕,刚开始学习python,相关知识点还没全部学习到,还请各位大神多多指教 import re # 定义乘法 def mul(string): mul1 = re.search('-?\d ...

  9. Mergeable Stack ZOJ - 4016(list)

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

随机推荐

  1. C++ 的那些坑 (Day 1)

    永远的溢出 运算溢出 溢出是一个永恒的话题. int a = 0xf000; int b = 0xff000; long c = a * b; 此时c = -251658240,以为已经使用了long ...

  2. js 下拉加载

    // 下拉加载    var clientHeight = $(window).height() //当前可视的页面高度    console.log(clientHeight) //滚动条到页面底部 ...

  3. element-ui input组件源码分析整理笔记(六)

    input 输入框组件 源码: <template> <div :class="[ type === 'textarea' ? 'el-textarea' : 'el-in ...

  4. 【代码笔记】iOS-performSelectorOnMainThread

    代码: RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewControl ...

  5. 使用openpyxl模块将Excel中的数据导入数据库

    这里将不介绍openpyxl模块的详细操作. 主要就是记录一个使用openpyxl模块将Excel表格的数据导入数据库中的实例. from openpyxl import load_workbook ...

  6. oop(Object Oriented Programming)

    嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...

  7. 前端开发笔记(3)css基础(中)

    上一篇中我们学习了html的标准文档流,下面我们先来看看如何脱离标准流. 脱离标准流 css中一共有三种方法脱离标准流 浮动 绝对定位 固定定位 浮动 我们要搞清楚什么是浮动,先来看一个标准文档流的例 ...

  8. AJAX删除事件与加载数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Android项目实战(三十):Fresco加载gif图片并播放

    前言: 项目中图文混合使用的太多太多了,但是绝大部分都是静态图片. 然而项目开发中有这么一个需求:显示一个出一个简短的动画(一般都不超过3秒)演示 比如说:一个功能提供很多步骤来教用户做广播体操,那么 ...

  10. Expo大作战(二十七)--expo sdk api之Util(expo自带工具类),tackSnapshotAsync,Svg,SQLite

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...