SP1716 GSS3 - Can you answer these queries III

题意翻译

n 个数,q 次操作

操作0 x y把A_xAx 修改为yy

操作1 l r询问区间[l, r] 的最大子段和

依旧是维护最大子段和,还是再敲一遍比较好。

code:

#include<iostream>
#include<cstdio>
#define ls(o) o<<1
#define rs(o) o<<1|1
using namespace std;
const int wx=100017;
inline int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
return sum*f;
}
struct val_tree{
int l,r,lsum,rsum,sum,tot;
#define sum(o) t[o].sum
#define lsum(o) t[o].lsum
#define rsum(o) t[o].rsum
#define tot(o) t[o].tot
}t[wx*4];
int a[wx];
int n,m;
void up(int o){
tot(o)=tot(ls(o))+tot(rs(o));
lsum(o)=max(lsum(ls(o)),tot(ls(o))+lsum(rs(o)));
rsum(o)=max(rsum(rs(o)),tot(rs(o))+rsum(ls(o)));
sum(o)=max(rsum(ls(o))+lsum(rs(o)),max(sum(ls(o)),sum(rs(o))));
}
void build(int o,int l,int r){
t[o].l=l;t[o].r=r;
if(l==r){sum(o)=lsum(o)=rsum(o)=tot(o)=a[l];return ;}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)build(ls(o),l,mid);
if(r>mid)build(rs(o),mid+1,r);
up(o);
}
void update(int o,int l,int r,int k){
if(l<=t[o].l&&t[o].r<=r){
sum(o)=lsum(o)=rsum(o)=tot(o)=k;
return ;
}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)update(ls(o),l,r,k);
if(r>mid)update(rs(o),l,r,k);
up(o);
}
val_tree query(int o,int l,int r){
if(l<=t[o].l&&t[o].r<=r){
return t[o];
}
int mid=t[o].l+t[o].r>>1;
if(r<=mid)return query(ls(o),l,r);
if(l>mid)return query(rs(o),l,r);
val_tree tmp,tmp1,tmp2;
tmp1=query(ls(o),l,r);
tmp2=query(rs(o),l,r);
tmp.tot=tmp1.tot+tmp2.tot;
tmp.lsum=max(tmp1.lsum,tmp1.tot+tmp2.lsum);
tmp.rsum=max(tmp2.rsum,tmp2.tot+tmp1.rsum);
tmp.sum=max(tmp1.rsum+tmp2.lsum,max(tmp1.sum,tmp2.sum));
return tmp;
}
int main(){
n=read();
for(int i=1;i<=n;i++)a[i]=read();
build(1,1,n);
m=read();
for(int i=1,x,y,opt;i<=m;i++){
opt=read();x=read();y=read();
if(opt)printf("%d\n",query(1,x,y).sum);
else update(1,x,x,y);
}
return 0;
}

线段树 SP1716 GSS3 - Can you answer these queries III的更多相关文章

  1. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  2. SP1716 GSS3 - Can you answer these queries III

    题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...

  3. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

  4. SP1716 GSS3 - Can you answer these queries III(单点修改,区间最大子段和)

    题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx​ 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are give ...

  5. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  6. 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III

    GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...

  7. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  8. 【SP1716】GSS3 - Can you answer these queries III(动态DP)

    题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ...

  9. 题解 SP1716 【GSS3 - Can you answer these queries III】

    \[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...

随机推荐

  1. java ----获取路径的各种方法(总结)

    Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 那么针对这个站点的几个基本概念表述如下: 1. web站点的根目录:http: ...

  2. [phonegap]安装升级

    安装 npm install -g phonegapnpm install -g phonegap@版本号    比如 npm install -g phonegap@3.3.0-0.19.6 升级 ...

  3. jackson 进行json与java对象转换 之三

    2.测试类,没用Junit,用Main()方法输出. package test; import java.io.IOException; import java.util.ArrayList; imp ...

  4. list array解析(总算清楚一点了)

    # -*- coding: utf-8 -*- """ Created on Tue Aug 09 23:04:51 2016 @author: Administrato ...

  5. java虚拟机垃圾回收机制详解

    首先,看一下java虚拟机运行的时候内存分配图: jvm虚拟机栈:一个是线程独有的,每次启动一个线程,就创建一个jvm虚拟机栈,线程退出的时候就销毁.这里面主要保存线程本地变量名和局部变量值. 本地方 ...

  6. day17 11.JdbcUtils工具抽取

    连接数据库的四个必要条件:driverclass.url.username.password. package cn.itcast.utils; import java.sql.Connection; ...

  7. Codeforces 1137C Museums Tour (强连通分量, DP)

    题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...

  8. ROS Learning-020 learning_tf-04(编程)让turtle2 海龟跟随turtle1海龟,并绕着 turtle1海龟转圈 (Python版)

    ROS Indigo learning_tf-04 (编程)让 turtle2 海龟跟随 turtle1 海龟,并绕着 turtle1 海龟转圈 (Python版) 我使用的虚拟机软件:VMware ...

  9. 杭电acm 1039题

    这道题也比较简单,写三个函数判断三个条件即可..... 但是开始时我按照已经注释掉的提交,居然提示WA,我百思不得其解,后改成上面的判断式就可以了,求高手解答.... #include "i ...

  10. QGraphicsScene绘制网格背景

    博客转载自:https://blog.csdn.net/u010177010/article/details/51496038 //两条轴线QPolygonF myPolygon1; myPolygo ...