题目链接

之前用线段树写了一遍,现在用\(ddp\)再写一遍。

#include <cstdio>
#define lc (now << 1)
#define rc (now << 1 | 1)
inline int max(int a, int b){
return a > b ? a : b;
}
const int INF = 2147483647 >> 2;
const int MAXN = 50010;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
struct Matrix{
int a[3][3];
}sum[MAXN << 2];
int a[MAXN], n, m, A, B, opt;
inline Matrix operator * (Matrix a, Matrix b){
Matrix c;
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
c.a[i][j] = -INF;
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
for(int k = 0; k < 3; ++k)
c.a[i][j] = max(c.a[i][j], a.a[i][k] + b.a[k][j]);
return c;
}
inline void pushup(int now){
sum[now] = sum[lc] * sum[rc];
}
void build(int now, int l, int r){
if(l == r){
sum[now].a[0][0] = sum[now].a[0][2] = sum[now].a[1][0] = sum[now].a[1][2] = a[l];
sum[now].a[0][1] = sum[now].a[2][0] = sum[now].a[2][1] = -INF;
sum[now].a[1][1] = sum[now].a[2][2] = 0; return ;
}
int mid = (l + r) >> 1;
build(lc, l, mid); build(rc, mid + 1, r); pushup(now);
}
void modify(int now, int l, int r, int x, int y){
if(l == r){
sum[now].a[0][0] = sum[now].a[0][2] = sum[now].a[1][0] = sum[now].a[1][2] = y;
return ;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(lc, l, mid, x, y);
else modify(rc, mid + 1, r, x, y);
pushup(now);
}
Matrix query(int now, int l, int r, int wl, int wr){
if(l >= wl && r <= wr) return sum[now];
int mid = (l + r) >> 1;
if(wl <= mid && wr > mid)
return query(lc, l, mid, wl, wr) * query(rc, mid + 1, r, wl ,wr);
else if(wl <= mid)
return query(lc, l, mid, wl, wr);
else return query(rc, mid + 1, r, wl, wr);
}
int main(){
n = read();
for(int i = 1; i <= n; ++i)
a[i] = read();
build(1, 1, n);
m = read();
for(int i = 1; i <= m; ++i){
opt = read(); A = read(); B = read();
if(opt){
Matrix ans = query(1, 1, n, A, B);
printf("%d\n", max(ans.a[1][0], ans.a[1][2]));
}
else modify(1, 1, n, A, B);
}
return 0;
}

【SP1716】GSS3 - Can you answer these queries III(动态DP)的更多相关文章

  1. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  2. 线段树 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] ...

  3. 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 ...

  4. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

  5. SP1716 GSS3 - Can you answer these queries III

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

  6. 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 ...

  7. 数据结构(线段树):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 ...

  8. 题解 SP1716 【GSS3 - Can you answer these queries III】

    \[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...

  9. 题解【SP1716】GSS3 - Can you answer these queries III

    题目描述 You are given a sequence \(A\) of \(N (N <= 50000)\) integers between \(-10000\) and \(10000 ...

随机推荐

  1. OpenFOAM——同心环中的自然对流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL009: Natural Convection in a Concentric A ...

  2. ICEM-哑铃

    原视频下载地址:https://pan.baidu.com/s/1kVBKJbT ;密码: jqeh

  3. 2019 SDN第二次上机作业

    2019 SDN第二次上机作业 1. 利用mininet创建如下拓扑,要求拓扑支持OpenFlow 1.3协议,主机名.交换机名以及端口对应正确,请给出拓扑Mininet执行结果,展示端口连接情况 创 ...

  4. SSH框架整合2

    ===========================================web.xml================================================== ...

  5. ipv4的TCP的几个状态 (SYN, FIN, ACK, PSH, RST, URG)

    1 在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 2 3 其中,对于我们日常的分析有用的就是前面的五个字段. 4 5 它们的含义是 ...

  6. 如何获取select下拉框中option选中的文本值

    $(select的id或者class).change(function(){ $(this).find("option:selected").text() }) 源文:https: ...

  7. 一个简单的java爬虫

    直接上代码: package com.jeecg.util; import java.io.BufferedReader; import java.io.IOException; import jav ...

  8. Oracle系列十一 数据处理

    数据操纵语言 DML(Data Manipulation Language – 数据操纵语言) 可以在下列条件下执行: 向表中插入数据 修改现存数据 删除现存数据 事务是由完成若干项工作的DML语句组 ...

  9. Mysql查询的执行顺序

    顺序: FROM JOIN ON WHERE GROUP BY (从此处开始可以使用select中指定的别名) AVG.SUM.MAX HAVING SELECT DISTINCT ORDER BY ...

  10. LinkedBlockingQueue与ArrayBlockingQueue

    阻塞队列与普通的队列(LinkedList/ArrayList)相比,支持在向队列中添加元素时,队列的长度已满阻塞当前添加线程,直到队列未满或者等待超时:从队列中获取元素时,队列中元素为空 ,会将获取 ...