SPOJ-GSS1-Can you answer these queries 1

link:

https://vjudge.net/problem/SPOJ-GSS1

Meaning of the questions:

You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows:
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.
Given M queries, your program must output the results of these queries.
区间最大子段和

Ideas:

Maintenance segment tree, interval sum, maximum and intermediate sections, the left section at the maximum point as a starting point and, in order to have a range of maximum and terminating end.
Upwardly maintain code

void PushUp(int root)
{
    Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
    //区间和
    Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
    //区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
    Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
    //区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
    Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
    //区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
}

Learned a new query operation returns a structure, very good use

Code:

/*
 *线段树维护区间最大子段和
 * 模板
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MAXN = 5e4+10;
const int INF = 1e9+10;
const int NINF = -1e9;

struct SegmentTree
{
    LL sum;//区间全部和
    LL midmax;//区间中间值最大和
    LL lmax;//以左端点为起点的最大和
    LL rmax;//以右端点为终点的最大和
}Seg[MAXN*4];
LL a[MAXN];
int n, q;

void PushUp(int root)
{
    Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
    //区间和
    Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
    //区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
    Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
    //区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
    Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
    //区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
}

void Build(int root, int l, int r)
{
    if (l == r)
    {
        Seg[root].sum = Seg[root].midmax = Seg[root].lmax = Seg[root].rmax = a[l];
        return;
    }
    int mid = (l+r)/2;
    Build(root<<1, l, mid);
    Build(root<<1|1, mid+1, r);
    PushUp(root);
}

SegmentTree Query(int root, int l, int r, int ql, int qr)
{
    SegmentTree lt = {NINF, NINF, NINF, NINF}, rt = {NINF, NINF, NINF, NINF}, re = {NINF, NINF, NINF, NINF};
    if (ql <= l && r <= qr)
        return Seg[root];
    int mid = (l+r)/2;
    if (ql <= mid)
        lt = Query(root<<1, l, mid, ql, qr);
    if (qr > mid)
        rt = Query(root<<1|1, mid+1, r, ql, qr);
    re.sum = lt.sum+rt.sum;
    re.midmax = max(lt.rmax+rt.lmax, max(lt.midmax, rt.midmax));
    re.lmax = max(lt.sum+rt.lmax, lt.lmax);
    re.rmax = max(rt.sum+lt.rmax, rt.rmax);
    return re;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++)
        cin >> a[i];
    Build(1, 1, n);
    cin >> q;
    int l, r;
    while (q--)
    {
        cin >> l >> r;
        SegmentTree res = Query(1, 1, n, l, r);
        cout << max(res.midmax, max(res.lmax, res.rmax)) << endl;
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11319000.html