【SP1716】GSS3 - Can you answer these queries III(动态DP)
题目链接
之前用线段树写了一遍,现在用\(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)的更多相关文章
- 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
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
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...
- 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 ...
- 数据结构(线段树):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 ...
- 题解 SP1716 【GSS3 - Can you answer these queries III】
\[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...
- 题解【SP1716】GSS3 - Can you answer these queries III
题目描述 You are given a sequence \(A\) of \(N (N <= 50000)\) integers between \(-10000\) and \(10000 ...
随机推荐
- @PathVariable不起作用,报错:Cannot resolve @PathVariable ' '
@PathVariable是占位符注解.可以把Url中变量的值传递到方法参数中. 示例如下: @PostMapping("/user/name/{id}") @ResponseBo ...
- 20189220 余超《Linux内核原理与分析》第九周作业
理解进程调度时机跟踪分析进程调度与进程切换的过程 本章的基础知识总结 一般来说,进程调度分为三种类型:中断处理过程(包括时钟中断.I/O 中断.系统调用和异常)中,直接调用schedule,或者返回用 ...
- Linux零拷贝技术 直接 io
Linux零拷贝技术 .https://kknews.cc/code/2yeazxe.html https://zhuanlan.zhihu.com/p/76640160 https://clou ...
- docker安装并运行kibana
拉镜像: [mall@VM_0_7_centos ~]$ : Pulling from library/kibana 256b176beaff: Already exists 88643bded09c ...
- Gan-based zero-shot learning 论文整理
1 Feature Generating Networks for Zero-Shot Learning Suffering from the extreme training data imbala ...
- Clang的线程安全分析静态工具
本文内容来自 Thread Safety Analysis,如需完整学习,请参考相关链接. Clang线程安全分析工具是C++语言的一种扩展,用于警告代码中潜在的竞争条件.它在编译期间进行静态分析,无 ...
- [LeetCode] 495. Teemo Attacking 提莫攻击
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- [LeetCode] 496. Next Greater Element I 下一个较大的元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 当代码上传git时,提示Repository not found The requested repository does not exist, or you do not have permission to access it. fatal: Could not read from remote repository. Please make sure you have the corre
1.检查当前git中设置的用户名与邮箱是否与自己电脑上的一致. 看这个文件中 如果不一致,只需要把里面的内容全部复制出来添加到git(看下图位置) 这是再执行:git push -u origin m ...
- Jenkins - 安装并启动Jenkins
1 - 关于Jenkins 构建流水线(build pipeline)工具Jenkins可以轻松地定义和管理各种各样的操作(构建.测试等),并将这些操作像管道pipe一样自由地进行组合,从而自动.流畅 ...