RMQ with Shifts(线段树)

RMQ with Shifts

Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Description

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

无

//线段树的应用,不是很难,写出来还是对线段树有更深一点的了解的
 #include <stdio.h>
#include <string.h> #define MAXN 100005 struct Node
{
int min;
int l,r;
}node[*MAXN];//节点
int pos[MAXN];//记录叶节点的位置
int num[MAXN];//记录最开始的数
int shift[];//记录shift里的数 int Min(int a,int b)
{
return a<b?a:b;
} int Build(int left,int right,int k)
{
node[k].l=left;
node[k].r=right;
if (left==right)//到叶节点
{
node[k].min=num[left];
pos[left]=k;
return node[k].min;
}
int mid=(left+right)/; node[k].min=Min(Build(left ,mid,*k),Build(mid+,right,*k+));
return node[k].min;
} int Query(int left,int right,int k)
{
if (left==node[k].l&&right==node[k].r)
{
return node[k].min;
}
int mid=(node[k].l+node[k].r)/;
if (left>mid) return Query(left,right,*k+);
else if (right<=mid) return Query(left,right,*k);
return Min(Query(left,mid,*k),Query(mid+,right,*k+));
} void Update(int k)
{
k/=;
while (k!=)
{
node[k].min=Min(node[*k].min,node[*k+].min);
k/=;
}
} int Get_shift(char str[])
{
int i,j;
int n=;
int len=strlen(str);
for (i=;i<len;i++)
{
int temp=;
for (j=i;str[j]!=','&&str[j]!=')';j++)
{
temp+=str[j]-'';
temp*=;
}
temp/=;
shift[++n]=temp;
i=j;
}
return n;
} int main()
{
int n,m;
int i,j;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&num[i]); Build(,n,); //递归建树
char str[];
int left,right; for (i=;i<=m;i++)
{
scanf("%s",&str);
if (str[]=='q')
{
left=,right=;
for (j=;str[j]!=',';j++)
{
left+=str[j]-'';
left*=;
}
left/=;
for (j++;str[j]!=')';j++)
{
right+=str[j]-'';
right*=;
}
right/=;
printf("%d\n",Query(left,right,));//查找区间内最小的
}
if (str[]=='s')
{
int shift_num=Get_shift(str);//获得shift里面的数 int temp=node[pos[shift[]]].min;
for (j=;j<=shift_num;j++)
node[pos[shift[j-]]].min=node[pos[shift[j]]].min;
node[pos[shift[j-]]].min=temp; for (j=;j<=shift_num;j++)
Update(pos[shift[j]]);
} }
return ;
}


上一篇:Eighth scrum meeting - 2015/11/2


下一篇:bzoj3932--可持久化线段树