Description

Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1A2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:

  1. ADD x y D: Add D to each number in sub-sequence {Ax ... Ay}. For example, performing "ADD 2 4 1" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5, 5}
  2. REVERSE x y: reverse the sub-sequence {Ax ... Ay}. For example, performing "REVERSE 2 4" on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
  3. REVOLVE x y T: rotate sub-sequence {Ax ... AyT times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
  4. INSERT x P: insert P after Ax. For example, performing "INSERT 2 4" on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
  5. DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
  6. MIN x y: query the participant what is the minimum number in sub-sequence {Ax ... Ay}. For example, the correct answer to "MIN 2 4" on {1, 2, 3, 4, 5} is 2

To make the show more interesting, the participant is granted a chance to turn to someone else that means when Jackson feels difficult in answering a query he may call you for help. You task is to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson whenever he calls.

Input

The first line contains (≤ 100000).

The following n lines describe the sequence.

Then follows M (≤ 100000), the numbers of operations and queries.

The following M lines describe the operations and queries.

Output

For each "MIN" query, output the correct answer.

Sample Input

  1.  
  2. ADD
  3. MIN

Sample Output

  1.  

Source

POJ Founder Monthly Contest – 2008.04.13, Yao Jinyu
一道splay模板题。要求你设计一个数据结构实现以下操作:
1)Add(l,r,d)向区间l~r中的元素加上一个数d;
2)Reverse(l,r)翻转区间l~r的元素;
3)Revolve(l,r,T)将区间l~r的后缀放到区间的前方,操作T次;
4)Insert(x,P)将元素P插入到元素x后方;
5)Delete(x)删除元素x;
6)Min(l,r)求区间l~r中的最小值;
因为操作和题目需要,我们建立三个虚根,0、root(在NewNode操作后,root初始值为1)和root的右孩子(即ch[root][1]),一个实根,root的右孩子的左孩子(即ch[ch[root][1]][0],Key_value的初始值)。
  1. #include<cstdio>
  2. #include<iostream>
  3. #define INF 0x3f3f3f3f
  4. #define Key_value ch[ch[root][1]][0]
  5. using namespace std;
  6. bool rev[];
  7. int n,tot,root,a[],key[],add[],pre[],ch[][],size[],minn[];
  8. inline void Update_Add(int r,int d)
  9. {
  10. if(!r)return;
  11. add[r]+=d;
  12. key[r]+=d;
  13. minn[r]+=d;
  14. }
  15. inline void Update_Reve(int r)
  16. {
  17. if(!r)return;
  18. rev[r]^=;
  19. swap(ch[r][],ch[r][]);
  20. }
  21. inline void Push_Up(int k)
  22. {
  23. minn[k]=min(key[k],min(minn[ch[k][]],minn[ch[k][]]));
  24. size[k]=size[ch[k][]]+size[ch[k][]]+;
  25. }
  26. inline void Push_Down(int r)
  27. {
  28. if(add[r]){
  29. Update_Add(ch[r][],add[r]);
  30. Update_Add(ch[r][],add[r]);
  31. add[r]=;
  32. }
  33. if(rev[r]){
  34. Update_Reve(ch[r][]);
  35. Update_Reve(ch[r][]);
  36. rev[r]=;
  37. }
  38. }
  39. inline int Get_Kth(int r,int k)
  40. {
  41. Push_Down(r);
  42. int t=size[ch[r][]]+;
  43. if(t==k)return r;
  44. if(t>k)return Get_Kth(ch[r][],k);
  45. else return Get_Kth(ch[r][],k-t);
  46. }
  47. inline void NewNode(int &r,int father,int val)
  48. {
  49. r=++tot;
  50. size[r]=;
  51. pre[r]=father;
  52. key[r]=minn[r]=val;
  53. }
  54. inline void Build(int &x,int father,int l,int r)
  55. {
  56. if(l>r)return;
  57. int mid=(l+r)>>;
  58. NewNode(x,father,a[mid]);
  59. Build(ch[x][],x,l,mid-);
  60. Build(ch[x][],x,mid+,r);
  61. Push_Up(x);
  62. }
  63. inline void Init()
  64. {
  65. root=tot=;
  66. minn[root]=INF;
  67. ch[root][]=ch[root][]=pre[root]=add[root]=rev[root]=size[root]=;
  68. NewNode(root,,INF);
  69. NewNode(ch[root][],root,INF);
  70. Build(Key_value,ch[root][],,n);
  71. }
  72. inline void Rotate(int x,int kind)
  73. {
  74. int y=pre[x];
  75. Push_Down(y);
  76. Push_Down(x);
  77. ch[y][!kind]=ch[x][kind];
  78. pre[ch[x][kind]]=y;
  79. if(pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;
  80. pre[x]=pre[y];
  81. ch[x][kind]=y;
  82. pre[y]=x;
  83. Push_Up(y);
  84. }
  85. inline void Splay(int r,int goal)
  86. {
  87. Push_Down(r);
  88. while(pre[r]!=goal){
  89. if(pre[pre[r]]==goal){
  90. Push_Down(pre[r]);
  91. Push_Down(r);
  92. Rotate(r,ch[pre[r]][]==r);
  93. }
  94. else{
  95. int y=pre[r];
  96. int kind=ch[pre[y]][]==y;
  97. Push_Down(pre[y]);
  98. Push_Down(y);
  99. Push_Down(r);
  100. if(ch[y][kind]==r){
  101. Rotate(r,!kind);
  102. Rotate(r,kind);
  103. }
  104. else{
  105. Rotate(y,kind);
  106. Rotate(r,kind);
  107. }
  108. }
  109. }
  110. Push_Up(r);
  111. if(!goal)root=r;
  112. }
  113. inline void Add(int l,int r,int d)
  114. {
  115. Splay(Get_Kth(root,l),);//调用Get_Kth()首参数要用root因为root可能变成区间内其他数;
  116. Splay(Get_Kth(root,r+),root);
  117. Update_Add(Key_value,d);
  118. Push_Up(ch[root][]);
  119. Push_Up(root);
  120. }
  121. inline int Min(int l,int r)
  122. {
  123. Splay(Get_Kth(root,l),);//ADD注释+1;
  124. Splay(Get_Kth(root,r+),root);
  125. return minn[Key_value];
  126. }
  127. inline void Del(int x)
  128. {
  129. Splay(Get_Kth(root,x),);
  130. Splay(Get_Kth(root,x+),root);
  131. pre[Key_value]=;
  132. Key_value=;//直接清零即可;
  133. Push_Up(ch[root][]);
  134. Push_Up(root);
  135. }
  136. inline void Reve(int l,int r)
  137. {
  138. Splay(Get_Kth(root,l),);
  139. Splay(Get_Kth(root,r+),root);
  140. Update_Reve(Key_value);
  141. Push_Up(ch[root][]);
  142. Push_Up(root);
  143. }
  144. inline void Revo(int a,int b,int t)
  145. {
  146. int c=b-t;//将区间a~b分为两个区间a~b-c、b-c+1~b,将区间2旋转到区间1前;
  147. Splay(Get_Kth(root,a),);
  148. Splay(Get_Kth(root,c+),root);
  149. int tmp=Key_value;
  150. Key_value=;
  151. Push_Up(ch[root][]);
  152. Push_Up(root);
  153. Splay(Get_Kth(root,b-c+a),);
  154. Splay(Get_Kth(root,b-c+a+),root);
  155. Key_value=tmp;
  156. pre[Key_value]=ch[root][];
  157. Push_Up(ch[root][]);
  158. Push_Up(root);
  159. }
  160. inline void Ins(int x,int t)
  161. {
  162. Splay(Get_Kth(root,x+),);
  163. Splay(Get_Kth(root,x+),root);
  164. NewNode(Key_value,ch[root][],t);
  165. Push_Up(ch[root][]);
  166. Push_Up(root);
  167. }
  168. int main()
  169. {
  170. while(scanf("%d",&n)!=EOF){
  171. for(int i=;i<=n;i++)scanf("%d",&a[i]);
  172. Init();
  173. int Case;
  174. scanf("%d",&Case);
  175. for(;Case;Case--){
  176. char order[];
  177. scanf("%s",order);
  178. if(order[]=='A'){
  179. int x,y,z;
  180. scanf("%d%d%d",&x,&y,&z);
  181. Add(x,y,z);
  182. }
  183. if(order[]=='M'){
  184. int x,y;
  185. scanf("%d%d",&x,&y);
  186. printf("%d\n",Min(x,y));
  187. }
  188. if(order[]=='D'){
  189. int x;
  190. scanf("%d",&x);
  191. Del(x);
  192. }
  193. if(order[]=='I'){
  194. int x,t;
  195. scanf("%d%d",&x,&t);
  196. Ins(x,t);
  197. }
  198. if(order[]=='R'&&order[]=='E'){
  199. int x,y;
  200. scanf("%d%d",&x,&y);
  201. Reve(x,y);
  202. }
  203. if(order[]=='R'&&order[]=='O'){
  204. int x,y,T;
  205. scanf("%d%d%d",&x,&y,&T);
  206. Revo(x,y,(T%(y-x+)+y-x+)%(y-x+));
  207. }
  208. }
  209. }
  210. return ;
  211. }

poj2580 Super Memmo的更多相关文章

  1. 子类继承父类时JVM报出Error:Implicit super constructor People() is undefined for default constructor. Must define an explicit constructor

    当子类继承父类的时候,若父类没有定义带参的构造方法,则子类可以继承父类的默认构造方法 当父类中定义了带参的构造方法,子类必须显式的调用父类的构造方法 若此时,子类还想调用父类的默认构造方法,必须在父类 ...

  2. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. Maven Super POM

    Maven super POM defines some properties. Three ways to find it ${M2_HOME}/lib/maven-model-builder-3. ...

  4. java基础 super 子类调用父类

    如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 example如下: package test; /* * 如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 * */ ...

  5. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...

  6. java方法重载(overload)、重写(override);this、super关键简介

    一.方法重载: 条件:必须在一个类中,方法名称相同,参数列表不同(包括:数据类型.顺序.个数),典型案例构 造方重载.  注意:与返回值无关 二.方法重写: 条件: (1)继承某个类或实现某接口 (2 ...

  7. Java super关键字活用

    在实际开发中我们要自定义组件,就需要继承自某个组件类,如果我们自定义的这个组件类也需要像被继承的这个组件类一样,拥有丰富的构造方法. 关键字super的作用就更加显得尤为重要了,你可以在堆砌自己自定义 ...

  8. 深入super,看Python如何解决钻石继承难题 【转】

    原文地址 http://www.cnblogs.com/testview/p/4651198.html 1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通 ...

  9. 关于[super dealloc]

    销毁一个对象时,需要重写系统的dealloc方法来释放当前类所拥有的对象,在dealloc方法中需要先释放当前类中所有的对象,然后再调用[super dealloc]释放父类中所拥有的对象.如先调用[ ...

随机推荐

  1. SharePoint 2013 场解决方案包含第三方程序集

    前言 当我们使用SharePoint 场解决方案的时候,经常会包含第三方的程序集,而第三方的程序集经常会有强签名的问题,如果有强签名可以部署到GAC,没有的话也可以部署到应用程序下. 那么,很多初学者 ...

  2. Android实现登录

    登录界面布局文件         1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  3. linker command failed with exit code 1 (use -v to see invocation)解决办法

    [cpp] view plaincopy Undefined symbols for architecture i386:     "_OBJC_CLASS_$_FMDatabase&quo ...

  4. Java 线程池

    系统启动一个线程的成本是比较高的,因为它涉及到与操作系统的交互,使用线程池的好处是提高性能,当系统中包含大量并发的线程时,会导致系统性能剧烈下降,甚至导致JVM崩溃,而线程池的最大线程数参数可以控制系 ...

  5. (原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 决策树分析算法)

    随着大数据时代的到来,数据挖掘的重要性就变得显而易见,几种作为最低层的简单的数据挖掘算法,现在利用微软数据案例库做一个简要总结. 应用场景介绍 其实数据挖掘应用的场景无处不在,很多的环境都会应用到数据 ...

  6. mysql 触发器的创建 修改 删除

    //做一个简单的练习,创建一个简单的触发器 完成添加文章的时候,自动加上时间,默认作者 为 ‘日记本的回忆‘ show columns from test; //查看表结构 //查看已存在触发器 sh ...

  7. HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较

    摘要:   在前篇博客里已经讲述了通过一个自定义 HBase Filter来获取数据的办法,在末尾指出此办法的性能是不能满足应用要求的,很显然对于如此成熟的HBase来说,高性能获取数据应该不是问题. ...

  8. YUV420查表法高效、无失真的转换为RGB32格式

    YUV格式有两大类:planar和packed.planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V,这里所讲述的就是这中存储格式的:packed的YUV ...

  9. 烂泥:php5.6源码安装及php-fpm配置与nginx集成

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. LNMP环境的搭建中,现在只有php没有源码安装过.这篇文章就把这个介绍下. 注意本篇文章使用的centos 6.5 64bit. 登陆centos下载 ...

  10. iOS 读取相册二维码,兼容ios7(使用CIDetector 和 ZXingObjC)

    ios从相册读取二维码,在ios8以上,苹果提供了自带的识别图片二维码的功能,这种方式效率最好,也是最推荐的,但是如果你的系统需要向下兼容ios7,就必须用其他方式. 这里我选择的是 ZXingObj ...