Description
Given nn initially empty stacks, there are three types of operations:
1 sv: Push the value vv onto the top of the ss-th stack.
2 s: Pop the topmost value out of the ss-th stack, and print that value. If the ss-th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.
3 st: Move every element in the tt-th stack onto the top of the ss-th stack in order.
Precisely speaking, denote the original size of the ss-th stack by S(s)S(s), and the original size of the tt-th stack by S(t)S(t). Denote the original elements in the ss-th stack from bottom to top by E(s,1),E(s,2),…,E(s,S(s))E(s,1),E(s,2),…,E(s,S(s)), and the original elements in the tt-th stack from bottom to top by E(t,1),E(t,2),…,E(t,S(t))E(t,1),E(t,2),…,E(t,S(t)).
After this operation, the tt-th stack is emptied, and the elements in the ss-th stack from bottom to top becomes E(s,1),E(s,2),…,E(s,S(s)),E(t,1),E(t,2),…,E(t,S(t))E(s,1),E(s,2),…,E(s,S(s)),E(t,1),E(t,2),…,E(t,S(t)). Of course, if S(t)=0S(t)=0, this operation actually does nothing.
There are qq 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 TT, indicating the number of test cases. For each test case:
The first line contains two integers nn and qq (1≤n,q≤3×1051≤n,q≤3×105), indicating the number of stacks and the number of operations.
The first integer of the following qq lines will be opop (1≤op≤31≤op≤3), indicating the type of operation.
- If op=1op=1, two integers ss and vv (1≤s≤n1≤s≤n, 1≤v≤1091≤v≤109) follow, indicating an operation of the first type.
- If op=2op=2, one integer ss (1≤s≤n1≤s≤n) follows, indicating an operation of the second type.
- If op=3op=3, two integers ss and tt (1≤s,t≤n1≤s,t≤n, s≠ts≠t) follow, indicating an operation of the third type.
It's guaranteed that neither the sum of nn nor the sum of qq over all test cases will exceed 106106.
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 3Sample Output
13 12 11 10 EMPTY 14 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
题意:
1-对于第s个栈,在栈顶压入一个元素v
2-输出第s个栈的栈顶元素,为空则输出empty
3-合并第s、t个栈
我们用了stack刚开始,然后TLE了,原来其实用list容器做是最好的!!!第一次遇见,学习了~
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
//const int maxn = 1e6 + 5;
typedef long long ll;
const int maxn = 3e5+5;
list<int> l[maxn];
int main()
{
int n,t,q;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
l[i].clear();
while(q--)
{
int op,s,v,t;
scanf("%d",&op);
if(op==1)//对于第s个栈在栈顶压入一个元素v
{
scanf("%d%d",&s,&v);
l[s].push_back(v);
}
else if(op==2)//输出第s个栈的栈顶元素,为空则输出empty
{
scanf("%d",&s);
if(l[s].empty()) printf("EMPTY\n");
else
{
printf("%d\n",l[s].back());
l[s].pop_back();
}
}
else if(op==3)//合并第s、t个栈
{
scanf("%d%d",&s,&t);
l[s].splice(l[s].end(),l[t]); //splice 直接进行合并不进行排序,merge默认升序合并排序
}
}
}
return 0;
}