UVa 12299 RMQ with Shifts(线段树)

线段树,没了..

-----------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cctype>
  
#define rep(i,n) for(int i=0;i<n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<=r;i++)
  
using namespace std;
  
const int inf=0x7fffffff;
const int maxn=100005;
  
int a[maxn];
int minv[1<<18];
  
void build(int o,int l,int r) {
int m=l+(r-l)/2;
if(l==r) minv[o]=a[l];
else {
build(o*2,l,m);
build(o*2+1,m+1,r);
minv[o]=min(minv[o*2],minv[o*2+1]);
}
}
  
int p,v;
void update(int o,int l,int r) {
int m=l+(r-l)/2;
if(l==r) minv[o]=v;
else {
p<=m ? update(o*2,l,m) : update(o*2+1,m+1,r);
minv[o]=min(minv[o*2],minv[o*2+1]);
}
}
  
int ql,qr;
int query(int o,int l,int r) {
int m=l+(r-l)/2,ans=inf;
if(ql<=l && qr>=r) return minv[o];
if(ql<=m) ans=min(ans,query(o*2,l,m));
if(qr>m) ans=min(ans,query(o*2+1,m+1,r));
return ans;
}
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int n,q;
scanf("%d%d",&n,&q);
Rep(i,1,n) scanf("%d",&a[i]);
build(1,1,n);
int cmd[20],change[20],cnt;
char s[100];
while(q--) {
scanf("%s",s);
int len=strlen(s);
cmd[cnt=0]=0;
for(int i=6;i<len;++i)
if(isdigit(s[i])) (cmd[cnt]*=10)+=s[i]-'0';
else cmd[++cnt]=0;
if(s[0]=='q') {
ql=cmd[0],qr=cmd[1];
printf("%d ",query(1,1,n));
} else {
rep(i,cnt) change[i]=a[cmd[i]];
rep(i,cnt) { v=a[p=cmd[i]]=change[(i+1)%cnt]; update(1,1,n); }
}
}
return 0;
}

   

----------------------------------------------------------------------------------------- 

12299 RMQ with Shifts
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
原文地址:https://www.cnblogs.com/JSZX11556/p/4372987.html