RMQ with Shifts
In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], …, A[R]. Note that the indices start from 1, i.e. the left-most element is A[1].
In this problem, the array A is no longer static: we need to support another operation shift(i1, i2, i3, …, ik) (i1< i2 < … < ik, k>1): we do a left “circular shift” of A[i1], A[i2], …, A[ik].
For example, if A={6, 2, 4, 8, 5, 1, 4}, then shift(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that, shift(1,2) yields {8, 6, 4, 5, 4, 1, 2}.
Input
There will be only one test case, beginning with two integers n, q (1<=n<=100,000, 1<=q<=120,000), the number of integers in array A, and the number of operations. The next line contains n positive integers not greater than 100,000, the initial elements in array A. Each of the next q lines contains an operation. Each operation is formatted as a string having no more than 30 characters, with no space characters inside. All operations are guaranteed to be valid. Warning: The dataset is large, better to use faster I/O methods.
Output
For each query, print the minimum value (rather than index) in the requested range.
Sample Input
7 5
6 2 4 8 5 1 4
query(3,7)
shift(2,4,5,7)
query(1,4)
shift(1,2)
query(2,2)
Sample Output
1
4
6
Hint
题意:有一个数组a,然后有两个操作一个query (L, R)表示数组a的第L个数到第R个数的这个区间的最小值,还有一个操作就是shift(i1, i2, i3, …, ik),表示有k个数,然后每个数表示数组a的位置,然后将这k个数向左一个,比如A={6, 2, 4, 8, 5, 1, 4}, 然后 shift(2, 4, 5, 7)操作后就变成了{6, 8, 4, 5, 4, 1, 2},操作了shift(1,2) 后就变成了{8, 6, 4, 5, 4, 1, 2}。
分析:显然有多个询问,可以用线段树来维护,每个节点存放区间的最小值,然后每个操作的左移可以分解成多个点修改就行了。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<stack>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int Min[100005<<2];//存放每个区间的最小值
int a[100005]; //a数组
int len;
char order[35];
int n,m;
void push_up(int rt){//向上更新
Min[rt]=min(Min[rt*2],Min[rt*2+1]);
}
void bulid(int rt,int l,int r){
if (r==l){
//scanf ("%d",&Min[rt]);
Min[rt]=a[l];
return ;
}
int mid=(r+l)/2;
bulid(rt*2,l,mid);
bulid(rt*2+1,mid+1,r);
push_up(rt);
}
void update(int rt,int l,int r,int p,int v){//点修改,将位置p的值改为v
if (r==l){
Min[rt]=v;
return;
}
int mid=(r+l)/2;
if (p<=mid)update(rt*2,l,mid,p,v);
else update(rt*2+1,mid+1,r,p,v);
push_up(rt);
}
int query(int rt,int l,int r,int ql,int qr){//查询区间ql,qr之间的最小值
int ans=-1;
int mid=(r+l)/2;
if (ql<=l&&r<=qr) return Min[rt];//在区间内直接返回
if (qr<=mid)return query(rt*2,l,mid,ql,qr);//如果全部在左区间
else if (ql>mid) return query(rt*2+1,mid+1,r,ql,qr);//全部在右区间
else {//两个区间都有取最小值
ans=min(query(rt*2,l,mid,ql,qr),query(rt*2+1,mid+1,r,ql,qr));
}
return ans;
}
void shift(){
vector<int>v;//用来存放位置
int tmp=0;
int i;
for (i=0;order[i];i++)if (order[i]=='(')break;
for (int j=i+1;order[j];j++){
while (order[j]!=','&&order[j]!=')'){//当遇到','或者')'说明一个数字转换好了
tmp=tmp*10+order[j]-'0';//求数字
j++;
}
v.push_back(tmp);
tmp=0;
}
int tt=a[v[0]];//保存第一个数的值
for (int i=0;i<v.size()-1;i++){
update(1,1,n,v[i],a[v[i+1]]);//更新第v[i]个位置改成a[v[i]]
a[v[i]]=a[v[i+1]];
}
update(1,1,n,v[v.size()-1],tt);//最后一个
a[v[v.size()-1]]=tt;
}
int main ()
{
while (scanf ("%d%d",&n,&m)!=EOF){
for (int i=1;i<=n;i++){
scanf ("%d",&a[i]);
}
bulid(1,1,n);
for (int i=0;i<m;i++){
scanf ("%s",order);
len=strlen(order);
if (order[0]=='s'){
shift();
}
else {
int left=0,right=0;
//sscanf(order,"query(%d,%d)",&left,&right);
int i;
for (i=6;order[i]!=',';i++){//求左值
left=left*10+order[i]-'0';
}
for (i=i+1;order[i]!=')';i++){//求右值
right=right*10+order[i]-'0';
}
int ans=query(1,1,n,left,right);//求答案
printf ("%d\n",ans);
}
}
}
return 0;
}