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

Re:RMQ

Posted by cookie at 2007-07-13 21:14:26 on Problem 3264
In Reply To:Re:RMQ Posted by:nuciedh at 2007-07-13 21:08:44
别人程序就不贴了...我按她的意思写了一个:
#include<iostream>
#include<algorithm>
using namespace std;
struct MM
{
	int v,id;
}a[50002];
int b[50002];
bool com(MM p,MM q)
{
	return p.v<q.v;
}
int main()
{
	int n,m,i,j,k,st,end;
	while(scanf("%d %d",&n,&m)==2)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",&b[i+1]);
			a[i].v=b[i+1];
			a[i].id=i+1;
		}
		sort(&a[0],&a[n],com);
		
		for(i=0;i<m;i++)
		{
			scanf("%d %d",&st,&end);
			if(end-st<50)
			{
				int maxm=b[st];
				int minm=b[st];
				for(j=st;j<=end;j++)
				{
					if(maxm<b[j]) maxm=b[j];
					if(minm>b[j]) minm=b[j];
				}
				printf("%d\n",maxm-minm);
				continue;
			}
			for(j=0;j<n;j++)
				if(a[j].id>=st&&a[j].id<=end) break;
				
			for(k=n-1;k>=0;k--)
				if(a[k].id>=st&&a[k].id<=end) break;
			printf("%d\n",a[k].v-a[j].v);
		}
	}
	return 0;
}

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