RMQ with Shifts 线段树基基础


此页面通过工具从 csdn 导出,格式可能有问题。

思路

看上去是个线段树,不过每个元素都移动,树结构本身无法实现这个功能,只能一个一个的修改,但是里面的技巧还是有的。学长的代码都上百行了,贴一帖我的60行代码,细节上有点优化处理。

代码

#include <cstdio>
#define N 101010
struct rec{
    int l,r,v;
}t[N*5];
int n,m,a[N],p[30];
int minn(int a,int b){
    if (a<b) return a;
    return b;
}
void build(int root, int l, int r){
    t[root].l = l;
    t[root].r = r;
    if ( l==r ) {
        t[root].v = a[l];
        return ;
    }
    int mid = (l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    t[root].v = minn(t[root*2].v,t[root*2+1].v);
}
void update(int root, int x){
    if ( t[root].l == x && t[root].r == x ) {
        t[root].v = a[x];
        return ;
    }
    int mid = (t[root].l+t[root].r)/2;
    if ( mid < x ) update(root*2+1,x);
    else update(root*2,x);
    t[root].v = minn(t[root*2].v,t[root*2+1].v);
}
int getv(int root,int l,int r){
    if ( t[root].l == l && t[root].r == r ) {
        return t[root].v;
    }
    int mid = (t[root].l+t[root].r)/2;
    if ( l>mid ) return getv(root*2+1,l,r);
    if ( r<=mid) return getv(root*2,l,r);
    return minn(getv(root*2,l,mid),getv(root*2+1,mid+1,r));
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i(1);i<=n;i++){
        scanf("%d",&a[i]);
    }scanf("\n");
    build(1,1,n);
    while (m--){
        char c,c2;
        scanf("%c%c%c%c%c%c",&c,&c2,&c2,&c2,&c2,&c2);
        if ( c=='q' ){
            int ax,bx;
            scanf("%d,%d)\n",&ax,&bx);
            printf("%d\n",getv(1,ax,bx));
        }else{
            int j = 0;
            while ( scanf("%d,",&p[++j])==1 ); j--;
            scanf(")\n");
            int tt=a[p[1]];
            for ( int i(1);i<j;i++ ) a[p[i]] = a[p[i+1]];
            a[p[j]] = tt;
            for ( int i(1);i<=j;i++) update(1,p[i]);
        }
    }
    return 0;
}

题目


Problem K: RMQ with Shifts

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 12 Solved: 5
[ Submit ][ Status ][ Web Board ] [ Edit ]

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 56 2 4 8 5 1 4query(3,7)shift(2,4,5,7)query(1,4)shift(1,2)query(2,2)

Sample Output

146

HINT

[ Submit ][ Status ][ Web Board ]
Avatar
huiren
Code Artisan

问渠那得清如许,为有源头活水来

下一页
上一页
comments powered by Disqus