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.
For each query, print the minimum value (rather than index) in the requested range.
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)
1 4 6
思路:还以为是把shift的索引所对的值降序排序然后更新,原来却是shift的全部索引向左移一位,首位就放到最后。直接线段树单点更新。
#include <cstdio> #include <algorithm> using namespace std; int n,q,node[300000],ans,sortarr[30],num[100001],val[30]; void build(int left,int right,int index) { if(left==right) { node[index]=num[left]; } else { int mid=(left+right)>>1; build(left,mid,index<<1); build(mid+1,right,index<<1|1); node[index]=min(node[index<<1],node[index<<1|1]); } } void update(int p,int left,int right,int index,int val) { int mid=(left+right)>>1; if(left==right) node[index]=val; else { if(p<=mid) update(p,left,mid,index<<1,val); else update(p,mid+1,right,index<<1|1,val); node[index]=min(node[index<<1],node[index<<1|1]); } } void query(int L,int R,int left,int right,int index) { if(L<=left && right<=R) { ans=min(ans,node[index]); } else { int mid=(left+right)>>1; if(L<=mid) query(L,R,left,mid,index<<1); if(R>mid) query(L,R,mid+1,right,index<<1|1); } } int main() { int i,j,a,b,cnt,temp; char s[81]; while(~scanf("%d%d",&n,&q)) { for(i=1;i<=n;i++) scanf("%d",&num[i]); build(1,n,1); while(q--) { scanf("%s",s); if(s[0]==‘q‘) { sscanf(s,"query(%d,%d)",&a,&b); ans=999999999; query(a,b,1,n,1); printf("%d\n",ans); } else { cnt=0; int t=0; for(i=0;s[i];i++) if(s[i]==‘(‘) break; for(int j=i+1;s[j];j++){ while(s[j]!=‘,‘&&s[j]!=‘)‘){ t=t*10+s[j]-‘0‘; j++; } sortarr[cnt++]=t; t=0; } for(i=0;i<cnt;i++) val[i]=num[sortarr[i]]; for(i=0;i<cnt;i++) { update(sortarr[i],1,n,1,val[i+1<cnt?i+1:0]); num[sortarr[i]]=val[i+1<cnt?i+1:0]; } } } } }
RMQ with Shifts(线段树单点跟新),布布扣,bubuko.com
原文:http://blog.csdn.net/faithdmc/article/details/21320449