题意是有至多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. 使用 dmidecode 查看Linux服务器信息

    使用 dmidecode 查看Linux服务器信息 来源  http://www.laozuo.org/6682.html 对于大部分普通服务器用户来说,我们选择VPS.服务器产品的时候比较关心的是产 ...

  2. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  3. emwin之求解窗口坐标及大小的一种方法

    @2019-01-26 [小记] 使用函数 WM_GetWindowRectEx(hItem, &Rect),坐标就存储在对象 Rect 中,可用于一些默认创建的窗口

  4. 一种导致 emwin 中 EDIT 控件不显示的情况

    @2018-12-11 [小记] 设计界面中使用了 EDIT 控件,但在其初始化语句中误使用了 text-color 属性API,导致了控件 EDIT 中的 Text 无法显示,具体如下 hItem ...

  5. [NOI2014]购票(斜率优化+线段树)

    题目描述 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路连接 ...

  6. Ubuntu18.04下给Jupyter-NoteBook设置默认工作路径(附Win设置)

    上一篇Jupyter的文章:https://www.cnblogs.com/dotnetcrazy/p/9201976.html Linux 生成配置文件:jupyter-notebook --gen ...

  7. react-native中使用长列表

    React Native 提供了几个适用于展示长列表数据的组件,一般而言我们会选用FlatList或是SectionList. FlatList组件用于显示一个垂直的滚动列表,其中的元素之间结构近似而 ...

  8. Oracle 常用的十大 DDL 对象

    table:(表) 创建表 create table test3 (tid number,tname varchar2(),hiredate date default sysdate); create ...

  9. Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)

    传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...

  10. vue $refs的基本用法

    <div id="app"> <input type="text" ref="input1"/> <butto ...