ACM: 线段树 poj 2750 连续最大和

小猫在公园布局花卉,通过线段树算法优化花卉布局以获得最大观赏价值,同时处理变动情况。
Potted Flower
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.)

ACM: <wbr>线段树 <wbr>poj <wbr>2750 <wbr>连续最大和


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

题意: Little cat 要布置一个circle形状的花卉, 圆形的花卉由每一个pot构成, 并且每一个pot有一个

      观赏值.最后一个圆形花卉的观赏值是计算连续最大子段和.不过这题的最大连续和是最多是(N-1)个;

      即不是整个环.

解题思路:

   1. 最大连续和问题, 主要是处理连续问题, 再保持最大值. 线段树的节点相应的要设置几个域:

      maxsum: 段区间最大连续和;

      minsum: 段区间最小连续和

      sum: 段区间连续和.

      lmax, rmax: 当前段区间, 左右子树的最大和

      lmin, rmin: 当前段区间, 左右子树的最小和

   2. 维护一棵线段树成为了关键, 即是怎么将上述7个域怎么在改变叶子节点之后相应改变.

    p->maxsum = max( p->left->rmax+p->right->lmax, max(p->left->maxsum, p->right->maxsum) )

   ACM: <wbr>线段树 <wbr>poj <wbr>2750 <wbr>连续最大和

   (选择左子树的右部连续最大值+右子树的左部连续最大值, 还是左子树最大连续和, 否则右子树最大值)

   p->minsum类似最大连续和, 就是求最小值.

   p->lmax = max(p->left->lmax, p->left->sum+p->right->lmax);

  (当前节点的左部连续最大, 要不是左子树的左部连续最大, 否则左子树区间连续和+右子树的左部最大值)

   同理 p->rmax = max(p->right->rmax, p->right->sum+p->left->rmax);

        p->lmin = min(p->left->lmin, p->left->sum+p->right->lmin);

        p->rmin = min(p->right->rmin, p-right->sum+p->left->rmin);

   p->sum = p->left->sum + p->right->sum;

   这样7个域都可以再更新时, 相应的改变. 简单的说, 上述的变化时为了保证"连续性".

   3. 最后剩下一个问题, 当root->maxsum == root->sum时, 说明计算最大连续和时, 将全部N个值都加上

      了, 这样需要减去其中最小的一个连续和root->minsum.显然, 因为一个环, 任意去掉区间中, 连续和

      最小的那段, 依然保持了"连续性". 并且求出最大值.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 100005

struct node
{
 int l, r;
 int maxsum, minsum, sum;
 int lmax, rmax;
 int lmin, rmin;
}pt[MAX*4];

int n, m;
int a[MAX];
int A, B;

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

inline int min(int a, int b)
{
 return a < b ? a : b;
}

inline void change1(int pos, int val)
{
 pt[pos].minsum = pt[pos].maxsum = pt[pos].sum = val;
 pt[pos].lmax = pt[pos].rmax = val;
 pt[pos].lmin = pt[pos].rmin = val;
}

inline void change2(int pos)
{
 pt[pos].sum = pt[pos*2].sum+pt[pos*2+1].sum;
 pt[pos].maxsum = max( pt[pos*2].rmax+pt[pos*2+1].lmax, max(pt[pos*2].maxsum, pt[pos*2+1].maxsum) );
 pt[pos].minsum = min( pt[pos*2].rmin+pt[pos*2+1].lmin, min(pt[pos*2].minsum, pt[pos*2+1].minsum) );
 
 pt[pos].lmax = max(pt[pos*2].lmax, pt[pos*2].sum+pt[pos*2+1].lmax);
 pt[pos].rmax = max(pt[pos*2+1].rmax, pt[pos*2+1].sum+pt[pos*2].rmax);

 pt[pos].lmin = min(pt[pos*2].lmin, pt[pos*2].sum+pt[pos*2+1].lmin);
 pt[pos].rmin = min(pt[pos*2+1].rmin, pt[pos*2+1].sum+pt[pos*2].rmin);
}

void buildTree(int l, int r, int pos)
{
 pt[pos].l = l, pt[pos].r = r;
 if(l == r)
 {
  change1(pos, a[l]);
  return ;
 }
 int mid = (l+r)/2;
 buildTree(l, mid, pos*2);
 buildTree(mid+1, r, pos*2+1);
 change2(pos);
}

void insert(int A, int B, int pos)
{
 if(pt[pos].l == A && pt[pos].r == A)
 {
  change1(pos, B);
  return ;
 }

 int mid = (pt[pos].l+pt[pos].r)/2;
 if(A <= mid)
  insert(A, B, pos*2);
 else
  insert(A, B, pos*2+1);
 change2(pos);
}

int main()
{
 int i;
// freopen("input.txt","r",stdin);
 while(scanf("%d",&n) != EOF)
 {
  for(i = 1; i <= n; ++i)
   scanf("%d",&a[i]);
  buildTree(1, n, 1);

  scanf("%d",&m);
  int result;
  for(i = 1; i <= m; ++i)
  {
   scanf("%d %d",&A, &B);
   insert(A, B, 1);
   
   result = pt[1].maxsum;
   if(pt[1].maxsum == pt[1].sum)
    result = pt[1].sum-pt[1].minsum;
   result = max(result, pt[1].sum-pt[1].minsum);
   printf("%d\n", result);
  }
 }

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值