题目链接

之前用线段树写了一遍,现在用\(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. 使用IDEA查看变量调用链

    在开发中,我们有时需要查看某个变量是怎么来的,从哪个类的某个方法调用后进入另一个类的某个方法. 如果只有一两层的调用,那么还能直接通过方法跳转来观察. 但是,如果有七八层的调用链呢,在各个方法之间跳来 ...

  2. 剑指offer:左旋转字符串

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”ab ...

  3. shell | crontab 定时任务

    crontab工具 linux下自带的定时任务执行器 常用命令:crontab -l //显示用户的crontab文件的内容crontab -e //编辑用户的crontab文件的内容crontab ...

  4. Research Guide for Neural Architecture Search

    Research Guide for Neural Architecture Search 2019-09-19 09:29:04 This blog is from: https://heartbe ...

  5. Laravel 入门常见问题汇总

    一.安装完成后想打开 Laravel 内置的登录页面,报错 解决方法: Laravel 利用 PHP5.4 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...

  6. Python之schedule用法,类似linux下的crontab

    # -*- coding: utf-8 -*- # author:baoshan import schedule import time def job(): print("I'm work ...

  7. MD5(2)

    /************************************************ MD5 算法的Java Bean @author:Topcat Tuppin Last Modifi ...

  8. PL/SQL无法显示字段可以为NULL还是不能为NULL

    今天用mybatis操作oracle,用PL/SQL看到数据表的字段,明明都是可以为NULL的字段,各个字段都报错,ORA-01400 字段不能为NULL. 后面请教了同事和朋友,才知道这是PL/SQ ...

  9. [LeetCode] 275. H-Index II H指数 II

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  10. 如何杀死处于进程状态D的进程

    D状态的就是 uninterruptible sleep ,此时进程不能被信号唤醒,GDB等调试工具也不能对它调试,因为GDB也是用到了信号,也杀不死它 D状态的形成 如何分析D状态 cat /pro ...