Sample Input
2 10 1 1 1 23 1 1 0 233 2 1 1 1 2 1 2333 1 2 1 23333 3 1 2 1 2 2 0 2 1 1 2 1 0 2 1 1
Sample Output
23 -1 2333 233 23333 提示 由于读入过大,C/C++ 选手建议使用读入优化。 一个简单的例子: void read(int &x){ char ch = getchar();x = 0; for (; ch < '0' || ch > '9'; ch = getchar()); for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; }
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<functional>
#include<deque>
using namespace std;
typedef long long ll;
int n,q1;
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main(){
ll u,v,w,val,op;
while(~scanf("%d%d",&n,&q1)){
deque<ll> q[n+1];
while(q1--){
op=read();
if(op==1){
u=read();
w=read();
val=read();
if(w==0){
q[u].push_front(val);
}else q[u].push_back(val);
}else if(op==2){
u=read();
w=read();
if(q[u].empty()) printf("-1\n");
else{
if(w==0){
printf("%lld\n",q[u].front());
q[u].pop_front();
}else{
printf("%lld\n",q[u].back());
q[u].pop_back();
}
}
}else{
u=read();
v=read();
w=read();
if(w==0){
while(!q[v].empty()){
q[u].push_back(q[v].front());
q[v].pop_front();
}
}else{
while(!q[v].empty()){
q[u].push_back(q[v].back());
q[v].pop_back();
}
}
}
}
}
return 0;
}