POJ 2750 线段树 题解

Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4837 Accepted: 1841

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

Source

POJ Monthly–2006.01.22,Zeyuan Zhu

【解题报告】
题目大意:有一个长度为n的数列,它们首尾相连(也就是一个环),现在给你一个修改的操作(把一个位置上的值换成另一个),求这环上数值的和做大的一段区间(区间长度不能为n)。
思路的话看到数据范围就晓得是用数据结构的知识。我们把环从一个地方,切断拉成一条直线,用线段树记录当前区间的非空最大子列和当前区间的非空最小子列。
考虑到我们所需要求和区间可能在被拆成了两截,所以要注意处理的方式。
网上简单的解释:如果环上的数都是正整数,答案是:环上数的总和-根结点的非空最小子列;
否则,答案是:max{根结点的非空最大子列,环上数的总和-根结点的非空最小子列},每次问答的复杂度是O(logN)。
DP转移方程很玄学。。。
感觉自己数据结构的知识掌握的不好,看到一道题很多时候都不知道怎么下手,只有去看别人的题解。。。
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010

struct TNode
{
    int sum,maxn,minn;//当前区间的和、最小子序列和、最大子序列和
    int lmin,lmax;// 当前区间从left出发所能形成的 最小子序列和、最大子序列和
    int rmin,rmax;// 当前区间以right结尾所能形成的 最小子序列和、最大子序列和
    int left,right;
}T[N*3];
int n,m;
int p[N];
void update(int r)
{
    T[r].sum=T[2*r].sum+T[2*r+1].sum;
    T[r].lmax=max(T[2*r].lmax, T[2*r].sum+T[2*r+1].lmax);
    T[r].rmax=max(T[2*r+1].rmax,T[2*r].rmax+T[2*r+1].sum);
    T[r].maxn=max(max(T[2*r].maxn,T[2*r+1].maxn),T[2*r].rmax+T[2*r+1].lmax);
    T[r].lmin=min(T[2*r].lmin,T[2*r].sum+T[2*r+1].lmin);
    T[r].rmin=min(T[2*r+1].rmin,T[2*r].rmin+T[2*r+1].sum);
    T[r].minn=min(min(T[2*r].minn,T[2*r+1].minn), T[2*r].rmin+T[2*r+1].lmin);
}
void build(int st,int ed,int r)
{
    T[r].left=st,T[r].right=ed;
    if(st==ed)// 叶子节点
    {
        T[r].sum=T[r].maxn=T[r].minn=p[ed];
        T[r].lmax=T[r].lmin=T[r].rmax=T[r].rmin=p[ed];
        return;
    }
    int mid=(st+ed)>>1;
    build(st,mid,2*r);
    build(mid+1,ed,2*r+1);
    update(r);
}
void modify(int adr,int r)
{
    if(T[r].left==T[r].right)//到达叶节点
    {
        T[r].sum=T[r].maxn=T[r].minn=p[0];
    T[r].lmin=T[r].lmax=T[r].rmin=T[r].rmax=p[0];
        return;
    }
    int mid=(T[r].left+T[r].right)>> 1;
    if(adr<= mid)
    modify(adr,2*r);
    else
    modify(adr,2*r+1);
    update(r);// 更新整棵线段树
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&p[i]);
    build(1,n,1);
    scanf("%d",&m);
    while(m--)
    {
        int nwp,ans=0;
        scanf("%d%d",&nwp,&p[0]);
        modify(nwp,1);
        if(T[1].sum==T[1].maxn)
        ans=T[1].sum-T[1].minn;
        else
        ans=max(T[1].maxn,T[1].sum-T[1].minn);
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值