uva12299 RMQ with Shifts

RMQ with Shifts


In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L$ \le$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$ \le$n$ \le$100, 000, 1$ \le$q$ \le$250, 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



题意:给一串数,有q个询问,询问一个串中最下的那个数,还有个shift操作,比如2,4,5,7就是把4位置上的数移动到2上,5位置上的数移动到4位置上,2位置的移动到7上。

分析:这题目一看就知道是线段树,只不过更新点变了一下而已,更新点我们可以用一个循环来按照上面的那种方法一次进行更新。。。。。有个地方要注意下,把一行当做一串字符读进去的时候,要住一输入的位置不只是一个个位数,在这地方RE了几次



#include
#include
#include
using namespace std;
const int MAXN=100010;
struct seg
{
    int l,r;
    int mmin;
}tree[MAXN<<2];
int num[MAXN],a[50];
char s[50];
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    if(l==r)
    {
        tree[k].mmin=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
    tree[k].mmin=min(tree[k<<1].mmin,tree[k<<1|1].mmin);
}
void update(int n,int d,int k)
{
    if(tree[k].l==tree[k].r&&tree[k].l==n)
    {
        tree[k].mmin=d;
        return;
    }
    int mid=(tree[k].l+tree[k].r)>>1;
    if(n<=mid)
        update(n,d,k<<1);
    else
        update(n,d,k<<1|1);
    tree[k].mmin=min(tree[k<<1].mmin,tree[k<<1|1].mmin);
}
int query(int l,int r,int k)
{
   if(tree[k].l==l&&tree[k].r==r)
        return tree[k].mmin;
   int mid=(tree[k].l+tree[k].r)>>1;
   if(r<=mid)
        return query(l,r,k<<1);
   else if(l>mid)
        return query(l,r,k<<1|1);
   else
   {
       int t1=query(l,mid,k<<1);
       int t2=query(mid+1,r,k<<1|1);
       return min(t1,t2);
   }
}
int cnt;
int getnum()    //取出输入那一串字符串里的数字
{
    int i=6,sum=0,f;
    if(s[0]=='q')
        f=1;
    else
        f=0;
    for(;s[i];++i)
    {
        sum=0;
        for(;s[i]!=','&&s[i]!=')';++i)
            sum=sum*10+(s[i]-'0');
        a[cnt++]=sum;

    }
    return f;
}
int main()
{
    int n,m,i,j;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&num[i]);
        build(1,n,1);
        while(m--)
        {
            scanf("%s",s);
            cnt=0;
            int flag=getnum();
            if(flag)
            {
                int ans=query(a[0],a[1],1);
                printf("%d\n",ans);
            }
            else
            {
                int temp=num[a[0]];
                for(i=0;i


你可能感兴趣的:(线段树)