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

来源:互联网 发布:mac安装jdk 编辑:程序博客网 时间:2024/06/05 10:08
Potted Flower
Description
The little cat takesover the management of a new park. There is a large circular statuein the center of the park, surrounded by N pots of flowers. Eachpotted flower will be assigned to an integer number (possiblynegative) denoting how attractive it is. See the following graph asan example:

(Positions of potted flowers are assigned to index numbers in therange of 1 ... N. The i-th pot and the (i + 1)-th pot areconsecutive 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 "ONEarc-style cane-chair" for tourists having a rest, and the sum ofattractive values of the flowers beside the cane-chair should be aslarge as possible. You should notice that a cane-chair cannot be atotal circle, so the number of flowers beside the cane-chair may be1, 2, ..., N - 1, but cannot be N. In the above example, if weconstruct a cane-chair in the position of that red-dashed-arc, wewill have the sum of 3+(-2)+1+2=4, which is the largest among allpossible constructions.

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

Input

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

The second line contains N integers, which are the initialattractive value of each potted flower. The i-th number is for thepotted 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 aninstruction "A B" in the form described above.

Restriction: All the attractive values are within [-1000, 1000]. Weguarantee the maximal sum will be always a positiveinteger.

Output

For eachinstruction, output a single line with the maximum sum ofattractive 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;
}

0 0