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 1340502116 at 2016-02-09 15:19:39 on Problem 2823
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int INF=0x7fffffff;
const int MAXN=1000005;
struct Node{
	int l,r;
	int min,max;
}a[MAXN*3];
int V,k;
void build(int l,int r,int n)
{
	a[n].l=l;
	a[n].r=r;
	if(l==r)
	{
		scanf("%d",&a[n].min);
		a[n].max=a[n].min;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,n<<1);
	build(mid+1,r,(n<<1)|1);
	a[n].min=min(a[n<<1].min,a[(n<<1)|1].min);
	a[n].max=max(a[n<<1].max,a[(n<<1)|1].max);
}
int query(int l,int r,int n,int type)
{
	if(a[n].l==l&&a[n].r==r)
	{
		if(type==1)	return a[n].min;
		else	return a[n].max;
	}
	int mid=(a[n].l+a[n].r)>>1;
	if(r<=mid)	return query(l,r,n<<1,type);
	else if(mid<l)	return query(l,r,(n<<1)|1,type);
	else
	{
		if(type==1)	return min(query(l,mid,n<<1,type),query(mid+1,r,(n<<1)|1,type));
		else	return max(query(l,mid,n<<1,type),query(mid+1,r,(n<<1)|1,type));
	}
}
int main()
{
	while(scanf("%d%d",&V,&k)!=EOF)
    {
    	build(1,V,1);
    	for(int i=1;i<=V-k+1;i++)
    		printf("%d ",query(i,i+k-1,1,1));
    	printf("\n");
    	for(int i=1;i<=V-k+1;i++)	
    		printf("%d ",query(i,i+k-1,1,2));
    	printf("\n");
	}
	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