zoj4016 Mergeable Stack

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

這道題T了好久…
一開始用的vector,T了;瞄了一眼題解,改成list,又T了;仔細看了下題解,於是發現了個牛逼的函數splice(),可以把一個list1插入到另一個list2的任意位置,並且清空原來的list1,但改完還是T了……;最後發現是cin和cout惹的鍋啊…輸入輸出速度太慢了,大量IO時注意不要用cin和cout啊!

#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=3e5+5;
list<int> lis[maxn];
int main()
{
    int t,n,q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            lis[i].clear();
        while(q--)
        {
            int op,num,num2,x;
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d",&num,&x);
                lis[num].push_back(x);
            }
            else if(op==2)
            {
                scanf("%d",&num);
                if(lis[num].empty())
                    printf("EMPTY\n");
                else
                {
                    printf("%d\n",lis[num].back());
                    lis[num].pop_back();
                }
            }
            else if(op==3)
            {
                scanf("%d%d",&num,&num2);
                lis[num].splice(lis[num].end(),lis[num2]);//該函數將list2插入list1中並清空list2
            }
        }
    }
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章