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 ...
随机推荐
- photonView 空指针异常
1.OBJ上要有PhotonView 脚本 2.PhotonNetwork.Instantiate方法初始化出来OBJ OBJ 预制体要放在Resources文件夹下 PhotonNetwork.In ...
- Centos下Yum安装PHP5.5
默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 ...
- Linux不停往外发包
一台Linux这两天不停往外发包,造成外部无法访问. [root@ct-nat ~]# watch ifconfig-------------查看数据包新增情况 Every 2.0s: ifconfi ...
- java对象在内存中的结构(HotSpot虚拟机)
一.对象的内存布局 HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header).实例数据(Instance Data)和对齐填充(Padding). 从上面的这张图里面可以 ...
- rails常用命令示例
数据迁移命令 1.一下命令执行后会在db\migrate下产生同名数据迁移文件(文件内容可自行修改,基本语法见“数据迁移文件”部分) 创建model:rails generate model user ...
- VMware Workstation虚拟机安装Windows 7系统
1.进入VMware Workstation虚拟机软件界面,选择新建虚拟机
- 前端之css样式(选择器)
一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...
- Java enum(枚举)使用详解之一
enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程中的一些经验和总结,主要包括如下内容: 1. 原始 ...
- kvm ip查看
1.virsh --list(查看有哪些服务器) 2.virsh dumpxml 虚拟机名称 查看服务器对应的mac地址 3.然后再宿主机上arp -a 查看对应的mac地址对应的ip
- iOS开发者福利之精品源码汇总!免费下载
汇总一些看着不错的源码,有需要的朋友过来下载吧!{:4_102:} 1.用swift制作的色彩炫丽的进度条-KDCircularProgressKDCircularProgress是使用swift制作 ...