#POJ2750#Potted Flower(线段树)

30 篇文章 0 订阅
12 篇文章 0 订阅

Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4883 Accepted: 1861

Description

The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer number (possibly negative) denoting how attractive it is. See the following graph as an example: 

(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.) 



The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot be a total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest among all possible constructions. 

Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which means changing the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction. 

Input

There will be a single test data in the input. You are given an integer N (4 <= N <= 100000) in the first input line. 

The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position. 

A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above. 

Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer. 

Output

For each instruction, output a single line with the maximum sum of attractive values for the optimum cane-chair.

Sample Input

5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1

Sample Output

4
4
3
5

题意:

给出一个环,求环上最大的连续子串的和,不能包含整个环


本来这节课是讲优化DP的,然后我并没想出来,就开始乱搞,居然还做出来了,很开心。

分为两种情况:

一:不同时包含1和N的时候,直接求最大连续和,用线段树维护

(此处需注意的是,线段树维护出的很可能同时包含1和N,但是这时必定全是非负数,所以只需减去最小连续和即可)

二:同时包含1和N的时候,求一个最小连续和,再用全部数的和减去它,用线段树维护


Status Accepted
Time 547ms
Memory 9888kB
Length 1929
Lang G++
Submitted
Shared

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int Max = 100005;

struct node{
	int l, r;
	int lmax, rmax, lmin, rmin;
	int sum, sumax, sumin;
}Tr[Max << 2];

int N, M;

int getint(){
    char c;	int flag = 1, num = 0;
    while((c = getchar()) < '0' || c > '9')	if(c == '-')	flag = -1;
    while(c >= '0'&& c <= '9'){	num = num * 10 + c - 48; c = getchar();}
    num *= flag;
	return num;
}

void pushup(int i){
	Tr[i].sum = Tr[i << 1].sum + Tr[i << 1 | 1].sum;
	Tr[i].sumax = max(max(Tr[i << 1].sumax, Tr[i << 1 | 1].sumax), Tr[i << 1].rmax + Tr[i << 1 | 1].lmax);
	Tr[i].sumin = min(min(Tr[i << 1].sumin, Tr[i << 1 | 1].sumin), Tr[i << 1].rmin + Tr[i << 1 | 1].lmin);
	Tr[i].lmax = max(Tr[i << 1].lmax, Tr[i << 1].sum + Tr[i << 1 | 1].lmax);
	Tr[i].rmax = max(Tr[i << 1 | 1].rmax, Tr[i << 1 | 1].sum + Tr[i << 1].rmax);
	Tr[i].lmin = min(Tr[i << 1].lmin, Tr[i << 1].sum + Tr[i << 1 | 1].lmin);
	Tr[i].rmin = min(Tr[i << 1 | 1].rmin, Tr[i << 1 | 1].sum + Tr[i << 1].rmin);
}

void build_tr(int i, int l, int r){
	Tr[i].l = l, Tr[i].r = r;
	if(l == r){
		int x = getint();
		Tr[i].lmax = Tr[i].lmin = Tr[i].rmax = Tr[i].rmin = x;
		Tr[i].sum = Tr[i].sumax = Tr[i].sumin = x;
		return ;
	}
	int mid = (l + r) >> 1;
	build_tr(i << 1, l ,mid);
	build_tr(i << 1 | 1, mid + 1, r);
	pushup(i);
}

void insert(int i, int p, int x){
	if(Tr[i].l > p || Tr[i].r < p)	return ;
	if(Tr[i].l == p && Tr[i].r == p){
		Tr[i].lmax = Tr[i].rmax = Tr[i].lmin = Tr[i].rmin = x;
		Tr[i].sum = Tr[i].sumax = Tr[i].sumin = x;
		return ;
	}
	insert(i << 1, p, x);
	insert(i << 1 | 1, p, x);
	pushup(i);
}

int main(){
	N = getint();
	build_tr(1, 1, N);
	M = getint();
	int x, p, Ans;
	while(M --){
		p = getint(), x = getint();
		insert(1, p, x);
		Ans = max(Tr[1].sumax, Tr[1].sum - Tr[1].sumin);
		if(Ans == Tr[1].sum)	printf("%d\n", Ans - Tr[1].sumin);
		else printf("%d\n", Ans);
	}
	return 0;
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值