POJ 2570 线段树

Potted Flower

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define ls rt<<1
#define rs rt<<1|1

#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)>(y)?(y):(x))
#define MAXN 100010

int N,M;
int a[MAXN];

struct seg_tree
{
    int l,r;
    int lmax,rmax,summax;
    int lmin,rmin,summin;
    int sum;
}tree[MAXN*3];

void PushUp(int rt)
{
    tree[rt].sum=tree[ls].sum+tree[rs].sum;

    tree[rt].lmax=MAX(tree[ls].lmax,tree[ls].sum+tree[rs].lmax);
    tree[rt].rmax=MAX(tree[rs].rmax,tree[rs].sum+tree[ls].rmax);
    tree[rt].summax=MAX(MAX(tree[ls].summax,tree[rs].summax),tree[ls].rmax+tree[rs].lmax);

    tree[rt].lmin=MIN(tree[ls].lmin,tree[ls].sum+tree[rs].lmin);
    tree[rt].rmin=MIN(tree[rs].rmin,tree[rs].sum+tree[ls].rmin);
    tree[rt].summin=MIN(MIN(tree[ls].summin,tree[rs].summin),tree[ls].rmin+tree[rs].lmin);

}

void build(int rt,int l,int r)
{
    if(l>r) return;
    tree[rt].l=l;
    tree[rt].r=r;
    int mid=(l+r)/2;
    if(l==r)
    {
        tree[rt].sum=a[l];
        tree[rt].lmax=tree[rt].rmax=tree[rt].summax=a[l];
        tree[rt].lmin=tree[rt].rmin=tree[rt].summin=a[l];
        return;
    }
    build(rt*2,l,mid);
    build(rt*2+1,mid+1,r);
    PushUp(rt);
}

void update(int rt,int pos,int val)
{
    if(tree[rt].l==tree[rt].r && tree[rt].l==pos)
    {
        tree[rt].sum=val;
        tree[rt].lmax=tree[rt].rmax=tree[rt].summax=val;
        tree[rt].lmin=tree[rt].rmin=tree[rt].summin=val;
        return;
    }

    int m=(tree[rt].l + tree[rt].r)>>1;
    if(pos<=m)
        update(rt*2,pos,val);
    else
        update(rt*2+1,pos,val);
    PushUp(rt);
}

int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=1;i<=N;i++)
            scanf("%d",&a[i]);
        build(1,1,N);

        scanf("%d",&M);
        int pos,val;

        while(M--)
        {
            scanf("%d%d",&pos,&val);
            update(1,pos,val);

            if(tree[1].sum==tree[1].summax)
                printf("%d\n",tree[1].sum-tree[1].summin);
            else
                printf("%d\n",MAX(tree[1].summax,tree[1].sum-tree[1].summin));
        }
    }
    return 0;
}

题意:给定一个环形序列,进行在线操作,每次修改一个元素,输出环上的最大连续子列的和,但不能是完全序列。

分析:

如果不是环的话,只是一个序列,用线段树很方便求,所以将环从某一点切开成一个序列。

那么答案的最大连续和可能包含断点(换种想法,包含断点时的最大连续和即为 sum-区间最小的连续和)

1,若是所有的数都大于0,那么最大连续和(必须断开一个)即为 总和sum-最小非空连续和。

2,若是区间最小连续和小于0,那么答案就是MAX(区间最大连续和,sum-最小非空连续和)。即分为包含断点和不包含的比较。

POJ 2570 线段树

时间: 2024-10-07 06:33:35

POJ 2570 线段树的相关文章

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj 2777 线段树的区间更新

Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Chosen Problem Solving and Program design as an optional course, you are required to solve al

转载::POJ 2991 线段树+计算几何(有c++结构体操作)

POJ 2991 线段树+计算几何 (2011-02-27 21:13:44) 转载▼ 标签: 杂谈 分类: OI 话说这一题真的是很恶心很恶心,不过确实改变了我对线段树的一些看法,算是很经典的题目. 题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度.然后有一些操作a. b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标. 首先要用到一个计算几何的知识(没学过..请教而来)

poj 2750(线段树的动态规划)

Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4186   Accepted: 1581 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 f

poj 2828 线段树

http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 并且后来的也可以确定----如果从前往后,所有的随时都不是确定的 2.线段树叶子节点直接维护区间(线段)信息,非叶子节点v维护的是以v为树根的整个子树的信息,那么假设父节点rt信息为[l,r]那么左子树维护[l,mid],右子树维护[mid+1,r]的信息.如果如果是前缀和,rt里是1-n的和,左子树1~n/2的和,右子树是n/2+1~n

POJ 3468 线段树+lazy标记

lazy标记 Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to

POJ 3667 线段树的区间合并简单问题

题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“中间”的空房间线段树操作:update:区间替换query:询问满足条件的最左端点 题目链接: http://vjudge.net/problem/viewProblem.action?id=10354 题目操作: 因为这里找从最左边住起的房间,所以这里不能像常规地写query函数那样写了,终于发现

poj 2886 线段树的更新+反素数

Who Gets the Most Candies? Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description N children are sitting in a circle to play a game. The children are numbered from

poj 3468 线段树成段更新

http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58132   Accepted: 17704 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two k