在做这题时我一开始把\(tag\)写入了结构体

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> inline ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
} const int N = 1e5 + 7; int n; struct RPG {
int l, r, sum;
bool operator < (const RPG &com) const {
return sum < com.sum;
}
RPG operator + (const RPG &b) const {
return (RPG){ l, b.r, sum + b.sum};
}
};
struct nod {
RPG lmax, lmin, rmin, rmax, mx, mn, all;
bool tag;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bool operator < (const nod &com) const {
return mx.sum < com.mx.sum;
}
} t[N << 2];
#define ls rt << 1
#define rs rt << 1 | 1
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
inline void Newnode(int rt, int pos, int val) {
t[rt].lmax = t[rt].lmin = t[rt].rmax = t[rt].rmin = t[rt].mx = t[rt].mn = t[rt].all = (RPG){ pos, pos, val};
}
inline nod Merge(nod x, nod y) {
nod s;
s.lmax = max(x.lmax, x.all + y.lmax);
s.lmin = min(x.lmin, x.all + y.lmin);
s.rmax = max(y.rmax, x.rmax + y.all);
s.rmin = min(y.rmin, x.rmin + y.all);
s.mx = max(max(x.mx, y.mx), x.rmax + y.lmax);
s.mn = min(min(x.mn, y.mn), x.rmin + y.lmin);
s.all = x.all + y.all;
return s;
}
inline void Pushup(int &rt) {
t[rt] = Merge(t[ls], t[rs]);
}
inline void Pushrev(int rt) {
swap(t[rt].lmax, t[rt].lmin);
swap(t[rt].rmax, t[rt].rmin);
swap(t[rt].mx, t[rt].mn);
t[rt].lmax.sum = -t[rt].lmax.sum;
t[rt].lmin.sum = -t[rt].lmin.sum;
t[rt].rmax.sum = -t[rt].rmax.sum;
t[rt].rmin.sum = -t[rt].rmin.sum;
t[rt].mx.sum = -t[rt].mx.sum;
t[rt].mn.sum = -t[rt].mn.sum;
t[rt].all.sum = -t[rt].all.sum;
t[rt].tag ^= 1;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Pushdown(int rt) {
if(!t[rt].tag) return;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pushrev(ls);
Pushrev(rs);
t[rt].tag = 0;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Modify(int rt, int l, int r, int x, int w) {
if(l == r){
Newnode(rt, x, w);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(x <= mid)
Modify(lson, x, w);
else
Modify(rson, x, w);
Pushup(rt);
}
inline void Updata(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R){
Pushrev(rt);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(L <= mid) Updata(lson, L, R);
if(R > mid) Updata(rson, L, R);
Pushup(rt);
return;
}
inline nod Query(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R) return t[rt];
Pushdown(rt);
int mid = (l + r) >> 1;
if(R <= mid) return Query(lson, L, R);
else if(L > mid) return Query(rson, L, R);
else return Merge(Query(lson, L, R), Query(rson, L, R));
Pushup(rt);
} nod sta[N];
int top;
inline int Solve(int l, int r, int K) {
int ans = 0;
while(K--){
nod s = Query(1, 1, n, l, r);
if(s.mx.sum < 0) break;
ans += s.mx.sum;
Updata(1, 1, n, s.mx.l, s.mx.r);
sta[++top] = s;
}
while(top){
nod s = sta[top--];
Updata(1, 1, n, s.mx.l, s.mx.r);
}
return ans;
} int main() {
//FileOpen();
io >> n;
R(i,1,n){
int x;
io >> x;
Modify(1, 1, n, i, x);
} int m;
io >> m;
while(m--){
int opt;
io >> opt;
if(opt){
int l, r, K;
io >> l >> r >> K;
printf("%d\n", Solve(l, r, K));
}
else{
int x, w;
io >> x >> w;
Modify(1, 1, n, x, w);
}
} return 0;
}

(注意打感叹号处)

一直没过样例,后来参考一位大佬的代码把\(tag\)单独放外面

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> inline ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
} const int N = 1e5 + 7; int n; struct RPG {
int l, r, sum;
bool operator < (const RPG &com) const {
return sum < com.sum;
}
RPG operator + (const RPG &b) const {
return (RPG){ l, b.r, sum + b.sum};
}
};
struct nod {
RPG lmax, lmin, rmin, rmax, mx, mn, all;
bool operator < (const nod &com) const {
return mx.sum < com.mx.sum;
}
} t[N << 2];
bool tag[N << 2]; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#define ls rt << 1
#define rs rt << 1 | 1
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
inline void Newnode(int rt, int pos, int val) {
t[rt].lmax = t[rt].lmin = t[rt].rmax = t[rt].rmin = t[rt].mx = t[rt].mn = t[rt].all = (RPG){ pos, pos, val};
}
inline nod Merge(nod x, nod y) {
nod s;
s.lmax = max(x.lmax, x.all + y.lmax);
s.lmin = min(x.lmin, x.all + y.lmin);
s.rmax = max(y.rmax, x.rmax + y.all);
s.rmin = min(y.rmin, x.rmin + y.all);
s.mx = max(max(x.mx, y.mx), x.rmax + y.lmax);
s.mn = min(min(x.mn, y.mn), x.rmin + y.lmin);
s.all = x.all + y.all;
return s;
}
inline void Pushup(int &rt) {
t[rt] = Merge(t[ls], t[rs]);
}
inline void Pushrev(int rt) {
swap(t[rt].lmax, t[rt].lmin);
swap(t[rt].rmax, t[rt].rmin);
swap(t[rt].mx, t[rt].mn);
t[rt].lmax.sum = -t[rt].lmax.sum;
t[rt].lmin.sum = -t[rt].lmin.sum;
t[rt].rmax.sum = -t[rt].rmax.sum;
t[rt].rmin.sum = -t[rt].rmin.sum;
t[rt].mx.sum = -t[rt].mx.sum;
t[rt].mn.sum = -t[rt].mn.sum;
t[rt].all.sum = -t[rt].all.sum;
tag[rt] ^= 1;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Pushdown(int rt) {
if(!tag[rt]) return;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pushrev(ls);
Pushrev(rs);
tag[rt] = 0;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Modify(int rt, int l, int r, int x, int w) {
if(l == r){
Newnode(rt, x, w);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(x <= mid)
Modify(lson, x, w);
else
Modify(rson, x, w);
Pushup(rt);
}
inline void Updata(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R){
Pushrev(rt);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(L <= mid) Updata(lson, L, R);
if(R > mid) Updata(rson, L, R);
Pushup(rt);
return;
}
inline nod Query(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R) return t[rt];
Pushdown(rt);
int mid = (l + r) >> 1;
if(R <= mid) return Query(lson, L, R);
else if(L > mid) return Query(rson, L, R);
else return Merge(Query(lson, L, R), Query(rson, L, R));
Pushup(rt);
} nod sta[N];
int top;
inline int Solve(int l, int r, int K) {
int ans = 0;
while(K--){
nod s = Query(1, 1, n, l, r);
if(s.mx.sum < 0) break;
ans += s.mx.sum;
Updata(1, 1, n, s.mx.l, s.mx.r);
sta[++top] = s;
}
while(top){
nod s = sta[top--];
Updata(1, 1, n, s.mx.l, s.mx.r);
}
return ans;
} int main() {
//FileOpen();
io >> n;
R(i,1,n){
int x;
io >> x;
Modify(1, 1, n, i, x);
} int m;
io >> m;
while(m--){
int opt;
io >> opt;
if(opt){
int l, r, K;
io >> l >> r >> K;
printf("%d\n", Solve(l, r, K));
}
else{
int x, w;
io >> x >> w;
Modify(1, 1, n, x, w);
}
} return 0;
}

(注意感叹号处)

就成功过掉了

这是为什么呢?是哪儿影响的呢?

大佬发话

原来是\(Merge()\)处没有初始化!

模拟费用流,线段树维护区间

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> inline ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
} const int N = 1e5 + 7; int n; struct RPG {
int l, r, sum;
bool operator < (const RPG &com) const {
return sum < com.sum;
}
RPG operator + (const RPG &b) const {
return (RPG){ l, b.r, sum + b.sum};
}
};
struct nod {
RPG lmax, lmin, rmin, rmax, mx, mn, all;
bool tag;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bool operator < (const nod &com) const {
return mx.sum < com.mx.sum;
}
} t[N << 2];
#define ls rt << 1
#define rs rt << 1 | 1
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
inline void ChangeNode(int rt, int pos, int val) {
t[rt].lmax = t[rt].lmin = t[rt].rmax = t[rt].rmin = t[rt].mx = t[rt].mn = t[rt].all = (RPG){ pos, pos, val};
}
inline nod Merge(nod x, nod y) {
nod s;
s.lmax = max(x.lmax, x.all + y.lmax);
s.lmin = min(x.lmin, x.all + y.lmin);
s.rmax = max(y.rmax, x.rmax + y.all);
s.rmin = min(y.rmin, x.rmin + y.all);
s.mx = max(max(x.mx, y.mx), x.rmax + y.lmax);
s.mn = min(min(x.mn, y.mn), x.rmin + y.lmin);
s.all = x.all + y.all;
s.tag = false;
return s;
}
inline void Pushup(int &rt) {
t[rt] = Merge(t[ls], t[rs]);
}
inline void Pushrev(int rt) {
swap(t[rt].lmax, t[rt].lmin);
swap(t[rt].rmax, t[rt].rmin);
swap(t[rt].mx, t[rt].mn);
t[rt].lmax.sum = -t[rt].lmax.sum;
t[rt].lmin.sum = -t[rt].lmin.sum;
t[rt].rmax.sum = -t[rt].rmax.sum;
t[rt].rmin.sum = -t[rt].rmin.sum;
t[rt].mx.sum = -t[rt].mx.sum;
t[rt].mn.sum = -t[rt].mn.sum;
t[rt].all.sum = -t[rt].all.sum;
t[rt].tag ^= 1;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Pushdown(int rt) {
if(!t[rt].tag) return;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pushrev(ls);
Pushrev(rs);
t[rt].tag = 0;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
inline void Modify(int rt, int l, int r, int x, int w) {
if(l == r){
ChangeNode(rt, x, w);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(x <= mid)
Modify(lson, x, w);
else
Modify(rson, x, w);
Pushup(rt);
}
inline void Updata(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R){
Pushrev(rt);
return;
}
Pushdown(rt);
int mid = (l + r) >> 1;
if(L <= mid) Updata(lson, L, R);
if(R > mid) Updata(rson, L, R);
Pushup(rt);
return;
}
inline nod Query(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R) return t[rt];
Pushdown(rt);
int mid = (l + r) >> 1;
if(R <= mid) return Query(lson, L, R);
else if(L > mid) return Query(rson, L, R);
else return Merge(Query(lson, L, R), Query(rson, L, R));
Pushup(rt);
} nod sta[N];
int top;
inline int Solve(int l, int r, int K) {
int ans = 0;
while(K--){
nod s = Query(1, 1, n, l, r);
if(s.mx.sum < 0) break;
ans += s.mx.sum;
Updata(1, 1, n, s.mx.l, s.mx.r);
sta[++top] = s;
}
while(top){
nod s = sta[top--];
Updata(1, 1, n, s.mx.l, s.mx.r);
}
return ans;
} int main() {
//FileOpen();
io >> n;
R(i,1,n){
int x;
io >> x;
Modify(1, 1, n, i, x);
} int m;
io >> m;
while(m--){
int opt;
io >> opt;
if(opt){
int l, r, K;
io >> l >> r >> K;
printf("%d\n", Solve(l, r, K));
}
else{
int x, w;
io >> x >> w;
Modify(1, 1, n, x, w);
}
} return 0;
}

CF280D k-Maximum Subsequence Sum(线段树)的更多相关文章

  1. 【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)

    [BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交 ...

  2. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  3. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  5. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  7. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  9. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

随机推荐

  1. jeecgboot-vue3笔记(九)——treeSelect树形选择组件的使用(异步加载)

    使用效果 初始化加载顶层节点,点击各层的>加载该节点的子节点,加载后>标识去除不再重复加载. 前端代码 vue ant-design组件 tree-data,树节点,children方式或 ...

  2. 【zigbee无线通信模块步步详解】ZigBee3.0模块建立远程网络控制方法

    本文以路灯控制应用为例,简述ZigBee3.0模块使用流程. 一.建立网络 1.通过USB转串口模块将出厂的ZigBee自组网模块连接,打开上位机软件"E180-ZG120A-Setting ...

  3. 测试平台系列(97) 完善执行case部分

    大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的教程,希望大家多多支持. 欢迎关注我的公众号米洛的测开日记,获取最新文章教程! 回顾 上一节我们讨论了怎么结束一个 ...

  4. 开发工具-Java SDK下载地址

    更新记录 2022年6月14日 加入更多的下载地址. 2022年6月10日 完善标题. 下载地址: https://www.oracle.com/java/technologies/downloads ...

  5. Windows系统重置用户登录密码

    更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年8月23日. 方法一.使用带有密码恢复功能的PE盘 买一张 PE光盘 或 自制PE启动盘,这里推荐微PE. 准备 ...

  6. java标识符 identifier

    1,标识符 --> 类名 方法名  变量名 常量名 接口名   为程序员自己命名的内容 main也是标识符但是不能修改 2, 命名规则 只能以   数字 字母(中文) 下划线 美元符号      ...

  7. 实现领域驱动设计 - 使用ABP框架 - 创建实体

    用例演示 - 创建实体 本节将演示一些示例用例并讨论可选场景. 创建实体 从实体/聚合根类创建对象是实体生命周期的第一步.聚合/聚合根规则和最佳实践部分建议为Entity类创建一个主构造函数,以保证创 ...

  8. 深入解析kubernetes controller-runtime

    Overview controller-runtime 是 Kubernetes 社区提供可供快速搭建一套 实现了controller 功能的工具,无需自行实现Controller的功能了:在 Kub ...

  9. HTML入门,基础知识

    初识HTML HTML: 超文本标记语言 一.HTML的基本结构 根控制标记(头) ​ 头控制标记(头) ​ 标题 标题标记 ​ 头控制标记(尾) ​ 网页显示区域(一般要实现的代码都在这里写) &l ...

  10. plain framework的实际应用和扩展

    首先在这里庆祝香港回归祖国的怀抱25周年,想起那年还是一个小学生戴着红领巾和胸章激动不已,实现祖国的统一是每个中华儿女从小的梦想!趁着这欢庆的日子,突然想要写些什么,其实最近也在做一些事,由于工作繁忙 ...