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


ADD
MIN

Sample Output


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的初始值)。
#include<cstdio>
#include<iostream>
#define INF 0x3f3f3f3f
#define Key_value ch[ch[root][1]][0]
using namespace std;
bool rev[];
int n,tot,root,a[],key[],add[],pre[],ch[][],size[],minn[];
inline void Update_Add(int r,int d)
{
if(!r)return;
add[r]+=d;
key[r]+=d;
minn[r]+=d;
}
inline void Update_Reve(int r)
{
if(!r)return;
rev[r]^=;
swap(ch[r][],ch[r][]);
}
inline void Push_Up(int k)
{
minn[k]=min(key[k],min(minn[ch[k][]],minn[ch[k][]]));
size[k]=size[ch[k][]]+size[ch[k][]]+;
}
inline void Push_Down(int r)
{
if(add[r]){
Update_Add(ch[r][],add[r]);
Update_Add(ch[r][],add[r]);
add[r]=;
}
if(rev[r]){
Update_Reve(ch[r][]);
Update_Reve(ch[r][]);
rev[r]=;
}
}
inline int Get_Kth(int r,int k)
{
Push_Down(r);
int t=size[ch[r][]]+;
if(t==k)return r;
if(t>k)return Get_Kth(ch[r][],k);
else return Get_Kth(ch[r][],k-t);
}
inline void NewNode(int &r,int father,int val)
{
r=++tot;
size[r]=;
pre[r]=father;
key[r]=minn[r]=val;
}
inline void Build(int &x,int father,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
NewNode(x,father,a[mid]);
Build(ch[x][],x,l,mid-);
Build(ch[x][],x,mid+,r);
Push_Up(x);
}
inline void Init()
{
root=tot=;
minn[root]=INF;
ch[root][]=ch[root][]=pre[root]=add[root]=rev[root]=size[root]=;
NewNode(root,,INF);
NewNode(ch[root][],root,INF);
Build(Key_value,ch[root][],,n);
}
inline void Rotate(int x,int kind)
{
int y=pre[x];
Push_Down(y);
Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
inline void Splay(int r,int goal)
{
Push_Down(r);
while(pre[r]!=goal){
if(pre[pre[r]]==goal){
Push_Down(pre[r]);
Push_Down(r);
Rotate(r,ch[pre[r]][]==r);
}
else{
int y=pre[r];
int kind=ch[pre[y]][]==y;
Push_Down(pre[y]);
Push_Down(y);
Push_Down(r);
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}
else{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(!goal)root=r;
}
inline void Add(int l,int r,int d)
{
Splay(Get_Kth(root,l),);//调用Get_Kth()首参数要用root因为root可能变成区间内其他数;
Splay(Get_Kth(root,r+),root);
Update_Add(Key_value,d);
Push_Up(ch[root][]);
Push_Up(root);
}
inline int Min(int l,int r)
{
Splay(Get_Kth(root,l),);//ADD注释+1;
Splay(Get_Kth(root,r+),root);
return minn[Key_value];
}
inline void Del(int x)
{
Splay(Get_Kth(root,x),);
Splay(Get_Kth(root,x+),root);
pre[Key_value]=;
Key_value=;//直接清零即可;
Push_Up(ch[root][]);
Push_Up(root);
}
inline void Reve(int l,int r)
{
Splay(Get_Kth(root,l),);
Splay(Get_Kth(root,r+),root);
Update_Reve(Key_value);
Push_Up(ch[root][]);
Push_Up(root);
}
inline void Revo(int a,int b,int t)
{
int c=b-t;//将区间a~b分为两个区间a~b-c、b-c+1~b,将区间2旋转到区间1前;
Splay(Get_Kth(root,a),);
Splay(Get_Kth(root,c+),root);
int tmp=Key_value;
Key_value=;
Push_Up(ch[root][]);
Push_Up(root);
Splay(Get_Kth(root,b-c+a),);
Splay(Get_Kth(root,b-c+a+),root);
Key_value=tmp;
pre[Key_value]=ch[root][];
Push_Up(ch[root][]);
Push_Up(root);
}
inline void Ins(int x,int t)
{
Splay(Get_Kth(root,x+),);
Splay(Get_Kth(root,x+),root);
NewNode(Key_value,ch[root][],t);
Push_Up(ch[root][]);
Push_Up(root);
}
int main()
{
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++)scanf("%d",&a[i]);
Init();
int Case;
scanf("%d",&Case);
for(;Case;Case--){
char order[];
scanf("%s",order);
if(order[]=='A'){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Add(x,y,z);
}
if(order[]=='M'){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",Min(x,y));
}
if(order[]=='D'){
int x;
scanf("%d",&x);
Del(x);
}
if(order[]=='I'){
int x,t;
scanf("%d%d",&x,&t);
Ins(x,t);
}
if(order[]=='R'&&order[]=='E'){
int x,y;
scanf("%d%d",&x,&y);
Reve(x,y);
}
if(order[]=='R'&&order[]=='O'){
int x,y,T;
scanf("%d%d%d",&x,&y,&T);
Revo(x,y,(T%(y-x+)+y-x+)%(y-x+));
}
}
}
return ;
}

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. 关于web软件信息安全问题资料的整理(四)

    整理出了几点解决方案 1.修护漏洞.对于防护的一方来看,如果先于攻击一方发现Web系统中存在的漏洞,尽早修复它们,就可以防患于未然,获得最低的防护成本.漏洞的修复方式并不是一定要依靠修改网页代码才可以 ...

  2. Windows 上的 Jetty 小工具

    做项目经常遇到需要开发Java应用,我喜欢用Jetty进行开发.部署,主要是由于Jetty的轻量级. Jetty 项目主页:http://www.eclipse.org/jetty/, 最新版9.30 ...

  3. Android开发学习——搭建开发环境

    在学校开课学习了android的一些简单的UI组件,布局,四大组件学习了2个,数据存储及网络通信,都是一些简单的概念,入门而已.许多东西需要自己去学习. 学习一下 Android开发环境的搭建,两种方 ...

  4. Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  5. iPhone 信号格转变数字

    ?在拨号键输入*3001#12345#* ?按呼叫键,就会进入Field Test页面 ?你就会看到信号格变为了数字,然而只要按home键返回就又会变为信号格 ?如果想一直变为数字的话,在Field ...

  6. Swift-Switch穿透

    switch vegetable {        case "celery":            print("Add some raisins and make ...

  7. Tomcat:云环境下的Tomcat设计思路——Tomcat的多实例安装

    Cloud现在是一个热门的技术,Tomcat是学习Java的人一般都会接触的Web服务器,如果在Cloud环境下使用Tomcat,又当如何呢?不可避免的,要安装多个Tomcat了,这里称之为Tomca ...

  8. Mysql查询阻塞初探

    第一次值班,报警打电话给我说,数据库复制延时一个多小时,那个时候是半夜啊,但我还是很清醒的起来,开机.vpn.登录.show processlist,结果发现情况是这样的: 红线框表示的是当前每个线程 ...

  9. jQuery简单入门(二)

    2.Dom操作 A.DOM分类 个人认为在jQuery中这些分类被弱化了,有兴趣的读者可以自行补充这方面的知识: aa.DOM Core bb.HTML -DOM cc. CSS-DOM B.jQue ...

  10. Hadoop+MongoDB的四种方案

    背景: 公司核心业务库现存在MongoDB中,分布在6台MongoDB节点.现面临如下问题: 1.最大的一张表有10多个G,MongoDB在查询方面尚能胜任,但是涉及到复杂计算时会比较吃力. 2.Mo ...