RMQ with Shifts(线段树单点跟新)

Problem 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

思路:还以为是把shift的索引所对的值降序排序然后更新,原来却是shift的全部索引向左移一位,首位就放到最后。直接线段树单点更新。

#include <cstdio>
#include <algorithm>
using namespace std;

int n,q,node[300000],ans,sortarr[30],num[100001],val[30];

void build(int left,int right,int index)
{
    if(left==right)
    {
        node[index]=num[left];
    }
    else
    {
        int mid=(left+right)>>1;

        build(left,mid,index<<1);
        build(mid+1,right,index<<1|1);

        node[index]=min(node[index<<1],node[index<<1|1]);
    }
}

void update(int p,int left,int right,int index,int val)
{
    int mid=(left+right)>>1;

    if(left==right) node[index]=val;
    else
    {
        if(p<=mid) update(p,left,mid,index<<1,val);
        else update(p,mid+1,right,index<<1|1,val);

        node[index]=min(node[index<<1],node[index<<1|1]);
    }
}

void query(int L,int R,int left,int right,int index)
{
    if(L<=left && right<=R)
    {
        ans=min(ans,node[index]);
    }
    else
    {
        int mid=(left+right)>>1;

        if(L<=mid) query(L,R,left,mid,index<<1);
        if(R>mid) query(L,R,mid+1,right,index<<1|1);
    }
}

int main()
{
    int i,j,a,b,cnt,temp;
    char s[81];

    while(~scanf("%d%d",&n,&q))
    {
        for(i=1;i<=n;i++) scanf("%d",&num[i]);

        build(1,n,1);

        while(q--)
        {
            scanf("%s",s);

            if(s[0]=='q')
            {
                sscanf(s,"query(%d,%d)",&a,&b);
                ans=999999999;
                query(a,b,1,n,1);
                printf("%d\n",ans);
            }
            else
            {
                cnt=0;
                int t=0;
                for(i=0;s[i];i++)  if(s[i]=='(') break;
                for(int j=i+1;s[j];j++){
                    while(s[j]!=','&&s[j]!=')'){
                        t=t*10+s[j]-'0';
                        j++;
                    }
                    sortarr[cnt++]=t;
                    t=0;
                }

                for(i=0;i<cnt;i++) val[i]=num[sortarr[i]];

                for(i=0;i<cnt;i++)
                {
                    update(sortarr[i],1,n,1,val[i+1<cnt?i+1:0]);
                    num[sortarr[i]]=val[i+1<cnt?i+1:0];
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值