Maximum field and -- gss1 mushroom orz

Source: Internet
Author: User

Too much naive = as a knowledge point to sum up. Merge. For the maximum field and of an interval, you can separate the subsegments and of the two intervals below, or add the left side of the interval to the right side. Then let's figure out =

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long long ll;const ll maxn = 3000100;ll lson[maxn], rson[maxn], lm[maxn], rm[maxn], mans[maxn], sum[maxn];ll ne = 0, root;ll build(ll l, ll r) {    ll now = ++ ne;    if(l ^ r) {        ll mid = (l + r) >> 1;        lson[now] = build(l, mid);        rson[now] = build(mid + 1, r);    }    return now;}void update(ll now) {    if(lson[now]) {        sum[now] = sum[lson[now]] + sum[rson[now]];        lm[now] = max(lm[lson[now]], sum[lson[now]] + lm[rson[now]]);        rm[now] = max(rm[rson[now]], sum[rson[now]] + rm[lson[now]]);        mans[now] = max(max(mans[lson[now]], mans[rson[now]]), lm[rson[now]] + rm[lson[now]]);    }}void insert(ll now, ll l, ll r, ll v, ll pos) {    if(l == r) {        sum[now] = lm[now] = rm[now] = mans[now] = v;    }    else {        ll mid = (l + r) >> 1;        if(pos <= mid) insert(lson[now], l, mid, v, pos);        else insert(rson[now], mid + 1, r, v, pos);        update(now);    }}ll ask(ll now, ll l, ll r, ll ls, ll rs) {    ll ret;    if(l == ls && r == rs) ret = now;    else {        ll mid = (l + r) >> 1;        if(rs <= mid) ret = ask(lson[now], l, mid ,ls, rs);        else if(ls >= mid + 1) ret = ask(rson[now], mid + 1, r, ls, rs);        else {            ret = ++ ne;            ll rl = ask(lson[now], l, mid, ls, mid);            ll rr = ask(rson[now], mid + 1, r, mid + 1, rs);            lson[ret] = rl, rson[ret] = rr;            update(ret);        }    }    return ret;}ll ll_get() {    ll x = 0; char c = (char)getchar(); bool flag = 0;    while(!isdigit(c) && c != ‘-‘) c = (char)getchar();    if(c == ‘-‘) flag = 1;    while(!isdigit(c)) c = (char)getchar();    while(isdigit(c)) {        x = x * 10 + (ll)(c - ‘0‘);        c = (char)getchar();    }    if(flag) x = -x;    return x;}ll n, m;void sov() {    n = ll_get(); root = build(1, n);    for(ll i = 1; i <= n; ++ i) {        ll a = ll_get();        insert(root, 1, n, a, i);    }    m = ll_get();    while(m --) {        ll ls, rs; ls = ll_get(), rs = ll_get();        ll ans = ask(root, 1, n, ls, rs);        printf("%lld\n", mans[ans]);    }}int main() {    //freopen("test.in", "r", stdin);    //freopen("b.out", "w", stdout);    sov();    return 0;}

 

Maximum field and -- gss1 mushroom orz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.