题意是有至多150000个双端队列,400000次简单操作,直接开会导致内存超限,所以用 STL 中的 map 和 deque ,而读入过大已经在题目中有所说明,直接用已经给出的快速读入即可。要注意的是在两个队列合并时,要用 insert 函数,直接一个一个操作会超时(自己对双端队列的 STL 还是不够熟悉......)代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int N = ;
map<int, deque<int> > q;
void read(int &x){
char ch = getchar();x = ;
for (; ch < '' || ch > ''; ch = getchar());
for (; ch >='' && ch <= ''; ch = getchar()) x = x * + ch - '';
}
int main()
{
int a,n,m,u,v,w,val;
while(~scanf("%d%d",&n,&m))
{
for(int i = ; i <= n; i++)
q[i].clear();
while(m--)
{
read(a);
if(a == )
{
read(u);
read(w);
read(val);
if(w == ) q[u].push_front(val);
else if(w == ) q[u].push_back(val);
}
else if(a == )
{
read(u);
read(w);
if(q[u].empty())
{
puts("-1");
continue;
}
if(w == )
{
printf("%d\n",q[u].front());
q[u].pop_front();
}
else if(w == )
{
printf("%d\n",q[u].back());
q[u].pop_back();
}
}
else if(a == )
{
read(u);
read(v);
read(w);
if(w == )
{
q[u].insert(q[u].end(),q[v].begin(),q[v].end());
q[v].clear();
}
else if(w == )
{
q[u].insert(q[u].end(),q[v].rbegin(),q[v].rend());
q[v].clear();
}
}
} }
return ;
}

一般来说 STL 用法简单,但是其中会带有许多与题目本身无关的功能,而用时也就会多出很多,用数组去模拟一些 STL 中的标准模板可以大幅减少不必要的耗时,本题中手写的双端队列用时不到用 STL 的一半,代码如下:

 #include<stdio.h>
void read(int &x){
char ch = getchar();x = ;
for (; ch < '' || ch > ''; ch = getchar());
for (; ch >='' && ch <= ''; ch = getchar()) x = x * + ch - '';
}
struct record
{
int value;
record *next;
record *pre;
}stack1[];
int cnt;
class listqueue
{
public:
record *head,*tail;
void pop()
{
if(!empty())
{
printf("%d\n",tail->value);
tail = tail->pre;
if(tail == )
head = ;
else
tail->next = ;
}
else
puts("-1");
}
bool empty()
{
return head == ;
}
void shift()
{
if(!empty())
{
printf("%d\n",head->value);
head = head->next;
if(head == )
tail = ;
else
head->pre = ;
}
else
puts("-1");
}
void clear()
{
head = tail = ;
}
void unshift(int n)
{
stack1[cnt].value = n;
stack1[cnt].next = head;
stack1[cnt].pre = ;
if(head)
head->pre = &stack1[cnt];
head = &stack1[cnt];
if(tail == )
tail = head;
cnt++;
}
void push(int n)
{
stack1[cnt].value = n;
stack1[cnt].next = ;
stack1[cnt].pre = tail;
if(tail)
tail->next = &stack1[cnt];
tail = &stack1[cnt];
if(head == )
head = tail;
cnt++;
}
void append(listqueue &v)
{
if(v.head)
{
if(head == )
head = v.head;
else
{
tail->next = v.head;
v.head->pre = tail;
}
tail = v.tail;
v.clear();
}
}
void reverse()
{
if(empty())
return;
record *temp,*temp1;
for(temp = tail; temp != head; temp = temp->next)
{
temp1 = temp->next;
temp->next = temp->pre;
temp->pre = temp1;
}
temp1 = temp->next;
temp->next = temp->pre;
temp->pre = temp1;
head = tail;
tail = temp;
}
}rec[];
int main()
{
int n,q,u,v,w,val;
int type,i;
while(~scanf("%d%d",&n,&q))
{
cnt = ;
for(i = ; i <= n; i++)
{
rec[i].clear();
}
while(q--)
{
read(type);
if(type == )
{
read(u);
read(w);
read(val);
if(w == )
rec[u].unshift(val);
else
rec[u].push(val);
}
else if(type == )
{
read(u);
read(w);
if(w == )
rec[u].shift();
else
rec[u].pop();
}
else
{
read(u);
read(v);
read(w);
if(w == )
rec[v].reverse();
rec[u].append(rec[v]);
}
}
}
return ;
}

HDU 6375(双端队列 ~)的更多相关文章

  1. HDU 4286 Data Handler --双端队列

    题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...

  2. HDU - 6386 Age of Moyu (双端队列+bfs)

    题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...

  3. lintcode二叉树的锯齿形层次遍历 (双端队列)

    题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...

  4. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  5. STL---deque(双端队列)

    Deque是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的存储块,并且在一个映射结 ...

  6. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  7. 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41844   Accepted: 12384 ...

  8. Java 集合深入理解(10):Deque 双端队列

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 什么是 Deque Deque 是 Double ended queue (双端队列) 的缩写,读音和 deck 一样,蛋 ...

  9. BZOJ2457 BeiJing2011 双端队列

    [问题描述] Sherry现在碰到了一个棘手的问题,有N个整数需要排序.  Sherry手头能用的工具就是若干个双端队列.        她需要依次处理这N个数,对于每个数,Sherry能做以下两件事 ...

随机推荐

  1. Ionic的NavController 和ModalController 的区别

    网上也没有找到直接介绍两者区别的文章,以下都是个人感觉 区别 NavController 和 ModalController 都是打开新页面,但是NavController 是直接将页面放入到原有的页 ...

  2. docker 创建私有仓库

    由于国内访问国际网络缓慢,从DockerHub下载镜像的速度感人,所以我们有必要在本地或者自己的云服务器上搭建一套镜像仓库,提高容器下载速度使用私有仓库需修改地址:Centos 7 Docker配置: ...

  3. Hdoj 1159.Common Subsequence 题解

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  4. NOIp2018爆零记

    Day-2~Day0 考前抱佛脚,赶紧刷刷各种模板 Day 1 在开考之前打好了拍子模板,然后试题密码就发下来了(这是我前面的神仙打了\(100\)多行\(emacs\)的配置\(QAQ\)). 先按 ...

  5. FastDFS 文件上传工具类

    FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...

  6. CF1153F Serval and Bonus Problem

    Serval and Bonus Problem 1.转化为l=1,最后乘上l 2.对于一个方案,就是随便选择一个点,选在合法区间内的概率 3.对于本质相同的所有方案考虑在一起,贡献就是合法区间个数/ ...

  7. poj1958 strange towers of hanoi

    说是递推,其实也算是个DP吧. 就是4塔的汉诺塔问题. 考虑三塔:先从a挪n-1个到b,把最大的挪到c,然后再把n-1个从b挪到c,所以是 f[i] = 2 * f[i-1] + 1; 那么4塔类似: ...

  8. Django session相关操作

    Django session 是存储在数据库中的所以要先跟数据库建立连接 本连接有Django跟数据库建立连接的操作:https://www.cnblogs.com/Niuxingyu/p/10296 ...

  9. bzoj3782上学路线(Lucas+CRT+容斥DP+组合计数)

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3782 有部分分的传送门:https://www.luogu.org/problemnew/ ...

  10. 第十六节、基于ORB的特征检测和特征匹配

    之前我们已经介绍了SIFT算法,以及SURF算法,但是由于计算速度较慢的原因.人们提出了使用ORB来替代SIFT和SURF.与前两者相比,ORB有更快的速度.ORB在2011年才首次发布.在前面小节中 ...