线段树区间合并——UVA 1400

 "Ray, Pass me the dishes!"
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ... , the dishes are represented by 1, 2, 3 ... ). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.

Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.

For each question Neal asks, he will first write down an interval [ab] (inclusive) to represent all the dishes aa + 1,..., b , where a and bare positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.

Input

The input file contains multiple test cases. For each test case, there are two integers n and m in the first line (nm < 500000) . n is the number of dishes and m is the number of questions Neal asks.

Then n numbers come in the second line, which are the values of the dishes from left to right. Next m lines are the questions and each line contains two numbers a , b as described above. Proceed to the end of the input file.

Output

For each test case, output m lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.

Sample Input

3 1 
1 2 3 
1 1

Sample Output

Case 1:

1 1

题意:给出n个数和m个区间询问,对于每个询问,求该区间内的最大连续和,输出起始下标和终点下标。如果有多个解,

输出起点小的,如果起点相同,输出终点小的。

思路:线段树每个结点维护区间的最大连续和,最大前缀和,最大后缀和,还有它们的起点终点下标。


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define ms(x,y) memset(x,y,sizeof(x))
#define MAX(x,y) ((x)>(y)?(x):(y))
#define LL long long
const int MAXN=500000+10;
const int INF=1<<30;
using namespace std;
LL sum[MAXN<<2];
LL msum[MAXN<<2];
LL lsum[MAXN<<2];
LL rsum[MAXN<<2];
int ll[MAXN<<2];
int lr[MAXN<<2];
int ml[MAXN<<2];
int mr[MAXN<<2];
int rl[MAXN<<2];
int rr[MAXN<<2];

void up(int root)
{
	int lroot = root<<1;
	int rroot = root<<1|1;
	sum[root] = sum[lroot] + sum[rroot];
	lsum[root] = lsum[lroot];
	rsum[root] = rsum[rroot];
	//维护前缀
	ll[root] = ll[lroot];
	lr[root] = lr[lroot];
	LL sl = sum[lroot] + lsum[rroot];
	if(sl > lsum[root]){
		lsum[root] = sl;
		lr[root] = lr[rroot];
	}
	//维护后缀
	rl[root] = rl[rroot];
	rr[root] = rr[rroot];
	LL sr = sum[rroot] + rsum[lroot];
	if(sr >= rsum[root]){
		rsum[root] = sr;
		rl[root] = rl[lroot];
	}
	//维护区间最值
	ml[root] = ml[lroot]; 
	mr[root] = mr[lroot]; 
	msum[root] = msum[lroot];
	if(msum[root] < rsum[lroot] + lsum[rroot]){
		msum[root] = rsum[lroot] + lsum[rroot];
		ml[root] = rl[lroot];
		mr[root] = lr[rroot];
	}
	if(msum[root] < msum[rroot]){
		msum[root] = msum[rroot];
		ml[root] = ml[rroot];
		mr[root] = mr[rroot];
	}
}

void Build(int root, int left, int right)
{
	if(left == right){
		scanf("%lld", &sum[root]);
		lsum[root] = sum[root]; ll[root] = left; lr[root] = right;
		msum[root] = sum[root]; ml[root] = left; mr[root] = right;
		rsum[root] = sum[root]; rl[root] = left; rr[root] = right;
		return;
	}
	int mid = (left + right)>>1;
	Build(root<<1, left, mid);
	Build(root<<1|1, mid+1, right);
	up(root);	
}

struct Node
{
	LL msum, lsum, rsum, sum;
	int ll, lr, ml, mr, rl, rr;
};

Node query(int root, int left, int right, int l, int r)
{
	if(l == left && right == r){
		Node N;
		N.sum = sum[root];
		N.msum = msum[root];
		N.lsum = lsum[root];
		N.rsum = rsum[root];
		N.ll = ll[root];
		N.lr = lr[root];
		N.ml = ml[root];
		N.mr = mr[root];
		N.rl = rl[root];
		N.rr = rr[root];
		return N;
	}
	int mid = (left + right)>>1;
	Node res,res1,res2;
	int lroot = root<<1;
	int rroot = root<<1|1;
	int markl = 0, markr = 0;
	if(r <= mid) return res = query(lroot, left, mid, l, r);
	if(l > mid) return res2 = query(rroot, mid+1, right, l, r);
	else{
		res = query(lroot, left, mid, l, mid);
		res2 = query(rroot, mid+1, right, mid+1, r);

		res1.sum = res.sum + res2.sum;
		res1.lsum = res.lsum;
		res1.rsum = res2.rsum;
		res1.ll = res.ll;
		res1.lr = res.lr;
		LL sl = res.sum + res2.lsum;
		if(sl > res1.lsum){
			res1.lsum = sl;
			res1.lr = res2.lr;
		}
		res1.rl = res2.rl;
		res1.rr = res2.rr;
		LL sr = res2.sum + res.rsum;
		if(sr >= res1.rsum){
			res1.rsum = sr;
			res1.rl = res.rl;
		}
		res1.msum = res.msum;
		res1.ml = res.ml;
		res1.mr = res.mr;
		if(res1.msum < res.rsum + res2.lsum){
			res1.msum = res.rsum + res2.lsum;
			res1.ml = res.rl;
			res1.mr = res2.lr;
		}
		if(res1.msum < res2.msum){
			res1.msum = res2.msum;
			res1.ml = res2.ml;
			res1.mr = res2.mr;
		}
		return res1;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,q,w = 0;
	while(~scanf("%d%d", &n,&q))
	{
		printf("Case %d:\n", ++w);
		Build(1,1,n);
		int l,r;
		while(q--)
		{
			scanf("%d%d", &l,&r);
			Node ans = query(1,1,n,l,r);
			printf("%d %d\n", ans.ml, ans.mr);
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值