湖南省第七届程序设计竞赛 RMQ with Shifts

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


/*赤裸裸的线段树,数据的预处理比较恶心,明明本地所有测试数据都过了,oj上就是过不了。
总的来说就是一道套模板的水题*/
#include <stdio.h>
int change[30];
int old[100005];
struct node
{
    int L,R;
    int minV;
} tree[400010];
int minn(int a,int b){
    if(a<b)
        return a;
    else
        return b;
}
void build(int root,int L,int R)
{
    tree[root].L=L;
    tree[root].R=R;
    if(L==R)
    {
        tree[root].minV=old[L];
        return;
    }
    build(2*root,L,(L+R)/2);
    build(2*root+1,(L+R)/2+1,R);
    tree[root].minV=minn(tree[2*root].minV,tree[2*root+1].minV);

}
void update(int root,int i)
{
    if (tree[root].L==i&&tree[root].R==i)
    {
        tree[root].minV=old[i];
        return ;
    }
    int mid=(tree[root].L+tree[root].R)/2;
    if(i<=mid)
        update(root*2,i);
    else
        update(root*2+1,i);
    tree[root].minV=minn(tree[2*root].minV,tree[2*root+1].minV);
}

int query(int root,int l,int r)
{
    if(tree[root].L==l && tree[root].R==r)
    {
        return tree[root].minV;
    }
    int mid=(tree[root].L+tree[root].R)/2;
    if (r<=mid)
        return query(root*2,l,r);
    if (l>mid)
        return query(root*2+1,l,r);
    return minn(query(root*2,l,mid),query(root*2+1,mid+1,r));
}
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&old[i]);
    }
    scanf("\n");
    build(1,1,n);
    char c,c2,c3;
    int cfront,cback;
    for(int t=1; t<=q; t++)
    {
        scanf("%c%c%c%c%c%c",&c,&c2,&c2,&c2,&c2,&c2);
        if(c=='q')
        {
            scanf("%d,%d)",&cfront,&cback);
            scanf("\n");
            printf("%d\n",query(1,cfront,cback));

        }
        else
        {
            int j=1;
            while(scanf("%d%c",&change[j],&c3)==2 ){
                if(c3==')')
                    break;
                j++;
            };
            scanf("\n");
            int temp;
            temp=old[change[1]];
            for(int i=1; i<j; i++)
            {
                old[change[i]]=old[change[i+1]];
            }
            old[change[j]] =temp;
            for(int i=1; i<=j; i++)
            {
                update(1,change[i]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值