模拟题,题意:
给你三种操作
1.将v压入第s个队列
2.弹出s队列最后一个,s为空的话输出empty
3.将t队列放到s队列后面
一开始没想到操作三的splice,t了
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
vector<list<int> >p;
inline int read()
{
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int main()
{
int T;
T=read();
int n,m,op;
//std::list<int>::iterator it;
while(T--)
{
n=read();
m=read();
p.clear();
p.resize(n+1);
int s,v,t;
while(m--)
{
op=read();
if(op==1)
{
s=read();v=read();
p[s].push_back(v);
}
else if(op==2)
{
s=read();
if(p[s].empty()) printf("EMPTY\n");
else printf("%d\n",p[s].back()),p[s].pop_back();
}
else if(op==3)
{
s=read();t=read();
p[s].splice(p[s].end(),p[t]);
}
}
}
return 0;
}
splice的操作式是将t队列放到s队列后面,并且t队列置空;
类似的还有merge,但是前提是有序i,得到的序列也将是有序的
还有insert,但是插入的效率相对而言比较低,因为list是链表;