2011ACM湖南省省赛K题 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 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}.
  • 输入
  • 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
    很简单的线段数,只是对字符串的操作有点麻烦
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define N 100010
    int min(int a,int b){
    	return a>b?b:a;
    	}
    struct node{
    	int l;
    	int r;
    	int num;
    	}anode[4*N];
    int data[N];
    void bulid(int l,int r,int n){
    	int mid;
    	mid=(l+r)>>1;
    	anode[n].l=l;
    	anode[n].r=r;
    	if(l==r){
    		anode[n].num=data[l];
    		return;
    		}
    	bulid(l,mid,2*n);
    	bulid(mid+1,r,2*n+1);
    	anode[n].num=min(anode[2*n].num,anode[2*n+1].num);	
    	}
    void update(int x,int n,int y){
    	int mid;
    	mid=(anode[n].l+anode[n].r)>>1;
    	if(anode[n].l==x&&anode[n].r==x){
    		anode[n].num=data[y];
    		return;
    		} 
    	if(x<=mid)
    	update(x,2*n,y);
    	else if(x>mid)
    	update(x,2*n+1,y);
    	anode[n].num=min(anode[2*n].num,anode[2*n+1].num);	
    	}	
    int query(int l,int r,int n){
    	int mid;
    	mid=(anode[n].l+anode[n].r)>>1;
    	if(anode[n].l==l&&anode[n].r==r){
    		return anode[n].num;
    		}
    	if(r<=mid)
    	return query(l,r,2*n);
    	else if(l>mid)
    	return query(l,r,2*n+1);
    	else 
    	return min(query(l,mid,2*n),query(mid+1,r,2*n+1));	
    	}		
    int main(){
    	int i,n,m,j,temp;
    	int c[50];
    	char a[50];
    	while(~scanf("%d %d",&n,&m)){
    		for(i=1;i<=n;i++)
    		scanf("%d",&data[i]);
    		bulid(1,n,1);
    		while( m-- ) {
    			char a[40];
    			scanf("%s", a);
    			if( a[0] == 'q' ) {
    				int x, y;
    				sscanf( a + 6, "%d , %d", &x, &y );
    				printf("%d\n", query(x,y,1));
    			}
    			else {
    				char *p = strtok( a + 6, "," );
    				int x = 0;
    				while( p ) {
    					sscanf( p, "%d", &c[x] );
    					p = strtok( NULL, "," );
    					x++;
    				}
    		        for(i=0;i<x-1;i++){
    					j=i;
    					update(c[j],1,c[j+1]);
    					update(c[j+1],1,c[j]);
    					temp=data[c[j]];
    					data[c[j]]=data[c[j+1]];
    					data[c[j+1]]=temp;
    					}
    			}
    		}
    	}
    	return 0;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值