题意是有至多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. 直接使用security.basic.path无效|——springboot2.0以上的security的配置

    问题 springcloud 版本 为 Finchley.RELEASEspringboot 版本为 2.0.3.RELEASE 现在有需求,/swagger-ui.html 页面需要添加登录认证,但 ...

  2. 对如下字符串(234453)[234]{2324}分析它的括号使用是否正确,括号匹配(Java实现)

    我的一种思路是递归: private static String s = "(2344[)]53[234]{2324}"; private static boolean f(int ...

  3. MT【303】估计

    (2016浙江填空压轴题)已知实数$a,b,c$则 (     )A.若$|a^2+b+c|+|a+b^2+c|\le1,$则$a^2+b^2+c^2<100$B.若$|a^2+b+c|+|a+ ...

  4. MT【301】值域宽度

    (2015浙江理科)已知函数$f(x)=x^2+ax+b,(a,b\in R)$.记$M(a,b)$是$|f(x)|$在区间$[-1,1]$上的最大值.(1)证明:当$|a|\ge2$时,$M(a,b ...

  5. 20165223《JAVA程序设计》第一周学习总结

    20165223 <JAVA程序设计>第一周学习总结 教材学习内容总结 通过网站JAVA第一章视频教程.教材.老师所给的教程及网上查询进行学习 第一章要点 JAVA地位和特点 地位:网络. ...

  6. 区块链使用Java,以太坊 Ethereum, web3j, Spring Boot

    Blockchain is one of the buzzwords in IT world during some last months. This term is related to cryp ...

  7. 基于Jenkins,docker实现自动化部署(持续交互)

      前言 随着业务的增长,需求也开始增多,每个需求的大小,开发周期,发布时间都不一致.基于微服务的系统架构,功能的叠加,对应的服务的数量也在增加,大小功能的快速迭代,更加要求部署的快速化,智能化.因此 ...

  8. 登录rabbitmq报错User can only log in via localhost

    在访问管理界面使用guest用户登录时出现login failed错误. 到服务器上查询日志显示出现错误的原因是:HTTP access denied: user ‘guest’ - User can ...

  9. R: 修改镜像、bioconductor安装及go基因富集分析

    1.安装bioconductor及go分析涉及的相关包 source("http://bioconductor.org/biocLite.R") options(BioC_mirr ...

  10. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...