线段树练习:【SPOJ1716】 GSS3

原题:http://www.spoj.com/problems/GSS3/

SPOJ Problem Set (classical)

1716. Can you answer these queries III

Problem code: GSS3

You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: 
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.

Input

The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN. 
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.

Output

For each query, print an integer as the problem required.

Example

Input:
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3

Output:
6
4
-3

Added by:Bin Jin
Date:2007-08-03
Time limit:0.330s
Source limit:5000B
Memory limit:1536MB
Cluster:Cube (Intel Pentium G860 3GHz)
Languages:All except: C++ 4.9 SCM chicken
Resource:own problem







#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int MAXD = 10010, INF = 0x3f3f3f3f;

struct Seg
{
    int mid, l, r;
};

Seg seg[4 * MAXD];

int A[MAXD], a[MAXD];

void update(int cur, int x, int y)
{
    int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1;
    seg[cur].mid = max(seg[ls].mid, seg[rs].mid);
    seg[cur].mid = max(seg[cur].mid, seg[ls].r + seg[rs].l);
    seg[cur].l = max(seg[ls].l, A[mid] - A[x - 1] + seg[rs].l);
    seg[cur].r = max(seg[rs].r, A[y] - A[mid] + seg[ls].r);
}

void build(int cur, int x, int y)
{
    int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1;
    if (x == y)
    {
        seg[cur].mid = seg[cur].l = seg[cur].r = a[x];
        return ;
    }
    build(ls, x, mid);
    build(rs, mid + 1, y);
    update(cur, x, y);
}

int query(int cur, int x, int y, int s, int t, int flag, int &ans)
{
    int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1;
    if (x >= s && y <= t)
    {
        ans = max(ans, seg[cur].mid);
        if (flag == 0) return seg[cur].l;
        else return seg[cur].r;
    }
    if (mid >= t) return query(ls, x, mid, s, t, 0, ans);
    else if (mid + 1 <= s) return query(rs, mid + 1, y, s, t, 1, ans);

    int ln, rn;
    ln = query(ls, x, mid, s, t, 1, ans);
    rn = query(rs, mid + 1, y, s, t, 0, ans);
    ans = max(ans, ln + rn);

    if (flag == 0) return max(seg[ls].l, A[mid] - A[x - 1] + rn);
    else return max(seg[rs].r, A[y] - A[mid] + ln);
}

void Search(int cur, int x, int y, int s, int t, int flag, int &ans)
{
    int mid = (x + y) >> 1, ls = cur << 1, rs = cur << 1 | 1;
    if (x >= s && y <= t)
    {
        if (flag == 0) ans = max(ans, A[x - 1] - A[s - 1] + seg[cur].l);
        else ans = max(ans, A[t] - A[y] + seg[cur].r);
        return;
    }
    if (mid >= s) Search(ls, x, mid, s, t, flag, ans);
    if (mid + 1 <= t) Search(rs, mid + 1, y, s, t, flag, ans);
}

int main(void)
{
    int N, q, ans, t1, t2, x1, y1, x2, y2;

    scanf("%d", &N);
    A[0] = 0;
    for (int i = 1; i <= N; i ++)
    {
        scanf("%d", &a[i]);
        A[i] = A[i - 1] + a[i];
    }
    build(1, 1, N);

    scanf("%d", &q);
    for (int i = 0; i < q; i ++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if (y1 < x2)
        {
            t1 = t2 = -INF;
            Search(1, 1, N, x1, y1, 1, t1);
            Search(1, 1, N, x2, y2, 0, t2);
            printf("%d\n", t1 + t2 + A[x2 - 1] - A[y1]);
        }
        else
        {
            ans = -INF;
            query(1, 1, N, x2, y1, 0, ans);
            t1 = t2 = -INF;

            Search(1, 1, N, x1, y1, 1, t1);
            Search(1, 1, N, y1, y2, 0, t2);

            ans = max(ans, t1 + t2 - a[y1]);
            t1 = t2 = -INF;

            Search(1, 1, N, x1, x2, 1, t1);
            Search(1, 1, N, x2, y2, 0, t2);

            ans = max(ans, t1 + t2 - a[x2]);
            printf("%d\n", ans);
        }
    }

    return 0;
}

与GSS1类似,单点更新直到叶子,然后一层层PushUp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值