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 ...
随机推荐
- codeforces 628C C. Bear and String Distance
C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...
- leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...
- AngularJS方法 —— angular.copy
描述: 复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象). 如果省略了destination,一个新的对象或数组将会被创建出来: 如果提供了destination,则source对象中的 ...
- bzoj 4817: [Sdoi2017]树点涂色 LCT+树链剖分+线段树
题目: Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. Bob可能会进 ...
- 51nod 1686 第K大区间 二分瞎搞
题目: 定义一个区间的值为其众数出现的次数. 现给出n个数,求将所有区间的值排序后,第K大的值为多少. 题解: 答案明显单调,我们考虑二分答案. 转化为判定问题后我们需要观察到一个性质: 如果一个区间 ...
- js变量和函数提升的小结
对于变量和函数一起的提升说法,我比较认同"LittleBear"的说法. 比如: <script> console.log(a)//function a(){} var ...
- [转] 更新Flash CS6发布设置的目标播放器版本
目前Aodbe发布的最新版的Flash CS6,都不支持将Flash Player 11作为目标播放器版本发布.这个问题很容易解决,但涉及到的东西却比较多,我在这里将一一讲解.首先来个Setp by ...
- UVA624(01背包记录路径)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- HDOJ1166(线段树点修改)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 杂项-权限管理:RBAC
ylbtech-杂项-权限管理:RBAC 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中, ...