题意翻译

\(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 710A A. King Moves(水题)

    题目链接: A. King Moves 题意: 给出king的位置,问有几个可移动的位置; 思路: 水题,没有思路; AC代码: #include <iostream> #include ...

  2. Convolutional Neural Networks for Visual Recognition 5

    Setting up the data and the model 前面我们介绍了一个神经元的模型,通过一个激励函数将高维的输入域权值的点积转化为一个单一的输出,而神经网络就是将神经元排列到每一层,形 ...

  3. mci播放mp3

    1MIDI的播放---- 乐器数字化接口(MIDI)是由音乐界的一些大公司(包括生产电子音乐合成器的公司)制订的一项协议,后来被计算机产业所采用并成为多媒体音乐文件的标准格式.MIDI文件一般较小,对 ...

  4. [CERC 2008] Suffix reconstruction

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4319 [算法] 首先 , 我们可以求出这个字符串的rank数组 按照SA逐位枚举 , ...

  5. 【算法总结】Manacher's Algorithm

    Manacher's Algorithm针对的是最长回文子串问题.对于此问题,最直接的方法是遍历每一个元素,遍历过程中以每一个字符为中心向两边扩展以寻找此字符为中心的最长回文子串.复杂度O(n2).M ...

  6. tiny4412sdk-1506原生uboot卡死

    于16年2月多购买了tiny4412sdk-1506,用友善之臂(以下简称友善)的superboot是可以进入linux,而用三星原始的uboot_tiny4412-20130729则不可以.出现现象 ...

  7. 系列文章--AJAX技术系列总结

    各种AJAX方法的使用比较 用ASP.NET写个SQLSERVER的小工具  写自己的ASP.NET MVC框架(下)  写自己的ASP.NET MVC框架(上)  用Asp.net写自己的服务框架  ...

  8. [转]两种高性能I/O设计模式(Reactor/Proactor)的比较

    [原文地址:http://www.cppblog.com/pansunyou/archive/2011/01/26/io_design_patterns.html] 综述 这篇文章探讨并比较两种用于T ...

  9. 洛谷【P3908】异或之和

    二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P39 ...

  10. C#添加修改控件css样式

    一.添加属性 MyStyleSheet.Attributes.Add("href","/css/flostyle.css") 二.改变css样式 if (use ...