Monday, 13 August 2012

Spoj 3693. Maximum Sum Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 110000

struct Node{
    LL mx,sum;
};

Node nd[MX*4];

int lazy_value[4*MX];
bool lazy[4*MX];

void update_node(int idx, int st, int ed, LL val)
{
    lazy[idx]=1;
    lazy_value[idx]=val;

    nd[idx].mx=val;
    nd[idx].sum=val;
}

void update_lazy(int idx, int st, int ed)
{
    if(st==ed)    return;

    int mid=(st+ed)/2;

    update_node(2*idx, st, mid, lazy_value[idx]);
    update_node(2*idx+1, mid+1, ed, lazy_value[idx]);
    lazy[idx]=lazy_value[idx]=0;
}

void insert(int idx, int st, int ed, int s, int e, LL val)
{
    if(st==s && ed==e)
    {
        update_node(idx, st, ed, val);
        return;
    }

    //if(lazy[idx]) update_lazy(idx, st, ed);

    int mid=(st+ed)/2;
    if(e<=mid) insert(idx*2, st, mid, s, e, val);
    else insert(idx*2+1, mid+1, ed, s, e, val);
    /*else
    {
        insert(idx*2, st, mid, s, mid, val);
        insert(idx*2+1, mid+1, ed, mid+1, e, val);
    }*/
    //mx[idx]=max(mx[idx*2], mx[idx*2+1]);
    nd[idx].mx = max(nd[idx*2].mx, nd[idx*2+1].mx);
    nd[idx].sum = max3(nd[idx*2].sum, nd[idx*2+1].sum, nd[idx*2].mx + nd[idx*2+1].mx);
    //n[idx].sum = max(n[idx.sum], ;
    return;
}

Node query(int idx, int st , int ed, int s, int e)
{
    if(st==s && ed==e) return nd[idx];

    //if(lazy[idx]) update_lazy(idx, st, ed);

    int mid=(st+ed)/2;
    if(mid>=e)  return query(2*idx, st, mid, s, e);
    else if(s>mid)  return query(2*idx+1, mid+1, ed, s, e);
    else    //return max(query(idx*2, st, mid, s, mid), query(idx*2+1, mid+1, ed, mid+1, e));
    {
        Node ret,a,b;
        a = query(idx*2, st, mid, s, mid);
        b = query(idx*2+1, mid+1, ed, mid+1, e);

        ret.mx = max(a.mx, b.mx);
        ret.sum = max3(a.sum, b.sum, a.mx + b.mx);

        return ret;
    }
}

int main()
{
    int n, i, q, cnt, st, ed;
    LL val;
    char c;
   // string s_line;

    //cin>>n;
    scanf("%d",&n);

    for(i=1;i<=n;i++)
        {
            //cin>>val;
            scanf("%lld",&val);
            insert(1, 1, n, i, i, val);
        }

    //cin>>q;
    scanf("%d",&q);


    while(q--)
    {
        getchar();
        //cin>>c;
        //cin>>st>>ed;
        scanf("%c%d%",&c,&st);

        if(c=='U')
        {
                scanf("%lld",&val);
                insert(1, 1, n, st, st, val);
        }

        else
        {
            scanf("%d",&ed);
            printf("%lld\n",query(1, 1, n, st, ed).sum);
                //cout<<<<endl;

        }
    }

    return 0;
}

No comments:

Post a Comment