zoukankan      html  css  js  c++  java
  • SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3

    Description

    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

    GSS1加个单点修改
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define m ((l+r)>>1)
    #define lson o<<1,l,m
    #define rson o<<1|1,m+1,r
    #define lc o<<1
    #define rc o<<1|1
    using namespace std;
    typedef long long ll;
    const int N=5e5+5,INF=2e9+5;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,q,op,x,y;
    struct node{
        int sum,mx,pre,suf;
    }t[N<<2];
    void merge(int o){
        t[o].sum=t[lc].sum+t[rc].sum;
        t[o].mx=max(t[lc].suf+t[rc].pre,max(t[lc].mx,t[rc].mx));
        t[o].pre=max(t[lc].pre,t[lc].sum+t[rc].pre);
        t[o].suf=max(t[rc].suf,t[rc].sum+t[lc].suf);
    }
    void build(int o,int l,int r){
        if(l==r) t[o].sum=t[o].mx=t[o].pre=t[o].suf=read();
        else{
            build(lson);
            build(rson);
            merge(o);
        }
    }
    int qpre(int o,int l,int r,int ql,int qr){
        if(ql<=l&&r<=qr) return t[o].pre;
        else if(qr<=m) return qpre(lson,ql,qr);
        else return max(qpre(lson,ql,qr),t[lc].sum+qpre(rson,ql,qr));
    }
    int qsuf(int o,int l,int r,int ql,int qr){
        if(ql<=l&&r<=qr) return t[o].suf;
        else if(m<ql) return qsuf(rson,ql,qr);
        else return max(t[rc].suf,t[rc].sum+qsuf(lson,ql,qr));
    }
    int qmx(int o,int l,int r,int ql,int qr){
        if(ql<=l&&r<=qr) return t[o].mx;
        else{
            int ans=-INF;
            if(ql<=m) ans=max(ans,qmx(lson,ql,qr));
            if(m<qr) ans=max(ans,qmx(rson,ql,qr));
            if(ql<=m&&m<qr) ans=max(ans,qsuf(lson,ql,qr)+qpre(rson,ql,qr));
            return ans;
        }
    }
    void update(int o,int l,int r,int p,int v){
        if(l==r) t[o].sum=t[o].mx=t[o].pre=t[o].suf=v;
        else{
            if(p<=m) update(lson,p,v);
            else update(rson,p,v);
            merge(o);
        }
    }
    int main(){
        n=read();
        build(1,1,n);
        q=read();
        for(int i=1;i<=q;i++){
            op=read();x=read();y=read();
            if(op) printf("%d
    ",qmx(1,1,n,x,y));
            else update(1,1,n,x,y);
        }
    }
     
  • 相关阅读:
    [Jweb] Servlet 生命周期, TestLifeCycleServlet extends HttpServlet
    [Java] Javadoc jdk 下载地址
    [Jweb] 在 servlet 中重写 service() 方法,不重写 doGet, doPost 方法,示例。
    [Jweb] Servlet / GenericServlet / HttpServlet 及其方法示意图
    [Jweb] 第一个通过 tomcat 配置,浏览器访问的 web 界面
    [Jweb] tomcat 知识点 (from bjsxt ZZY)
    [Jweb] HTTP 1.1与HTTP 1.0的比较 (from bjsxt 张志宇)
    post方式提交数据
    php页面获取数据库中的数据
    表单输入框样式
  • 原文地址:https://www.cnblogs.com/candy99/p/6068078.html
Copyright © 2011-2022 走看看