比赛 20120418s 评测结果 AAAAAAAA
题目名称 山海经 最终得分 100
用户昵称 201101 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-04-18 10:01:01
显示代码纯文本
/*
UID:cheepok
PID:hill
*/

#include<stdio.h>

struct Node
{
	int l,r,m,x,y,z,sum;
	int x1,x2,y1,z1;
	Node *L,*R;Node();
};

struct orz
{
	int x,y,z;
	int x1,x2,y1,z1;
	orz();
};

Node :: Node()
{
	l=r=m=x=y=z=0;
	x1=x2=y1=z1=0;
	L=R=NULL;
}

orz :: orz()
{
	x=y=z=0;
	x1=x2=y1=z1=0;
}

int n,m,a[100001],s[100001];

int max(int a,int b)
{
	return a>b?a:b;
}

void Build(Node *Root)
{
	Root->m=(Root->l+Root->r)>>1;
	if(Root->l==Root->r)
	{
		Root->x=a[Root->m];
		Root->y=Root->x;
		Root->z=Root->x;
		Root->x1=Root->m;
		Root->x2=Root->m;
		Root->y1=Root->m;
		Root->z1=Root->m;
		return;
	}
	Node *p=new Node;
	Node *q=new Node;
	p->l=Root->l;
	p->r=Root->m;
	q->l=Root->m+1;
	q->r=Root->r;
	Build(p);
	Build(q);
	if(p->x>=q->x)
	{
		Root->x=p->x;
		Root->x1=p->x1;
		Root->x2=p->x2;
	}
	else
	{
		Root->x=q->x;
		Root->x1=q->x1;
		Root->x2=q->x2;
	}
	if(p->z+q->y>Root->x)
	{
		Root->x=p->z+q->y;
		Root->x1=p->z1;
		Root->x2=q->y1;
	}
	else if(p->z+q->y==Root->x&&Root->x1>p->z1)
	{
		Root->x=p->z+q->y;
		Root->x1=p->z1;
		Root->x2=q->y1;
	}
	if(p->y>=(s[p->r]-s[p->l-1]+q->y))
	{
		Root->y=p->y;
		Root->y1=p->y1;
	}
	else
	{
		Root->y=s[p->r]-s[p->l-1]+q->y;
		Root->y1=q->y1;
	}
	if(q->z>(s[q->r]-s[p->r]+p->z))
	{
		Root->z=q->z;
		Root->z1=q->z1;
	}
	else
	{
		Root->z=s[q->r]-s[p->r]+p->z;
		Root->z1=p->z1;
	}
	Root->L=p;
	Root->R=q;
	p=NULL;
	q=NULL;
	delete p;
	delete q;
	return;
}

orz search(Node *Root,int l,int r)
{
	if(l==Root->l&&r==Root->r)
	{
		orz ans;
		ans.x=Root->x;
		ans.x1=Root->x1;
		ans.x2=Root->x2;
		ans.y=Root->y;
		ans.y1=Root->y1;
		ans.z=Root->z;
		ans.z1=Root->z1;
		return ans;
	}
	if(r<=Root->m)
	{
		orz ans;
		ans=search(Root->L,l,r);
		return  ans;
	}
	else if(l>Root->m)
	{
		orz ans;
		ans=search(Root->R,l,r);
		return ans;
	}
	else
	{
		orz ans,left,right;
		left=search(Root->L,l,Root->m);
		right=search(Root->R,Root->m+1,r);
		if(left.x>=right.x)
		{
			ans.x=left.x;
			ans.x1=left.x1;
			ans.x2=left.x2;
		}
		else
		{
			ans.x=right.x;
			ans.x1=right.x1;
			ans.x2=right.x2;
		}
		if(left.z+right.y>ans.x)
		{
			ans.x=left.z+right.y;
			ans.x1=left.z1;
			ans.x2=right.y1;
		}
		else if(left.z+right.y==ans.x&&ans.x1>left.z1)
		{
			ans.x=left.z+right.y;
			ans.x1=left.z1;
			ans.x2=right.y1;
		}
		if(left.y>=s[Root->m]-s[l-1]+right.y)
		{
			ans.y=left.y;
			ans.y1=left.y1;
		}
		else
		{
			ans.y=s[Root->m]-s[l-1]+right.y;
			ans.y1=right.y1;
		}
		if(right.z>s[r]-s[Root->m]+left.z)
		{
			ans.z=right.z;
			ans.z1=right.z1;
		}
		else
		{
			ans.z=s[r]-s[Root->m]+left.z;
			ans.z1=left.z1;
		}
		return ans;
	}
}

int main()
{
	freopen("hill.in","r",stdin);
	freopen("hill.out","w",stdout);
	int i,x,y;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		s[i]=s[i-1]+a[i];
	}
	Node *Root=new Node;
	Root->l=1;Root->r=n;
	Build(Root);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		orz tmp;
		tmp=search(Root,x,y);
		printf("%d %d %d\n",tmp.x1,tmp.x2,tmp.x);
	}
	return 0;
}