Time Limit: 5000MS | Memory Limit: 65536K | |
Case Time Limit: 2000MS |
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, {A1, A2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:
- 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}
- 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}
- REVOLVE x y T: rotate sub-sequence {Ax ... Ay} T times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
- 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}
- DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
- 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 n (n ≤ 100000).
The following n lines describe the sequence.
Then follows M (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
5 1 2 3 4 5 2 ADD 2 4 1 MIN 4 5
Sample Output
5
Source
#include<set>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100100;
#define fa(i) (T[i].p)
#define L(i) (T[i].s[0])
#define R(i) (T[i].s[1])
#define Key (L(R(root)))
#define Loc(i) (T[fa(i)].s[1] == i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,l,r) for(int i=l;i<=r;i++)
#define Sets(a,b,c) {if(a) T[a].s[c] = b; if(b) fa(b) = a;}
struct tnode{
int s[2],v,size,p;
int mark,Min,rev;
}T[N<<1];
int n,m,root,tot,A[N],d;
int read(){
int num = 0 , q = 1; char ch = getchar();
while(ch>'9'||ch<'0') {
if(ch=='-') q = -1;
ch = getchar();
}
while(ch>='0'&&ch<='9'){
num = num * 10 + ch - '0';
ch = getchar();
}
return num * q;
}
void Update(int i){
T[i].size = T[L(i)].size + T[R(i)].size + 1;
T[i].Min = min(T[L(i)].Min,T[R(i)].Min);
T[i].Min = min(T[i].Min,T[i].v);
}
void Pushdown(int i){
int D = T[i].mark;
if(T[i].mark){
if(L(i)) {T[L(i)].mark+=D; T[L(i)].Min+=D;T[L(i)].v+=D;}
if(R(i)) {T[R(i)].mark+=D; T[R(i)].Min+=D;T[R(i)].v+=D;}
T[i].mark = 0;
}
if(T[i].rev){
if(L(i)) T[L(i)].rev ^= 1;
if(R(i)) T[R(i)].rev ^= 1;
swap(L(i),R(i));
T[i].rev = 0;
}
}
int Rank(int kth,int i){
Pushdown(i);
if(T[L(i)].size + 1 == kth) return i;
else if(T[L(i)].size >= kth) return Rank(kth,L(i));
else return Rank(kth - T[L(i)].size - 1 , R(i));
}
void Rot(int x){
int y = fa(x) , z = fa(y);
int lx = Loc(x) , ly = Loc(y);
Sets(y,T[x].s[!lx],lx);
Sets(z,x,ly);
Sets(x,y,!lx);
Update(y);
}
void Splay(int i,int goal){
while(fa(i)!=goal){
if(fa(fa(i))!=goal) Rot(fa(i));
Rot(i);
}
Update(i);
if(!goal) root = i;
}
void Build(int l,int r,int p,int &i){
if(l>r) return;
int m = (l+r)>>1;
T[i=++tot].p = p;T[i].v = A[m]; T[i].size = 1;T[i].Min = T[i].v;
Build(l,m-1,i,L(i));Build(m+1,r,i,R(i));
Update(i);
}
char op[10];
int x,y,D;
int main(){
#ifndef ONLINE_JUDGE
freopen("super.in","r",stdin);
freopen("super.out","w",stdout);
#endif
n = read();T[0].Min = T[0].v = A[0] = A[n+1] = 2147483647;
For(i,n) A[i] = read();
Build(0,n+1,0,root);
T[Rank(n+2,root)].Min = 2147483647;
m = read();
For(i,m){
scanf("%s",&op);
if(op[0]=='A'){
x = read(); y = read(); D = read();
Splay(Rank(x,root),0);
Splay(Rank(y+2,root),root);
T[Key].v+=D;T[Key].Min+=D;T[Key].mark+=D;
Update(R(root));Update(root);
}else
if(op[0]=='M'){
x = read(); y = read();
Splay(Rank(x,root),0);
Splay(Rank(y+2,root),root);
printf("%d\n",T[Key].Min);
}else
if(op[0]=='I'){
x = read(); D = read();
Splay(Rank(x+1,root),0);
Splay(Rank(x+2,root),root);
Key = ++tot;
T[Key].p = R(root);T[Key].size = 1;
T[Key].v = T[Key].Min = D;
Update(R(root));Update(root);
}else
if(op[0]=='D'){
x = read();
Splay(Rank(x,root),0);
Splay(Rank(x+2,root),root);
T[Key].p = 0; Key = 0;
Update(R(root));Update(root);
}else
if(op[3]=='E'){
x = read(); y = read();
Splay(Rank(x,root),0);
Splay(Rank(y+2,root),root);
T[Key].rev ^= 1;
}else
if(op[3]=='O'){
x = read(); y = read(); D = read();
while(D<0) D+=(y-x+1);
D%=(y-x+1);
Splay(Rank(y-D+1,root),0);
Splay(Rank(y+2,root),root);
int tkey = Key;Key = 0;
Update(R(root));Update(root);
Splay(Rank(x,root),0);
Splay(Rank(x+1,root),root);
Sets(R(root),tkey,0);
Update(R(root));Update(root);
}
}
return 0;
}