百度之星度度熊学队列

度度熊学队列

Accepts: 958

Submissions: 5545

Time Limit: 3000/1500 MS (Java/Others)

Memory Limit: 131072/131072 K (Java/Others)

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

Copy

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

Copy

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';
}

用listmo模拟一下就好了

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<list>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=150000+5;
list<int>a[maxn];
inline int read()
{
    int a = 0,f = 0;
    char p = getchar();
    while (!isdigit(p))
    {
        f |= p == '-';
        p = getchar();
    }
    while (isdigit(p))
    {
        a = (a << 3) + (a << 1) + (p ^ 48);
        p = getchar();
    }
    return f ? -a : a;
}
inline void out(ll x)
{
    if(x<0)
    {
        putchar('-');
        out(-x);
        return;
    }
    if(x>9)
        out(x/10);
    putchar(x%10+'0');
}

int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=1;i<=n;i++)a[i].clear();
        while(q--)
        {
            int p;
            p=read();
            if(p==1)
            {
                int u,w,v;
                u=read();
                w=read();
                v=read();
                if(w==0){a[u].push_front(v);}
                else if(w==1){a[u].push_back(v);}
            }
            if(p==2)
            {
                int u,w;
                u=read();
                w=read();
                if(a[u].empty())
                {
                    printf("-1\n");
                    continue;
                }
                if(w==0)
                {
                    out(a[u].front()),puts("");
                    a[u].pop_front();
                }
                else
                {
                    out(a[u].back()),puts("");
                    a[u].pop_back();
                }
            }
            if(p==3)
            {
                int u,v,w;
                u=read();
                v=read();
                w=read();
                if(w==0) a[u].splice(a[u].end(),a[v]);
                else if(w==1)
                {
                    reverse(a[v].begin(),a[v].end());
                    a[u].splice(a[u].end(),a[v]);
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qibage/article/details/81589211