12299 - RMQ with Shifts

来源:互联网 发布:影视特效飞天软件 编辑:程序博客网 时间:2024/05/18 00:17

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
shif t(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 shif t(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that,
shif t(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 ≤ 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
我有话说:
线段树的入门题目,点修改+区间查询。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100000+10;const int maxnode = 2000000+10;const int INF=100000000;typedef long long LL;int a[maxn],minv[maxnode];int p,v;void update(int o,int L,int R){    int M=(L+R)/2;    if(L==R)minv[o]=v;    else{        int lc=o*2;        int rc=o*2+1;        if(p<=M)update(lc,L,M);        if(p>M)update(rc,M+1,R);        minv[o]=min(minv[lc],minv[rc]);    }}int ql,qr;int query(int o,int L,int R){    int M=(L+R)/2,ans=INF;    int lc=o*2,rc=o*2+1;    if(ql<=L&&qr>=R)return minv[o];    if(ql<=M)ans=min(ans,query(lc,L,M));    if(qr>M)ans=min(ans,query(rc,M+1,R));    return ans;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)==2){        for(p=1;p<=n;p++){            scanf("%d",&v);            a[p]=v;            update(1,1,n);        }        /*for(int i=1;i<=n*2;i++)printf("%d ",minv[i]);            printf("\n");*/        for(int i=0;i<m;i++){            char s[50];            int tmp[50],cnt=0;            scanf("%s",s);            for(int i=0;i<strlen(s);i++){                if(s[i]<'0'||s[i]>'9')continue;                int t=0;                while(s[i]>='0'&&s[i]<='9'){                    t=t*10+s[i]-'0';                    i++;                }                tmp[cnt++]=t;            }            if(s[0]=='q'){ql=tmp[0];qr=tmp[1];printf("%d\n",query(1,1,n));}            else{                int t=a[tmp[0]];                for(int i=0;i<cnt-1;i++){                    p=tmp[i];v=a[tmp[i+1]];a[p]=v;                    update(1,1,n);                }                p=tmp[cnt-1];v=t;a[p]=v;                update(1,1,n);            }        }    }    return 0;}
0 0
原创粉丝点击