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. #define和const的区别

    下面使用#define和const定义常量: #define n_define 10 int main(int argc, char* argv[],int _version) { ; int *p= ...

  2. sysfs: cannot create duplicate filename '/class/spi_master/spi1'

    在编写SPI驱动程序的时候,遇到如下问题 s3c2410-spi s3c2410-spi.0: master is unqueued, this is deprecated ------------[ ...

  3. web页面导出到Excel乱码解决

    引言: 前几天 在做web项目的时候 需要导出页面上的数据 到Excel里面 但有的时候出现乱码(有de时候不出现 很奇怪) 原来的代码是这样的: HttpContext.Current.Respon ...

  4. Android逆向基础知识Smali

    什么是Smali: 我们用工具反编译一些APP的时候,会看到一个smali文件夹,里面其实就是每个Java类所对应的smali文件.Android虚拟机Dalvik并不是执行java虚拟机JVM编译后 ...

  5. S2-045漏洞利用工具&解决方案

    简单的重复造一个轮子,漏洞危害蛮大的 影响版本:Struts 2.3.5 - Struts 2.3.31,Struts 2.5 - Struts 2.5.10 仅供学习测试使用,严禁非法操作! 下载链 ...

  6. 使用 Github 和 Hexo 快速搭建个人博客

    导语 个人兴趣爱好特别广泛,喜欢捣鼓各种小东西自娱自乐.虽然都没能深入研究,但是自己的“孩子”还是很想拿出来遛遛得人一句夸奖的.所以刚学 Markdown 的时候很是有想过要搭个个人博客来玩玩,一来激 ...

  7. xcopy 命令行

    https://www.cnblogs.com/yang-hao/p/6003308.html xcopy-参数详解   XCOPY——目录复制命令  1.功能:复制指定的目录和目录下的所有文件连同目 ...

  8. xcode减小静态库的大小(转)

    减小静态库的大小 编译生成的.a文件太大,但又没有冗余的文件可以删除已减少体积,找了很久才找到解决办法,如下: Build Settings-->Generate Debug Symbols 将 ...

  9. eclipse配置hadoop location的端口号

    在eclipse下配置hadoop location的时候 hadoop端口号应该与conf文件夹下的core-site.xml以及mapred-site.xml保持一致 前者对应dfs master ...

  10. MySQL存储引擎 -- MyISAM 与 InnoDB 实现

    一.MyISAM索引实现MyISAM引擎使用B树作为索引结构,叶节点的data域存放的是数据记录的地址. MyISAM主键索引这里设表一共有三列,假设我们以Col1为主键,Col2为辅助索引.则下图是 ...