2011湖南省ACM大赛K题

                                     想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410
                                     转载请注明出处:
http://blog.csdn.net/wangjian8006

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}.
Input
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.
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)

 

Output for the Sample Input

1

4

6

 

题目大意:对于数组A,有两种操作,一种是query(L,R)(L<=R),这代表输出从L到R区间中A数组的最小值,对于shift操作,shift(i1,i2,i3,i4...in)是将,i2给i1,i3给i2...i(n)给i(n-1),然后将i1给i(n),一个循环左移的过程。

输入包括两个数,N,Q,N代表数组的过程,Q代表操作数的个数

最后对线段树离散点更新比较麻烦点。

#include <stdio.h>
#include <string.h>
#define MAXV 1<<18
#define min(a,b) a<b?a:b

int a[MAXV],T[MAXV],TR[MAXV],TL[MAXV],shift[MAXV],pos[MAXV];

void createTree(int v,int f,int t){
	TL[v]=f,TR[v]=t;		//记录顶点v的左右区间,方便等下查询
	if(f==t){				//若左右区间相等,则最小值就是本身
		T[v]=a[f];
		pos[f]=v;//记录数组的位置,叶子结点的位置,方便离散点更新,因为最后需要从叶子结点往根结点访问
		return;
	}

	int mid=(f+t)>>1;

	createTree(v<<1,f,mid);
	createTree((v<<1)|1,mid+1,t);

	T[v]=min(T[v<<1],T[(v<<1)|1]);
}

int queryTree(int v,int L,int R){		//其作用是找到从L到R的最小值
	if(L==TL[v] && R==TR[v])		//如果其区间符合一个结点的区间直接返回这个结点的值
		return T[v];
	int mid=(TL[v]+TR[v])>>1;
	if(R<=mid)			//当这个区间在当前结点的左边结点的区间中
		return queryTree(v<<1,L,R);
	else if(L>mid)		//当这个区间在当前结点的右边结点的区间中
		return queryTree((v<<1)|1,L,R);
	else				//当这个区间在当前结点的左边与右边结点中
		return min(queryTree(v<<1,L,mid),queryTree((v<<1)|1,mid+1,R));
}

int getshift(char *s){	//对shift字符串进行操作,将里面的数字给扣出来保存
	char *q=s+6;
	int sum=0,t=0;
	while(1){
		if((*q)==',') {
			shift[sum++]=t;
			t=0;
		}else t=t*10+((*q)-'0');
		q++;
		if((*q)==')') {
			shift[sum++]=t;
			break;
		}
	}
	return sum;
}

void updateTree(int sum){
	int i,v,temp;
	for(i=0;i<sum;i++){
		v=pos[shift[i]];			//shift记录了是哪些叶子结点有了变化
		while(1){
			if(v%2==0)		//从当前结点向父亲结点去更新,因为叶子结点变了,所以其父亲结点相应的要变化
				v=v>>1;
			else
				v=(v-1)>>1;
			temp = min(T[v<<1],T[(v<<1)|1]);
			if(temp==T[v]) break;
			T[v] = temp;		//这三行是表明,当前的最小值没有发生变化,那么不需要再向其父亲结点再更新
			if(v==1) break;		//遇到根结点退出
		}
	}
}

int main(){
	int n,t,i,L,R;
	char s[40];
	scanf("%d%d",&n,&t);
	
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	createTree(1,1,n);
	while(t--){
		scanf("%s",s);
		if(s[0]=='q'){
			sscanf(s,"query(%d,%d)",&L,&R);
			printf("%d\n",queryTree(1,L,R));
		}else{
			int sum = getshift(s);
			int temp = T[pos[shift[0]]];

			for(i=1;i<sum;i++)			//先将叶子结点变化的给更新了,才能往其他的结点更新
				T[pos[shift[i-1]]]=T[pos[shift[i]]];
			T[pos[shift[i-1]]] = temp;

			updateTree(sum);
		}
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值