题目链接

之前用线段树写了一遍,现在用\(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. 微信小程序之数据缓存

    关于缓存,举个示例,假定我不是通过微信授权登录的方式,小程序又是如何识别我登录后的身份呢???效果图: 这个功能我是通过缓存实现的. 关键核心代码如下: wx.setStorage({ key: 'u ...

  2. CodeMirror在线代码编辑器使用

    CodeMirror官网地址为:https://codemirror.net/ CodeMirror作为一款代码编辑器,其应用场景主要是在线网站写代码.如现在的leetcode.洛谷.code-vs等 ...

  3. 【大数据应用技术】作业十|分布式文件系统HDFS 练习

    本次作业的要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 1.目录操作 在HDFS中为hadoop用户创建一个用户目 ...

  4. NoSql数据库Redis系列(5)——Redis主从复制

    前面介绍Redis,我们都在一台服务器上进行操作的,也就是说读和写以及备份操作都是在一台Redis服务器上进行的,那么随着项目访问量的增加,对Redis服务器的操作也越加频繁,虽然Redis读写速度都 ...

  5. 2019_软工实践_Beta(3/5)

    队名:955 组长博客:点这里! 作业博客:点这里! 组员情况 组员1(组长):庄锡荣 过去两天完成了哪些任务 文字/口头描述 ? 维持进度,检查需求 展示GitHub当日代码/文档签入记录 接下来的 ...

  6. 范仁义html+css课程---7、表单

    范仁义html+css课程---7.表单 一.总结 一句话总结: 表单标签的话主要掌握form标签.input标签(以及input标签的不同的type值).select标签.textarea等标签,及 ...

  7. NGINX Cache Management (.imh nginx)

    In this article, we will explore the various NGINX cache configuration options, and tips on tweaking ...

  8. Netty 多客户端连接与通信

    实现场景: 聊天 服务端,客户端A,客户端B,客户端C.当客户端发送消息给服务端后,服务端在将这条消息广播个所有客户端户端A,客户端B,客户端C. 需求1: 客户端上线后,会通知所有客户端上线. 如客 ...

  9. sci会议和sci期刊区别是什么

    sci会议和sci期刊区别是什么?从名称上来看就很明显了,一个是学术会议,一个是学术期刊,是不同的学术研究载体,不论是会议论文还是期刊论文,都是受认可的,就目前来看,发表期刊论文的作者要偏多一些,主要 ...

  10. GIS地理处理脚本案例教程——批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取

    GIS地理处理脚本案例教程--批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 关键 ...