题意翻译

\(n\) 个数,\(q\) 次操作

操作\(0\) \(x\) \(y\)把\(A_x\) 修改为\(y\)

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

输入输出格式

输入格式:

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

输出格式:

For each query, print an integer as the problem required.

输入输出样例

输入样例#1:

4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3

输出样例#1:

6
4
-3

思路:首先分析询问的本质:求出区间最大子段和!很显然我们可以使用线段树维护序列,本题的难点主要在如何进行上传操作,将子树\(l\)和\(r\)的节点信息上传到子树\(rt\)时,对于\(rt\)维护的序列中,和最大的子段有两种情况:

  1. 子段不经过中点,那么 \(rt\) 的答案为 \(l\) 和 \(r\) 的答案的最大值。
  2. 子段经过了中点。这种情况比较复杂,因为我们无法知道子树的答案所对应的序列。这也是本题的难点所在。

然后我们用结构体板线段树来分情况维护一下最大字段和即可,结构体传址快。

代码:

#include<cstdio>
#include<algorithm>
#include<cctype>
#define maxn 50007
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
inline int qread() {
char c=getchar();int num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
int n,m;
struct Tree {
int lmax,rmax,sum,maxx;
}tree[maxn<<2];
inline void pushup(int rt) {
tree[rt].lmax=max(tree[ls].lmax,tree[ls].sum+tree[rs].lmax);
tree[rt].rmax=max(tree[rs].rmax,tree[rs].sum+tree[ls].rmax);
tree[rt].maxx=max(tree[ls].rmax+tree[rs].lmax,max(tree[ls].maxx,tree[rs].maxx));
tree[rt].sum=tree[ls].sum+tree[rs].sum;
}
void build(int rt, int l, int r) {
if(l==r) {
tree[rt].lmax=tree[rt].rmax=tree[rt].sum=tree[rt].maxx=qread();
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
void add(int rt, int l, int r, int L, int val) {
if(l==r) {
tree[rt].lmax=tree[rt].rmax=tree[rt].sum=tree[rt].maxx=val;
return;
}
int mid=(l+r)>>1;
if(L<=mid) add(ls,l,mid,L,val);
else add(rs,mid+1,r,L,val);
pushup(rt);
}
Tree query(int rt, int l, int r, int L, int R) {
if(L==l&&r==R) return tree[rt];
int mid=(l+r)>>1;
if(L>mid) return query(rs,mid+1,r,L,R);
else if(R<=mid) return query(ls,l,mid,L,R);
else {
Tree a=query(ls,l,mid,L,mid),b=query(rs,mid+1,r,mid+1,R),c;
c.lmax=max(a.lmax,a.sum+b.lmax);
c.rmax=max(b.rmax,b.sum+a.rmax);
c.sum=a.sum+b.sum;
c.maxx=max(a.rmax+b.lmax,max(a.maxx,b.maxx));
return c;
}
}
int main() {
n=qread();
build(1,1,n);
m=qread();
for(int i=1,k,x,y;i<=m;++i) {
k=qread(),x=qread(),y=qread();
if(!k) add(1,1,n,x,y);
else printf("%d\n",query(1,1,n,x,y).maxx);
}
return 0;
}

SP1716 GSS3的更多相关文章

  1. 线段树 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] ...

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

  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 - 动态dp,线段树

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

  5. SP1716 GSS3 - Can you answer these queries III

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

  6. SP1716 GSS3(线段树+矩阵乘法)

    Code: #include <bits/stdc++.h> #define N 50001 #define ll long long #define lson now<<1 ...

  7. 2018年12月25&26日

    小结:昨天因为整理课件,调代码耗费了大量时间,所以没来得及整理作业,这两天主要做的题目是关于树链剖分和线段树的,难度大约都是省选难度,毕竟只要涉及到树链剖分难度就肯定不低. 一. 完成的题目: 洛谷P ...

  8. 动态DP教程

    目录 前言 开始 更进一步 前言 最后一届NOIPTG的day2T3对于动态DP的普及起到了巨大的作用.然而我到现在还不会 开始 SP1716 GSS3 - Can you answer these ...

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

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

随机推荐

  1. codeforces 622A A. Infinite Sequence (二分)

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. leetcode 205. Isomorphic Strings(哈希表)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  3. Postgresql windows下安装过程

    1.下载前三个软件: 理论上安装Perl,TCL,Bison and Flex这三个插件 实际上安装ActiveState Perl,ActiveState tcl ,MinGW 因为MinGW包括了 ...

  4. 浅谈vue路由原理

    Vue的路由实现:hash模式 和 history模式 hash模式:在浏览器中符号“#”,#以及#后面的字符称之为hash,用window.location.hash读取: 特点:hash虽然在UR ...

  5. JS 获取json长度

    var keleyijson={"plug1":"myslider","plug2":"zonemenu"," ...

  6. 外置式与增量式PID模板程序(51单片机c语言)

    外置式PID模板 #define MuBiaoCS 0 //目标常数 #define CHang_aCS 0 //比例常数 #define CHang_bCS 0 //积分常数 #define CHa ...

  7. 字符编码ASCII、Unicode、GB

    计算机的存储都是二进制的,那么我们平时看到的各种字符都需要通过按照一定的格式转换成为二进制才能在被计算机识别与处理.这个过程便成为编码.常见的编码方式有ASCII.Unicode.GB2312等. 1 ...

  8. Web Pages(单页面模型)

    .NET 是一套框架,用来个HTML.JS.CSS和服务器端脚本构建网页和网站. 可以有三种开发模式:Web Pages(单页面模型).MVC(模型视图控制器).Web Forms(事件驱动模型) W ...

  9. Lua中的点、冒号与self

    Lua中的点.冒号与self,它们之间的关系主要体现在函数的定义与调用上,Lua在函数定义时可以用点也可以用冒号,如: function mytable.fun(p) return p end fun ...

  10. mahout 实现canopy

    环境: mahout-0.8 hadoop-1.1.2 ubuntu-12.04 理论这里就不说了,直接上实例: 下面举一个例子. 数据准备: canopy.dat文件,COPY到HDFS上,文件内容 ...