度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣。  初始时有 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 被清空。

维护双向链表模拟, 删除的时候要注意判断删除后的结点在哪一侧.

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <math.h>
  5. #include <set>
  6. #include <map>
  7. #include <queue>
  8. #include <string>
  9. #include <string.h>
  10. #include <bitset>
  11. #define REP(i,a,n) for(int i=a;i<=n;++i)
  12. #define PER(i,a,n) for(int i=n;i>=a;--i)
  13. #define hr putchar(10)
  14. #define pb push_back
  15. #define lc (o<<1)
  16. #define rc (lc|1)
  17. #define mid ((l+r)>>1)
  18. #define ls lc,l,mid
  19. #define rs rc,mid+1,r
  20. #define x first
  21. #define y second
  22. #define io std::ios::sync_with_stdio(false)
  23. #define endl '\n'
  24. #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
  25. using namespace std;
  26. typedef long long ll;
  27. typedef pair<int,int> pii;
  28. const int P = 1e9+7, INF = 0x3f3f3f3f;
  29. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  30. ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
  31. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  32. inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
  33. //head
  34.  
  35. const int N = 1e7+10;
  36. int n, q, clk;
  37. int head[N], tail[N];
  38. struct _ {int l,r,v;} a[N];
  39. int tim[N],ti;
  40. void upd(int x) {
  41. if (tim[x]!=ti) tim[x]=ti,head[x]=tail[x]=0;
  42. }
  43. void addL(int id, int v) {
  44. upd(id);
  45. a[++clk].v = v;
  46. if (!head[id]) {
  47. head[id] = tail[id] = clk;
  48. a[clk].l = a[clk].r = -1;
  49. }
  50. else {
  51. if (a[head[id]].l==-1) {
  52. a[head[id]].l = clk;
  53. a[clk].l = -1;
  54. a[clk].r = head[id];
  55. }
  56. else {
  57. a[head[id]].r = clk;
  58. a[clk].r = -1;
  59. a[clk].l = head[id];
  60. }
  61. head[id] = clk;
  62. }
  63. }
  64. void addR(int id, int v) {
  65. upd(id);
  66. a[++clk].v = v;
  67. if (!tail[id]) {
  68. head[id] = tail[id] = clk;
  69. a[clk].l = a[clk].r = -1;
  70. }
  71. else {
  72. if (a[tail[id]].l==-1) {
  73. a[tail[id]].l = clk;
  74. a[clk].l = -1;
  75. a[clk].r = tail[id];
  76. }
  77. else {
  78. a[tail[id]].r = clk;
  79. a[clk].r = -1;
  80. a[clk].l = tail[id];
  81. }
  82. tail[id] = clk;
  83. }
  84. }
  85. void delL(int id) {
  86. upd(id);
  87. if (!head[id]) return puts("-1"),void();
  88. printf("%d\n", a[head[id]].v);
  89. if (a[head[id]].l==-1&&a[head[id]].r==-1) {
  90. head[id] = tail[id] = 0;
  91. return;
  92. }
  93. int t = head[id];
  94. if (a[t].l==-1) head[id] = a[t].r;
  95. else head[id] = a[t].l;
  96. if (a[head[id]].r==t) a[head[id]].r = -1;
  97. else a[head[id]].l = -1;
  98. }
  99. void delR(int id) {
  100. upd(id);
  101. if (!tail[id]) return puts("-1"),void();
  102. printf("%d\n", a[tail[id]].v);
  103. if (a[tail[id]].l==-1&&a[tail[id]].r==-1) {
  104. head[id] = tail[id] = 0;
  105. return;
  106. }
  107. int t = tail[id];
  108. if (a[t].l==-1) tail[id] = a[t].r;
  109. else tail[id] = a[t].l;
  110. if (a[tail[id]].l==t) a[tail[id]].l = -1;
  111. else a[tail[id]].r = -1;
  112. }
  113. void reverse(int id) {
  114. upd(id);
  115. swap(head[id],tail[id]);
  116. }
  117. void merge(int x, int y) {
  118. if (x==y) return;
  119. upd(x),upd(y);
  120. if (!head[y]) return;
  121. if (!head[x]) {
  122. head[x] = head[y];
  123. tail[x] = tail[y];
  124. }
  125. else {
  126. if (a[tail[x]].l==-1) a[tail[x]].l = head[y];
  127. else a[tail[x]].r = head[y];
  128. if (a[head[y]].l==-1) a[head[y]].l = tail[x];
  129. else a[head[y]].r = tail[x];
  130. tail[x] = tail[y];
  131. }
  132. head[y] = tail[y] = 0;
  133. }
  134.  
  135. void read(int &x){
  136. scanf("%d",&x);
  137. }
  138.  
  139. int main() {
  140. while (~scanf("%d%d", &n, &q)) {
  141. ++ti;
  142. while (q--) {
  143. int op,u,v,w;
  144. read(op),read(u),read(v);
  145. if (op==1) {
  146. read(w);
  147. if (v==0) addL(u,w);
  148. else addR(u,w);
  149. }
  150. else if (op==2) {
  151. if (v==0) delL(u);
  152. else delR(u);
  153. }
  154. else {
  155. read(w);
  156. if (w) reverse(v);
  157. merge(u,v);
  158. }
  159. }
  160. }
  161. }

hdu 6375 度度熊学队列 (链表模拟)的更多相关文章

  1. hdu6375 度度熊学队列

    度度熊学队列 题目传送门 解题思路 STL大法好.直接用deque,但是N的范围很大,如果直接开那么多的deque会爆内存,所以用map< int, deque< int>>, ...

  2. hdu 6375 百度之星 度度熊学队列

    题目链接 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 ...

  3. 【2018百度之星初赛(A)】1002 度度熊学队列

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6375 Knowledge Point: STL - map:https://www.cnblogs.c ...

  4. 2018 “百度之星”程序设计大赛 - 初赛(A)度度熊学队列 list rope

    c++ list使用 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstr ...

  5. 2018百度之星初赛(A)2 度度熊学队列

    思路: 记录一下c++ stl中的双向链表list的各种用法. https://blog.csdn.net/fanyun_01/article/details/56881515 实现: #includ ...

  6. 2018百度之星初赛A轮 度度熊学队列

    注意:刚开始用数组存deque<int> qa[MAX]会爆内存 需要改用map<int, deque<int> > qa优化 不明觉厉 #include<b ...

  7. hdu 6118 度度熊的交易计划

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. HDU 6118 度度熊的交易计划 【最小费用最大流】 (2017"百度之星"程序设计大赛 - 初赛(B))

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. 个人学习分布式专题(二)分布式服务治理之Dubbo框架

    目录 Dubbo框架 1.1 Dubbo是什么 1.2 Dubbo企业级应用示例(略) 1.3 Dubbo实现原理及架构剖析 1.4 Dubbo+Spring集成 Dubbo框架 1.1 Dubbo是 ...

  2. CodeForces - 1183E Subsequences (easy version) (字符串bfs)

    The only difference between the easy and the hard versions is constraints. A subsequence is a string ...

  3. DELPHI10.3.2安卓SDK安装

    DELPHI10.3.2安卓SDK安装 DELPHI10.3.2默认安装以后,还需要安装安卓SDK,才可以编译安卓项目. 1)运行Android Tools 2)勾选安装下面几个

  4. javascript已存在的对象构造器中是不能添加新的属性的:

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鸟教程 ...

  5. 【转载】 AutoML总结

    原文地址: https://jinxin0924.github.io/2017/12/21/AutoML%E6%80%BB%E7%BB%93/ Posted by JxKing on December ...

  6. ISO/IEC 9899:2011 条款5——5.1.1 翻译环境

    5.1.1 翻译环境 5.1.1.1程序结构 1.一个C程序不需要一次全被翻译完.程序的文本被保存在本国际标准中被称作为源文件(或预处理文件)的单元里.一个源文件连同所有通过指示符#include所包 ...

  7. Django使用request和response对象

    当请求一张页面时,Django把请求的metadata数据包装成一个HttpRequest对象,然后Django加载合适的view方法,把这个HttpRequest 对象作为第一个参数传给view方法 ...

  8. 学习 TTreeView [2] - Items.Item[i]、Items[i]、.Text、SetFocus(设置焦点)、Select(选择)

    本例效果图: 源码: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...

  9. 配置Apache运行在event事件驱动模式下

    (1)启用MPM Include conf/extra/httpd-mpm.conf (2)配置evnet MPM参数  <IfModule event.c> #default 3 Ser ...

  10. nginx conf 文件

    server { listen ; server_name local.light.com; index index.html index.htm index.php; root /home/wwwr ...