ZOJ 4016 mergeable Stack (using the list to simulate the merging of multiple stacks, STL applications, splice functions!!!) )

Source: Internet
Author: User

Mergeable Stack Time limit: 2 Seconds Memory Limit: 65536 KB

Given initially empty stacks, there is 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 -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 was emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

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

Input

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

The first line contains integers and (), indicating the number of stacks and the number of Operatio ns.

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

  • If , integers and ( , ) follow, indicating an operation of the first type.
  • If , one integer ( ) follows, indicating an operation of the second type.
  • If , both integers and (< Span id= "Mathjax-element-39-frame" class= "Mathjax_svg", ) follow, indicating an operation of the third type.

It's guaranteed that neither the sum of or the sum of over all test cases would exceed .

Output

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

Sample Input
22 151 1 101 1 111 2 121 2 133 1 21 2 142 12 12 12 12 13 2 12 22 22 23 73 1 23 1 33 2 12 12 22 32 3
Sample Output
13121110empty14emptyemptyemptyemptyemptyempty

Author: WENG, Caizhi
Source: The 18th Zhejiang University programming Contest sponsored by Tusimple

Analysis:

/*
there are n stacks, q operations
1 s t: Press T into the s stack
2 S: first s stack pops an element and prints
3 s T: Stack T is pressed into the s stack from bottom to top, and the T stack is emptiedNote using the list to simulate the operation of the stack, especially the stack of the merge operation, using the splice function, Learning!!!
*/
#include <stdio.h>#include<iostream>#include<math.h>#include<string.h>#include<Set>#include<map>#include<list>#include<algorithm>using namespaceStd;typedefLong LongLL;intmon1[ -]= {0, to, -, to, -, to, -, to, to, -, to, -, to};intmon2[ -]= {0, to, in, to, -, to, -, to, to, -, to, -, to};intdir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};list<int> li[300005];intMain () {intT,n,q,op; intindex1,index2,v; CIN>>T;  while(t--) {scanf ("%d%d",&n,&q);  for(intI=0; i<=n;i++) li[i].clear ();  while(q--) {scanf ("%d",&op); if(op==1) {scanf ("%d%d",&index1,&v);            Li[index1].push_back (v); }Else if(op==2) {scanf ("%d",&index1); if(Li[index1].empty ()) {printf ("empty\n"); }                Else{printf ("%d\n", Li[index1].back ());                Li[index1].pop_back (); }            }Else if(op==3) {scanf ("%d%d",&index1,&index2);            Li[index1].splice (Li[index1].end (), li[index2]); }        }    }    return 0;}/*there are n stacks, Q operations 1 s t: Press T into the S-Stack 2 s: the first s stack pops an element and prints 3 s t: Stack T is pressed from bottom to top of the s stack, and the T stack is emptied note the operation of the list simulation stack, especially the stack merge operation, using the splice function, Learning!!! */

ZOJ 4016 mergeable Stack (using the list to simulate the merging of multiple stacks, STL applications, splice functions!!!) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.