问题描述

[LG-SP1716](https://www.luogu.org/problem/SP1716]


题解

GSS 系列的第三题,在第一题的基础上带单点修改。

第一题题解传送门

在第一题的基础上,增加一个单点修改就完事了。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std; template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
} const int maxn=50007; #define lfc (x<<1)
#define rgc ((x<<1)|1)
#define mid ((l+r)>>1)
int n;
int val[maxn<<2],lf[maxn<<2],rg[maxn<<2];
int sum[maxn<<2];
int a[maxn]; void pushup(int x){
sum[x]=sum[lfc]+sum[rgc];
lf[x]=max(lf[lfc],sum[lfc]+lf[rgc]);
rg[x]=max(rg[rgc],sum[rgc]+rg[lfc]);
val[x]=max(max(val[lfc],val[rgc]),rg[lfc]+lf[rgc]);
} void build(int x,int l,int r){
if(l==r){
sum[x]=val[x]=lf[x]=rg[x]=a[l];
return;
}
build(lfc,l,mid);build(rgc,mid+1,r);
pushup(x);
} const int INF=0x3f3f3f3f; int L,R,need; struct node{
int val,lf,rg,sum;
}; node query(int x,int l,int r){
if(L<=l&&r<=R) return (node){val[x],lf[x],rg[x],sum[x]};
if(L>mid) return query(rgc,mid+1,r);
if(R<=mid) return query(lfc,l,mid);
node res,s1=query(lfc,l,mid),s2=query(rgc,mid+1,r);
res.sum=s1.sum+s2.sum;
res.val=max(max(s1.val,s2.val),s1.rg+s2.lf);
res.lf=max(s1.lf,s1.sum+s2.lf);
res.rg=max(s2.rg,s2.sum+s1.rg);
return res;
} int op; void change(int x,int l,int r){
if(l==r){
val[x]=lf[x]=sum[x]=rg[x]=need;return;
}
if(L<=mid) change(lfc,l,mid);
else change(rgc,mid+1,r);
pushup(x);
} int main(){
read(n);
for(int i=1;i<=n;i++) read(a[i]);
build(1,1,n);
int T;read(T);
while(T--){
read(op);read(L);read(R);
if(op==1) printf("%d\n",query(1,1,n).val);
else{
need=R;
change(1,1,n);
}
}
return 0;
}

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

  1. 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 ...

  2. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...

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

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

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

    SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...

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

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

  6. 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 ...

  7. SP1716 GSS3 - Can you answer these queries III

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

  8. 数据结构(线段树):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 ...

  9. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

随机推荐

  1. Zeus:1 Vulnhub Walkthrough

    主机层面扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.14 PORT STATE SERVICE VERSION21/tcp open ftp vsftpd 2.0.8 ...

  2. Sunset: Vulnhub Walkthrough

    主机扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.147 PORT STATE SERVICE VERSION21/tcp open ftp pyftpdlib 1.5 ...

  3. layui table 表格查询无效问题

    [热身话题] 在开发的过程中,大量数据的展示大多采用表格的方式,直观,清晰.在这里,我也使用过一些框架Bootstrap.table ,Dev table ,layui table.本次采用的layu ...

  4. AndroidStudio集成.so遇见的问题:关于java.lang.UnsatisfiedLinkError: Native method not found

    我调试的是串口程序,程序中需要继承.so文件,AndroidStudio中集成.so文件的方法是将存放.so的文件夹(通常这个文件夹名字是:armeabi)拷贝到app的libs文件夹中,然后在app ...

  5. python 基础学习笔记(5)--文件操作

    **python 的文件操作** - [ ] 使用python来读写文件是非常简单的操作,我们使用open()来打开一个文件,获取到文件的语柄,然后通过文件语柄就可以进行各种各样的操作了. - [ ] ...

  6. 22(7).模型融合---CatBoost

    一.Catboost简介 全称:Gradient Boosting(梯度提升) + Categorical Features(类别型特征) 作者:俄罗斯的搜索巨头Yandex 官方地址 论文链接 | ...

  7. 电位器控制两个 LED 灯交替闪烁

    电路图: 布局:

  8. github用户注册和仓库创建

    访问github官网:https://github.com/,点击注册进入注册页面 输入用户名,电子邮箱和密码后点击下一步 邮箱验证,收到github的验证邮箱,打开后点击验证 选择个人计划 创建仓库 ...

  9. Python之format()函数

    Python2.6开始,新增了一种格式化字符串的函数format(),它增强了字符串的格式化功能.(官方推荐) 基本语法是通过{}和:来代替以前的% format()函数可以接收不限个参数,位置可以不 ...

  10. sql server判断表存在

    在创建表.更改表结构.删除表或对表进行什么操作之前,一个比较严谨的做法是先判断该表是否已经存在. 在SQL Server中判断一个表是否存在,有两个方法,下面以diso表为例. 方法1 from sy ...