LA 3938 线段树(区间合并)

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 b are 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
 
有bug。D了半天没搞出来、改天再来更改。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 500080
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define LL long long int
LL key[maxn];
int s,t;
struct ST
{
	int l,r,a,b,left,right;
	LL maxsum,sum,lsum,rsum;
}st[maxn<<2];

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

void PushUp(int id)
{
	st[id].left = st[id<<1].left;
	st[id].right = st[id<<1|1].right;
	st[id].lsum = st[id<<1].lsum;
	st[id].rsum = st[id<<1|1].rsum;
	st[id].sum = st[id<<1].sum + st[id<<1|1].sum;
	LL ans1 = st[id<<1].maxsum,ans2 = st[id<<1|1].maxsum;
	if(ans1 >= ans2)
	{
		st[id].maxsum = st[id<<1].maxsum;
		st[id].a = st[id<<1].a;
		st[id].b = st[id<<1].b;
	}
	else 
	{
		st[id].maxsum = st[id<<1|1].maxsum;
		st[id].a = st[id<<1|1].a;
		st[id].b = st[id<<1|1].b;
	}
	//lsum,rsum,maxsum,a,b,left,right都有可能更改
	//lsum
	if(st[id].lsum == st[id<<1].sum && st[id<<1|1].lsum > 0)
	{
		st[id].lsum = st[id].lsum + st[id<<1|1].lsum;
		st[id].left = st[id<<1|1].left;
	}
	//rsum
	if(st[id].rsum == st[id<<1|1].sum && st[id<<1].rsum >= 0)
	{
		st[id].rsum = st[id].rsum + st[id<<1].rsum;
		st[id].right = st[id<<1].right;
	}
	//maxsum
	if((st[id<<1].rsum + st[id<<1|1].lsum > st[id].maxsum) || 
	  (st[id<<1].right < st[id].a && st[id<<1].rsum + st[id<<1|1].lsum == st[id].maxsum))
	{
		st[id].maxsum = st[id<<1].rsum + st[id<<1|1].lsum;
		st[id].a = st[id<<1].right;
		st[id].b = st[id<<1|1].left;
	}
}

void buildtree(int id,int l,int r)
{
	st[id].l = l,st[id].r = r;
	if(l == r)
	{
		st[id].sum = st[id].lsum = st[id].rsum = st[id].maxsum = key[l];
		st[id].left = st[id].right = st[id].a = st[id].b = l;
		return;
	}
	int mid = (l + r) >> 1;
	buildtree(lson);
	buildtree(rson);
	PushUp(id);
}

LL query(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)
	{
		s = st[id].a;
		t = st[id].b;
		return st[id].maxsum;
	}
	if(st[id<<1].r >= r)
	{
		return query(id<<1,l,r);
	}
	else if(st[id<<1|1].l <= l)
	{
		return query(id<<1|1,l,r);
	}
	else 
	{
		LL ans1 = query(id<<1,l,st[id<<1].r);
		LL ans2 = query(id<<1|1,st[id<<1|1].l,r);
		if(ans1 >= ans2)	query(id<<1,l,st[id<<1].r);
		LL ans = max(ans1,ans2);
		if((st[id<<1].rsum + st[id<<1|1].lsum > ans) || 
		   (st[id<<1].rsum + st[id<<1|1].lsum == ans && st[id<<1].right < s))
		{
			s = st[id<<1].right;
			t = st[id<<1|1].left;
		}
		ans = max(ans,st[id<<1].rsum + st[id<<1|1].lsum);
		return ans;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,cas = 0;
	while(scanf("%d%d",&n,&m)==2)
	{
		cas++;
		for(int i = 1;i <= n;i++)	scanf("%lld",&key[i]);
		buildtree(1,1,n);
		printf("Case %d:\n",cas);
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			query(1,u,v);
			printf("%d %d\n",s,t);
		}
	}
	return 0;
}


回过头来看。终于把它A了。。热泪盈眶。。。
线段树维护的信息:a表示最小x。b表示最小y。
                  left表示最大前缀和的终点,right表示最大后缀和的起点
                  lsum,rsum,maxsum表示最大前缀和,最大后缀和,最大连续和。
                  此题query函数得写三个~~返回类型用结构体~~跪烂了!!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 500080
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define LL long long int
LL key[maxn],Sum[maxn];//前缀和数组
struct ST
{
	int l,r,a,b,left,right;
	LL maxsum,sum,lsum,rsum;
}st[maxn<<2];

struct Node
{
	int l,r;
	LL sum;
};

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

void PushUp(int id)
{
	st[id].left = st[id<<1].left;
	st[id].right = st[id<<1|1].right;
	st[id].lsum = st[id<<1].lsum;
	st[id].rsum = st[id<<1|1].rsum;
	st[id].sum = st[id<<1].sum + st[id<<1|1].sum;
	LL ans1 = st[id<<1].maxsum,ans2 = st[id<<1|1].maxsum;
	if(ans1 >= ans2)
	{
		st[id].maxsum = st[id<<1].maxsum;
		st[id].a = st[id<<1].a;
		st[id].b = st[id<<1].b;
	}
	else 
	{
		st[id].maxsum = st[id<<1|1].maxsum;
		st[id].a = st[id<<1|1].a;
		st[id].b = st[id<<1|1].b;
	}
	//lsum,rsum,maxsum,a,b,left,right都有可能更改
	//lsum
	if(st[id<<1].sum + st[id<<1|1].lsum > st[id<<1].lsum)
	{
		st[id].lsum = st[id<<1].sum + st[id<<1|1].lsum;
		st[id].left = st[id<<1|1].left;
	}
	//rsum
	if(st[id<<1|1].sum + st[id<<1].rsum >= st[id<<1|1].rsum)
	{
		st[id].rsum = st[id<<1].rsum + st[id<<1|1].sum;
		st[id].right = st[id<<1].right;
	}
	//maxsum
	if((st[id<<1].rsum + st[id<<1|1].lsum > st[id].maxsum) || 
	  (st[id<<1].right < st[id].a && st[id<<1].rsum + st[id<<1|1].lsum == st[id].maxsum))
	{
		st[id].maxsum = st[id<<1].rsum + st[id<<1|1].lsum;
		st[id].a = st[id<<1].right;
		st[id].b = st[id<<1|1].left;
	}
}

void buildtree(int id,int l,int r)
{
	st[id].l = l,st[id].r = r;
	if(l == r)
	{
		st[id].sum = st[id].lsum = st[id].rsum = st[id].maxsum = key[l];
		st[id].left = st[id].right = st[id].a = st[id].b = l;
		return;
	}
	int mid = (l + r) >> 1;
	buildtree(lson);
	buildtree(rson);
	PushUp(id);
}

Node query_suffix(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)
	{
		Node a;
		a.l = st[id].right;;
		a.r = r;
		a.sum = st[id].rsum;
		return a;
	}
	if(st[id<<1].r >= r)
	{
		return query_suffix(id<<1,l,r);
	}
	else if(st[id<<1|1].l <= l)
	{
		return query_suffix(id<<1|1,l,r);
	}
	Node a = query_suffix(id<<1,l,st[id<<1].r);
	Node b = query_suffix(id<<1|1,st[id<<1|1].l,r);
	Node ans;
	ans.r = r;
	if(a.sum + Sum[r] - Sum[st[id<<1].r] >= b.sum)
	{
		ans.sum = a.sum + Sum[r] - Sum[st[id<<1].r];
		ans.l = a.l;
	}
	else 
	{
		ans.sum = b.sum;
		ans.l = b.l;
	}
	return ans;
}

Node query_pre(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)
	{
		Node a;
		a.l = l;
		a.r = st[id].left;
		a.sum = st[id].lsum;
		return a;
	}
	if(st[id<<1].r >= r)
	{
		return query_pre(id<<1,l,r);
	}
	else if(st[id<<1|1].l <= l)
	{
		return query_pre(id<<1|1,l,r);
	}
	Node a = query_pre(id<<1,l,st[id<<1].r);
	Node b = query_pre(id<<1|1,st[id<<1|1].l,r);
	Node ans;
	ans.l = l;
	if(st[id<<1].sum + b.sum > a.sum)
	{
		ans.sum = st[id<<1].sum + b.sum;
		ans.r = b.r;
	}
	else 
	{
		ans.sum = a.sum;
		ans.r = a.r;
	}
	return ans;
}

Node query(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)
	{
		Node a;
		a.l = st[id].a;
		a.r = st[id].b;
		a.sum = st[id].maxsum;
		return a;
	}
	if(st[id<<1].r >= r)
	{
		return query(id<<1,l,r);
	}
	else if(st[id<<1|1].l <= l)
	{
		return query(id<<1|1,l,r);
	}
	else 
	{
		Node ans1 = query(id<<1,l,st[id<<1].r);
		Node ans2 = query(id<<1|1,st[id<<1|1].l,r);
		Node a = query_suffix(id<<1,l,st[id<<1].r);
		Node b = query_pre(id<<1|1,st[id<<1|1].l,r);
		Node ans;
		if(ans1.sum >= ans2.sum)
		{
			ans.sum = ans1.sum;
			ans.l = ans1.l;
			ans.r = ans1.r;
		}
		else 
		{
			ans.sum = ans2.sum;
			ans.l = ans2.l;
			ans.r = ans2.r;
		}
		if(a.sum + b.sum > ans.sum || (a.sum + b.sum == ans.sum && a.l < ans.l))
		{
			ans.sum = a.sum + b.sum;
			ans.l = a.l;
			ans.r = b.r;
		}
		return ans;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,cas = 0;
	while(scanf("%d%d",&n,&m)==2)
	{
		cas++;
		Sum[0] = 0;
		for(int i = 1;i <= n;i++)	
		{
			scanf("%lld",&key[i]);
			Sum[i] = Sum[i-1] + key[i];
		}
		buildtree(1,1,n);
		printf("Case %d:\n",cas);
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			Node a = query(1,u,v);
			printf("%d %d\n",a.l,a.r);
		}
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值