Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

没玩过rmq,线段树太烦燥了,未理解st, 朴素算法嘛,就它了

Posted by tzkq at 2010-07-14 07:08:51 on Problem 3264
稍微优化的朴素算法,从O(n)到O(n^(1/2)),题目的n=50000,那么最坏情况225*200000,4千w

恩,一个点最坏在0.4秒以内,线上跑了近2秒,还行,还算是轻松的解决。

#define M 200
int i,j,k,n,T,a[505],b[505],c,d,s[50050];
main(){
	memset(a,1,sizeof(a));

	for(scanf("%d%d",&n,&T);i<n;i++){
		scanf("%d",s+i);
		if(s[i]<a[i/M])a[i/M]=s[i];
		if(b[i/M]<s[i])b[i/M]=s[i];
	}

	for(;T--;){
		scanf("%d%d",&i,&j);
		c=1e8;d=0;
		i--;j--;
		for(k=i/M+1;k<j/M;k++){
			if(a[k]<c)c=a[k];
			if(d<b[k])d=b[k];
		}
		for(k=i;k<=j&&(k%M||k==i);k++){
			if(s[k]<c)c=s[k];
			if(d<s[k])d=s[k];
		}
		for(k=j;i<=k&&(k%M!=M-1||k==j);k--){
			if(s[k]<c)c=s[k];
			if(d<s[k])d=s[k];
		}
		printf("%d\n",d-c);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator