ZOJ4016 Linked List Simulation Operation


The meaning of the question: n stacks give you three operations: one is to push the stack, the other is to pop the stack, and the other is to move one stack directly to another Free up the original space
The following is the AC code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=3e5+10;
struct node{
    
    
    int num;
    node *pre,*link;
};
node *head[maxn],*tail[maxn];
int main() {
    
    
	int T;sd(T);
    while(T--){
    
    
        int n,q;
        sdd(n,q);
        rep(i,1,n){
    
    
            head[i]=new node;
            head[i]->num=-1;//判断是否为空
            head[i]->link=NULL;
            head[i]->pre=NULL;
            tail[i]=head[i];
        }
        rep(i,1,q){
    
    
            int op;sd(op);
            if(op==1){
    
    
                int s,v;
                sdd(s,v);
                node *tp=new node;
                tp->num=v;
                tp->link=NULL;
                tail[s]->link=tp;
                tp->pre=tail[s];
                tail[s]=tail[s]->link;
            }else if(op==2){
    
    
                int s;sd(s);
                if(tail[s]->num==-1){
    
    
                    printf("EMPTY\n");
                }else{
    
    
                    printf("%d\n",tail[s]->num);
                    tail[s]=tail[s]->pre;
                    delete tail[s]->link;
                    tail[s]->link=NULL;
                }
            }else if(op==3){
    
    
                int s,t;sdd(s,t);
                if(head[t]->link==NULL) continue;
                head[t]->link->pre=tail[s];
                tail[s]->link=head[t]->link;
                tail[s]=tail[t];
                head[t]->link=NULL;
                tail[t]=head[t];
            }
        }
    }
	return 0;
}

Given
nn
n initially empty stacks, there are three types of operations:
1 s v: Push the value
vv
v onto the top of the
ss
s-th stack.
2 s: Pop the topmost value out of the
ss
s-th stack, and print that value. If the
ss
s-th stack is empty, pop nothing and print “EMPTY” (without quotes) instead.
3 s t: Move every element in the
tt
t-th stack onto the top of the
ss
s-th stack in order.
Precisely speaking, denote the original size of the
ss
s-th stack by
S(s)S(s)
S(s), and the original size of the
tt
t-th stack by
S(t)S(t)
S(t). Denote the original elements in the
ss
s-th stack from bottom to top by
E(s,1),E(s,2),…,E(s,S(s))E(s,1), E(s,2), \dots, E(s,S(s))
E(s,1),E(s,2),…,E(s,S(s)), and the original elements in the
tt
t-th stack from bottom to top by
E(t,1),E(t,2),…,E(t,S(t))E(t,1), E(t,2), \dots, E(t,S(t))
E(t,1),E(t,2),…,E(t,S(t)).
After this operation, the
tt
t-th stack is emptied, and the elements in the
ss
s-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), \dots, E(s,S(s)), E(t,1), E(t,2), \dots, 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
S(t)=0, this operation actually does nothing.
There are
qq
q 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
T, indicating the number of test cases. For each test case:
The first line contains two integers
nn
n and
qq
q (
1≤n,q≤3×1051 \le n, q \le 3 \times 10^5
1≤n,q≤3×10
5
), indicating the number of stacks and the number of operations.
The first integer of the following
qq
q lines will be
opop
op (
1≤op≤31 \le op \le 3
1≤op≤3), indicating the type of operation.
If
op=1op = 1
op=1, two integers
ss
s and
vv
v (
1≤s≤n1 \le s \le n
1≤s≤n,
1≤v≤1091 \le v \le 10^9
1≤v≤10
9
) follow, indicating an operation of the first type.
If
op=2op = 2
op=2, one integer
ss
s (
1≤s≤n1 \le s \le n
1≤s≤n) follows, indicating an operation of the second type.
If
op=3op = 3
op=3, two integers
ss
s and
tt
t (
1≤s,t≤n1 \le s, t \le n
1≤s, t≤n,
s ≠ ts \ ne t
s

=t) follow, indicating an operation of the third type.
It’s guaranteed that neither the sum of
nn
n nor the sum of
qq
q over all test cases will exceed
10610^6
10
6
.
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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326150338&siteId=291194637