度度熊学队列
- -
Problem Description
度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣。
初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作。
①1 u w val 在编号为 u 的队列里加入一个权值为 val 的元素。(w=0 表示加在最前面,w=1 表示加在最后面)。
②2 u w 询问编号为 u 的队列里的某个元素并删除它。( w=0 表示询问并操作最前面的元素,w=1 表示最后面)
③3 u v w 把编号为 v 的队列“接在”编号为 u 的队列的最后面。w=0 表示顺序接(队列 v 的开头和队列 u 的结尾连在一起,队列v 的结尾作为新队列的结尾), w=1 表示逆序接(先将队列 v 翻转,再顺序接在队列 u 后面)。且该操作完成后,队列 v 被清空。
Input
有多组数据。
对于每一组数据,第一行读入两个数 N 和 Q。
接下来有 Q 行,每行 3~4 个数,意义如上。
N≤150000,Q≤400000
1≤u,v≤N,0≤w≤1,1≤val≤100000
所有数据里 Q 的和不超过500000
Output
对于每组数据的每一个操作②,输出一行表示答案。
注意,如果操作②的队列是空的,就输出−1且不执行删除操作。
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’;
}
由于N很大,单用deque会MLE,因此用map和deque就可以过了~
#include<deque>
#include<map>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
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';
}
map<int,deque<int> >mp;
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
mp.clear();
int x,u,v,w,val;
while(q--)
{
read(x);
if(x==1)
{
read(u);read(w);read(val);
if(w)
mp[u].push_back(val);
else
mp[u].push_front(val);
}
else if(x==2)
{
read(u);read(w);
if(mp[u].size())
{
int g;
if(w)
{
g=mp[u].back();
mp[u].pop_back();
}
else
{
g=mp[u].front();
mp[u].pop_front();
}
cout<<g<<endl;
}
else
cout<<"-1"<<endl;
}
else
{
read(u);read(v);read(w);
if(w)
mp[u].insert(mp[u].end(),mp[v].rbegin(),mp[v].rend());
else
mp[u].insert(mp[u].end(),mp[v].begin(),mp[v].end());
mp[v].clear();
}
}
}
return 0;
}
2018

被折叠的 条评论
为什么被折叠?



