SP1716 GSS3
题意翻译
\(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\)维护的序列中,和最大的子段有两种情况:
- 子段不经过中点,那么 \(rt\) 的答案为 \(l\) 和 \(r\) 的答案的最大值。
- 子段经过了中点。这种情况比较复杂,因为我们无法知道子树的答案所对应的序列。这也是本题的难点所在。
然后我们用结构体板线段树来分情况维护一下最大字段和即可,结构体传址快。
代码:
#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的更多相关文章
- 线段树 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] ... 
- 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 ... 
- SP1716 GSS3 - Can you answer these queries III  线段树
		问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ... 
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
		GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ... 
- SP1716 GSS3 - Can you answer these queries III
		题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ... 
- SP1716 GSS3(线段树+矩阵乘法)
		Code: #include <bits/stdc++.h> #define N 50001 #define ll long long #define lson now<<1 ... 
- 2018年12月25&26日
		小结:昨天因为整理课件,调代码耗费了大量时间,所以没来得及整理作业,这两天主要做的题目是关于树链剖分和线段树的,难度大约都是省选难度,毕竟只要涉及到树链剖分难度就肯定不低. 一. 完成的题目: 洛谷P ... 
- 动态DP教程
		目录 前言 开始 更进一步 前言 最后一届NOIPTG的day2T3对于动态DP的普及起到了巨大的作用.然而我到现在还不会 开始 SP1716 GSS3 - Can you answer these ... 
- 【SP1716】GSS3 - Can you answer these queries III(动态DP)
		题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ... 
随机推荐
- Java基础 之 System.getProperty()方法
			Java基础 之 System.getProperty()方法大全 public static void main(String[] args) { System.out.println(" ... 
- CodeForces-734E Anton and Tree 树的直径
			题目大意: 给定一棵有n个节点的树,有黑点白点两种节点. 每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色 现在给定一个初始局面问最少多少步可以让树变为纯色. 题解: 首先我们拿到这棵树时先将 ... 
- Android 开发:开源库Speex支持arm64的动态库文件
			随着处理器制造工艺的不断进步,和Android系统的不断发展,最近出了arm64-v8a的架构,由于项目中用到了speex的第三方语音编解码的动态库,其他架构的处理器暂不用说,一切正常,唯独到arm6 ... 
- saltstack集中化管理平台
			1.安装与启动 yum install salt-master -y 安装服务端 chkconfig salt-master on 自启动 service salt-master start 启动 y ... 
- VS2008中_T的作用
			引用: VC++里面定义字符串的时候,用_T来保证兼容性.VC++支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到unicode编码类型的时候,程序不需要修改. 如 ... 
- 广度优先搜索(BFS)
			广度优先搜索算法(Breadth-First-Search),又译作宽度优先搜索,或横向优先搜索,简称BFS,是一种图形搜索算法.简单的说,BFS是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点 ... 
- Ruby中的include
			Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ... 
- js遍历for,forEach, for in,for of
			ECMAScript5(es5)有三种for循环 简单for for in forEach ECMAScript6(es6)新增 for of 简单for for是循环的基础语法,也是最常用的循环结构 ... 
- numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)
			matlab是标准的,numpy相当于转置后计算 >> x = [2,0,-1.4;2.2,0.2,-1.5;2.4,0.1,-1;1.9,0,-1.2] x = 2.0000 0 ... 
- HTML5小知识
			1.HTML5一种“妥协的”语法 2.特点: 标签不区分大小写 元素可以省略结束标签 元素属性可以省略属性值 属性的属性值可以不适用引号 
