GSS3

Description

动态维护最大子段和,支持单点修改。

Solution

设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) 的最大子段和,那么

\[f[i] = max(f[i - 1] + a[i], a[i])
\]

\[g[i] = max(g[i - 1], f[i])
\]

发现只跟前一项有关。我们希望使用矩阵乘法的思路,但是矩阵乘法通常只能适用于递推问题。因此我们引入广义矩阵乘法。

矩阵乘法问题可分治的原因在于矩阵乘法满足结合律,而满足结合律的根本原因是乘法对加法满足分配率,即

\[a\cdot (b+c) = a\cdot c + b\cdot c
\]

那么在这里,很容易发现,加法运算对\(Min/Max\)运算也是满足分配率的,即

\[a + min(b,c) = min(a+c,b+c)
\]

\[a + max(b,c) = max(a+c,b+c)
\]

所谓广义矩阵乘法,就是将矩阵乘法中的加法运算换成\(Min/Max\)运算,乘法运算换成加法运算,那么这样的矩阵乘法仍然满足结合律。

考虑到 \(g[i]\) 从 \(f[i]\) 转移过来的那一项可以直接拆开,很容易得到转移方程

\[\begin{bmatrix}
f_{i} \\ g_{i} \\ 0
\end{bmatrix}
=
\begin{bmatrix}
a_{i} & -\infty & a_{i} \\
a_{i} & 0 & a_{i}\\
-\infty & -\infty & 0 \\
\end{bmatrix}
\cdot
\begin{bmatrix}
f_{i-1} \\ g_{i-1} \\ {0}
\end{bmatrix}
\]

可以将其记为

\[F_i = A_i \cdot F_{i-1}
\]

于是我们用线段树暴力维护所有\(A_i\)的乘积即可。复杂度\(O(27n \log{n})\)

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Matrix {
int n,m,a[5][5];
Matrix() {
n=m=0;
for(int i=0;i<4;i++) for(int j=0;j<4;j++) a[i][j]=0;
}
Matrix operator * (const Matrix &y) {
Matrix r;
if(m!=y.n) return r;
r.n = n; r.m = y.m;
for(int i=1;i<=n;i++) {
for(int j=1;j<=y.m;j++) {
for(int k=1;k<=m;k++) {
if(k==1) r.a[i][j]=a[i][k]+y.a[k][j];
else r.a[i][j]=max(r.a[i][j],a[i][k]+y.a[k][j]);
}
}
}
return r;
}
};
Matrix make(int x) {
Matrix r;
r.m=r.n=3;
r.a[1][1]=r.a[1][3]=r.a[2][1]=r.a[2][3]=x;
r.a[2][2]=r.a[3][3]=0;
r.a[1][2]=r.a[3][1]=r.a[3][2]=-1e+9;
return r;
} const int N = 1000005; Matrix val[N],zero;
int n,q,src[N],t1,t2,t3; void pushup(int p) {
val[p] = val[p*2]*val[p*2+1];
}
void build(int p,int l,int r) {
if(l==r) {
val[p]=make(src[l]);
}
else {
build(p*2,l,(l+r)/2);
build(p*2+1,(l+r)/2+1,r);
pushup(p);
}
}
void modify(int p,int l,int r,int pos,int key) {
if(l==r) {
val[p]=make(key);
}
else {
if(pos<=(l+r)/2) modify(p*2,l,(l+r)/2,pos,key);
else modify(p*2+1,(l+r)/2+1,r,pos,key);
pushup(p);
}
}
Matrix query(int p,int l,int r,int ql,int qr) {
Matrix R=make(-1e+9);
if(l>qr||r<ql) return R;
if(l>=ql&&r<=qr) return val[p];
return query(p*2,l,(l+r)/2,ql,qr)*query(p*2+1,(l+r)/2+1,r,ql,qr);
} signed main() {
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>src[i];
cin>>q;
zero.n=3; zero.m=1;
zero.a[1][1]=zero.a[2][1]=-1e+9;
build(1,1,n);
for(int i=1;i<=q;i++) {
cin>>t1>>t2>>t3;
if(t1==0) {
modify(1,1,n,t2,t3);
}
else {
Matrix r=query(1,1,n,t2,t3)*zero;
cout<<r.a[2][1]<<endl;
}
}
}

当然似乎这个问题用线段树暴力又短又快

#include <bits/stdc++.h>
using namespace std; int src[1000005],a[1000005],al[1000005],ar[1000005],s[1000005],n,m,t1,t2,t3,t4; struct Result {
int a,al,ar,s;
}; void build(int p,int l,int r) {
if(l==r) a[p]=al[p]=ar[p]=s[p]=src[l];
else {
build(p<<1,l,(l+r)/2),
build(p<<1|1,(l+r)/2+1,r);
a[p]=max(max(a[p<<1],a[p<<1|1]),max(max(ar[p<<1],al[p<<1|1]),ar[p<<1]+al[p<<1|1]));
al[p]=max(al[p<<1],s[p<<1]+max(0,al[p<<1|1]));
ar[p]=max(ar[p<<1|1],max(0,ar[p<<1])+s[p<<1|1]);
s[p]=s[p*2]+s[p*2+1];
}
} void modify(int p,int l,int r,int pos,int key) {
if(l==r) a[p]=al[p]=ar[p]=s[p]=key;
else {
if(pos<=(l+r)/2) modify(p<<1,l,(l+r)/2,pos,key);
else modify(p<<1|1,(l+r)/2+1,r,pos,key);
a[p]=max(max(a[p<<1],a[p<<1|1]),max(max(ar[p<<1],al[p<<1|1]),ar[p<<1]+al[p<<1|1]));
al[p]=max(al[p<<1],s[p<<1]+max(0,al[p<<1|1]));
ar[p]=max(ar[p<<1|1],max(0,ar[p<<1])+s[p<<1|1]);
s[p]=s[p*2]+s[p*2+1];
}
} Result query(int p,int l,int r,int ql,int qr) {
Result res;
res.a=-1e+8;
res.al=-1e+8;
res.ar=-1e+8;
res.s=-1e+8;
if(l>qr||r<ql) return res;
if(l>=ql&&r<=qr) {
res.a=a[p];
res.al=al[p];
res.ar=ar[p];
res.s=s[p];
return res;
}
else {
Result cl,cr;
cl=query(p<<1,l,(l+r)/2,ql,qr);
cr=query(p<<1|1,(l+r)/2+1,r,ql,qr);
res.a=max(max(cl.a,cr.a),max(max(cl.ar,cr.al),cl.ar+cr.al));
res.al=max(cl.al,cl.s+max(0,cr.al));
res.ar=max(cr.ar,max(0,cl.ar)+cr.s);
res.s=cl.s+cr.s;
return res;
}
} int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>src[i];
build(1,1,n);
cin>>m;
for(int i=1;i<=m;i++) {
cin>>t3>>t1>>t2;
if(t3) {
Result res=query(1,1,n,t1,t2);
cout<<res.a<<endl;
}
else {
modify(1,1,n,t1,t2);
}
}
}

SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树的更多相关文章

  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

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

  5. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  6. 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III

    GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...

  7. GSS3 - Can you answer these queries III

    题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx​ 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 感谢 @Edgration 提供的 ...

  8. Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

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

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

随机推荐

  1. node模块化开发基本知识学习笔记

    传统非模块化开发缺点: 1.命名冲突 2.文件依赖 标准的模块化规范: 1.AMD-requirejs 2.CMD-seajs 服务器端模块化规范: 1.CommonJS-Node.js 模块化相关的 ...

  2. 纪中20日c组模拟赛T1 2121. 简单游戏

    T1 2121. 简单游戏 (File IO): input:easy.in output:easy.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制 Goto Pro ...

  3. opencv —— normalize 矩阵归一化

    归一化:就是将数据通过某种算法,限制需要的一定范围内. 归一化的目的:简而言之,是使得没有可比性的数据变得具有可比性,同时又保持相比较的两个数据之间的相对关系,如大小关系:或是为了作图,原来很难在一张 ...

  4. disabled属性对form表单提交的影响

    在form表单里,如果对input加入disabled="disabled"或disabled="true"等属性,form表单提交的时候,就不会传值到后台. ...

  5. bootstrap234的ie兼容选择

    如果你需要兼容IE8甚至是IE7和IE6,那么只能选择Bootstrap2,虽然它自身在IE6的效果也并不完美.如果需要兼容IE678的话用2.如果需要高版本的浏览器,并且移动端优先的话,那么用boo ...

  6. kaggle之猫狗数据集下载

    链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw 提取码:2xq4 百度网盘实在是恶心,找的别人的网盘下载不仅速度慢,还老挂掉,自己去kaggle下 ...

  7. 虚拟机NAT模式连接外网

    虚拟机三种联网方式: 一.NAT(推荐使用)                功能:①可以和外部网络连通    ②可以隔离外部网络 二.桥接模式                        功能:直接 ...

  8. ArcGISServer发布流程

    发布数据服务 在进行WebGIS开发中,地图显示的内容可以分成两类:一类是底图,或者是矢量的世界地图.中国地图.某个地区的底图:另一类就是业务图,对于用于遥感数据发布的WebGIS应用就是遥感影像的边 ...

  9. PAT (Basic Level) Practice (中文)1087 有多少不同的值 (20 分) (set)

    当自然数 n 依次取 1.2.3.…….N 时,算式 ⌊ 有多少个不同的值?(注:⌊ 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: 输入给出一个正整数 N(2). 输出 ...

  10. Grammar Framework 1

    Word is cheap, show me the grammar. Doc requirements:TODO 1. English doc 2. Mark the importance 语法框架 ...