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

谁能提供几组数据啊,感激不尽...

Posted by hutu_2000 at 2009-07-14 19:24:41 on Problem 3368
代码如下:
#include <iostream>
using namespace std;
#define MA 2000000
#define N 100005
struct point{
	int begin,end;
};
point location[N*4];
int length=0;
struct node{
	int a,b,max;
	bool yezhi;
};
node str[N*4];
void init(int root,int x,int y);
int getmax(int root,int x,int y);
int main()
{
	int n,m,i;
	while(scanf("%d",&n)==1&&n)
	{
		scanf("%d",&m);
		memset(str,0,sizeof(str));
	    int bef=MA,loc=0;
	    for(i=0;i<n;i++)
		{ 
			int d;
		    scanf("%d",&d);
		    if(bef==MA)
			{ 
				bef=d;
			    location[length].begin=i;
			} 
		    else
			{ 
				if(d!=bef)
				{ 
					bef=d;
				    location[length++].end=i-1;
				    location[length].begin=i;
				} 
			} 
		}
		location[length].end=i-1;
		init(0,0,length);
		for(i=0;i<m;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			printf("%d\n",getmax(0,x-1,y-1));
		} 	
	}
	return 0;
}

void init(int root,int x,int y)
{
	if(x==y)
	{
		str[root].a=location[x].begin;
		str[root].b=location[x].end;
		str[root].max=str[root].b-str[root].a+1;
		str[root].yezhi=true;
	}
	else
	{
		init(2*root+1,x,(x+y)/2);
		str[root].a=str[2*root+1].a;
		init(2*root+2,(x+y)/2+1,y);
		str[root].b=str[2*root+2].b;
		str[root].max=str[2*root+1].max>str[2*root+2].max?str[2*root+1].max:str[2*root+2].max;
	}
}

int getmax(int root,int x,int y)
{
	if(x>y) return 0;
	if(str[root].a>x) x=str[root].a;
	if(str[root].b<y) y=str[root].b;
	if(str[root].yezhi)
	{
		if(x>y) return 0;
		else return y-x+1;
	}
	else
	{
		if(x==str[root].a&&y==str[root].b)
			return str[root].max;
		else
		{
			int u=getmax(2*root+1,x,y);
	        int v=getmax(2*root+2,x,y);
	        return u>v?u:v;
		}
	} 
}

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