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

700+k,1200+ms,树状数组~~~~

Posted by ambition0109 at 2011-03-22 19:56:49 on Problem 3264 and last updated at 2011-03-22 19:57:05
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX(x,y) (x>y?x:y)
#define MIN(x,y) (x<y?x:y)
#define Lowbit(x) (x&(-x))

int num[50010],N;
int idx_max[50010],idx_min[50010];
void Init(){
	for(int ii=1;ii<=N;ii++){
		idx_max[ii]=idx_min[ii]=num[ii];
		for(int i=1;i<Lowbit(ii);i<<=1){
			idx_max[ii]=MAX(idx_max[ii],idx_max[ii-i]);
			idx_min[ii]=MIN(idx_min[ii],idx_min[ii-i]);
		}
	}
}

int Query(int l,int r){
	int mmax=num[r],mmin=num[r];
	while(true){
		mmax=MAX(mmax,num[r]);
		mmin=MIN(mmin,num[r]);
		if(r==l) break;
		for(r-=1;r-l>=Lowbit(r);r-=Lowbit(r)){
			mmax=MAX(mmax,idx_max[r]);
			mmin=MIN(mmin,idx_min[r]);
		}
	}
	return mmax-mmin;
}

int main(){
	int &n=N,m;
	while(~scanf("%d%d",&n,&m)){
		for(int i=1;i<=n;i++){
			scanf("%d",&num[i]);
		}
		Init();
		for(int i=0;i<m;i++){
			int l,r;
			scanf("%d%d",&l,&r);
			printf("%d\n",Query(l,r));
		}
	}
}

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