• Mergeable Stack


    Mergeable Stack

    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

    水题
    模拟链表,删除注意消除指针

    代码如下:
    #pragma comment(linker, "/STACK:102400000,102400000")
    #include <iostream>
    #include <bits/stdc++.h>
    #define mem(a,b) memset(a,b,sizeof(a))
    typedef long long ll;
    const int INF=0x3f3f3f3f;
    const int MAXN=3*1e6+5;
    using namespace std;
    int tot;
    typedef struct Node{
        int v;
        Node* pre;
        Node* next;
    }*PNode,Node;
    struct PNod{
        PNode head,tail;
    }vis[MAXN];
    void Merge(int x,int y)
    {
        vis[x].tail->next=vis[y].head;
        vis[y].head->pre=vis[x].tail;
        vis[x].tail=vis[y].tail;
    }
    int Init(int x)
    {
        PNode p=(PNode)malloc(sizeof(PNode));
        p->v=x;p->next=p->pre=NULL;
        vis[++tot].head=vis[tot].tail=p;
        return tot;
    }
    void Insert(int x,int y)
    {
        PNode p=(PNode)malloc(sizeof(PNode));
        p->v=y;p->next=NULL;
        vis[x].tail->next=p;
        p->pre=vis[x].tail;
        vis[x].tail=p;
    }
    int Top(int x)
    {
        return vis[x].tail->v;
    }
    int Pop(int x)
    {
        PNode p=vis[x].tail;
        vis[x].tail=vis[x].tail->pre;
        delete p;
        if(vis[x].tail==NULL)
            return 0;
        return 1;
    }
    int main()
    {
        int t;
        map<int,int>my;
        scanf("%d",&t);
        while(t--)
        {
            int n,q,po,s,t,v;
            tot=0;
            my.clear();
            scanf("%d%d",&n,&q);
            while(q--)
            {
                scanf("%d",&po);
                if(po==1)
                {
                    scanf("%d%d",&s,&v);
                    if(!my[s])
                        my[s]=Init(v);
                    else
                        Insert(my[s],v);
                }
                else if(po==2)
                {
                    scanf("%d",&s);
                    if(my[s]==0)
                        puts("EMPTY");
                    else
                    {
                        printf("%d
    ",Top(my[s]));
                        if(!Pop(my[s]))
                            my.erase(s);
                    }
                }
                else
                {
                    scanf("%d%d",&s,&t);
                    if(!my[s]&&!my[t])continue;
                    else if(!my[s]&&my[t])
                    {
                        my[s]=my[t];
                        my.erase(t);
                    }
                    else if(my[s]&&!my[t])continue;
                    else
                    {
                        Merge(my[s],my[t]);
                        my.erase(t);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Java Lambda表达式初探
    解开lambda最强作用的神秘面纱
    常用正则表达式
    js,java时间处理
    Java 8新特性探究(二)深入解析默认方法
    Java 8里面lambda的最佳实践
    lambda表达式和闭包
    Lambda语法篇
    lambda表达式
    依赖注入和控制反转的理解
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/8734510.html
Copyright © 2020-2023  润新知