Uva 12295 - RMQ with Shifts
你为何这么叼!
这题说好的只有一组数据呢,还要0和文件结束个蛋?
RMQ with Shifts
Time limit: 10.000 seconds
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
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
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
The Seventh Hunan Collegiate Programming Contest
Problemsetter: Rujia Liu, Special Thanks: Yiming Li & Jane Alam Jan
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define INF 0x7fffffff 6 #define maxn 111111 7 int x[maxn<<2]; 8 int mi[maxn<<2]; 9 int tmp[maxn<<2]; 10 int n,m,cnt=0; 11 void up(int rt){mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);} 12 void build(int l,int r,int rt){ 13 if(l==r){scanf("%d",&mi[rt]);tmp[cnt++]=mi[rt];return;} 14 int m=(l+r)>>1; 15 build(l,m,rt<<1); 16 build(m+1,r,rt<<1|1); 17 up(rt); 18 } 19 void update(int p,int d,int l,int r,int rt){ 20 if(l==r){mi[rt]=d;return;} 21 int m=(l+r)>>1; 22 if(p<=m)update(p,d,l,m,rt<<1); 23 if(p>m)update(p,d,m+1,r,rt<<1|1); 24 up(rt); 25 } 26 int query(int L,int R,int l,int r,int rt){ 27 if(L<=l&&R>=r)return mi[rt]; 28 int m=(l+r)>>1; 29 int res=INF; 30 if(L<=m)res=min(res,query(L,R,l,m,rt<<1)); 31 if(R>m)res=min(res,query(L,R,m+1,r,rt<<1|1)); 32 return res; 33 } 34 int main(){ 35 while(~scanf("%d%d",&n,&m)&&n){ 36 build(0,n-1,1); 37 char op[31]; 38 int a,b; 39 while(m--){ 40 scanf("%5s",op); 41 if(op[0]=='q'){ 42 scanf("(%d,%d)",&a,&b); 43 a--;b--; 44 printf("%d\n",query(a,b,0,n-1,1)); 45 } 46 else{ 47 int k=0; 48 char ch; 49 while(ch!='(')ch=getchar(); 50 while(ch!=')')scanf("%d%c",&x[k++],&ch); 51 for(int i=0;i<k;i++)x[i]--; 52 int temp=tmp[x[0]]; 53 for(int i=0;i<k-1;i++)tmp[x[i]]=tmp[x[i+1]]; 54 tmp[x[k-1]]=temp; 55 for(int i=0;i<k;i++)update(x[i],tmp[x[i]],0,n-1,1); 56 } 57 } 58 } 59 return 0; 60 }
浙公网安备 33010602011771号