Mergeable Stack

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


传送门


思路


num[ ]:保存所有加入栈的元素;

head[i]:表示第i个栈的栈顶元素在num[ ]中的下标,若栈为空,则head[i]=-1;

Next[i]:num[ ]中下标为i的元素的下一个元素(一个元素只可能出现在一个栈里);

tail[i]:表示第i个栈的栈底元素在num[ ]中的下标,若栈为空,则tail[i]=-1;


注意细节!!!


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int MAX = 3e5+100;

int n,q;
int num[MAX],head[MAX],Next[MAX],tail[MAX];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&q);
        
        //init
        memset(num,0,sizeof(num));
        memset(head,-1,sizeof(head));
        memset(Next,0,sizeof(Next));
        memset(tail,-1,sizeof(tail));
        
        //num[ ]的元素总数
        int tot=0;
        
        for(int i=0;i<q;i++)
        {
            int op;
            scanf("%d",&op);
            //插入一个元素:把head[ ]指向那个元素在num[ ]中的下标
            if(op==1)
            {
                int s,v;
                scanf("%d%d",&s,&v);
                int x=head[s];
                num[tot]=v;
                head[s]=tot;
                Next[tot]=x;
                //如果这是第s个栈里的第一个元素,把tail[ ]指向这个元素在num[ ]中的下标
                if(x==-1)
                {
                    tail[s]=tot;
                }
                tot++;
            }
            else if(op==2)
            {
                int s;
                scanf("%d",&s);
                if(head[s]==-1)
                    printf("EMPTY\n");
                else
                {
                    int x=head[s];
                    printf("%d\n",num[x]);
                    head[s]=Next[x];
                    //若此时栈为空了,把tail[ ]也设置成-1(空)
                    if(head[s]==-1)
                    {
                        tail[s]=-1;
                    }
                }
            }
            else if(op==3)
            {
                int s,t;
                scanf("%d%d",&s,&t);
                
                //若第t个栈为空,就不需要再进行操作了
                if(head[t]==-1)
                    continue;
                    
                //如果第s个栈原来是空的,就把tail[s]指向第t个栈的最后一个元素
                if(head[s]==-1)
                {
                    tail[s]=tail[t];
                }
                //把两个栈进行连接
                Next[ tail[t] ] = head[s];
                head[s]=head[t];
                
                //把第t个栈设置为空
                head[t]=-1;
                tail[t]=-1;
            }
        }
    }
    return 0;
}
    


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值