CF280D k-Maximum Subsequence Sum(线段树)
在做这题时我一开始把\(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(线段树)的更多相关文章
- 【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)
[BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- 【单片机】CH32V103C8T6 ——窗口看门狗
本章教程通过串口调试助手打印显示程序运行状态,具体现象如下: 若计数器值在上窗口值和下窗口值0X40之间的时候,进行喂狗操作,计数器重新计数,程序正常运行,串口打印显示:The program run ...
- 羽夏笔记—— AT&T 与 GCC
写在前面 本文是本人根据<AT&T 汇编语言与 GCC 内嵌汇编简介>进一步整理,修改了一些错误,并删除我并不能复现代码相关的部分.该文章一是我对 AT&T 的学习记录 ...
- yolov1学习笔记
yolov1学习笔记 yolov1将目标检测归为一个回归问题,具有real-time的特点.局限性是:对于群体性的小目标检测效果很差. 论文概括 本文重新构造目标检测作为一个回归问题. 直接输入图像到 ...
- 【原创】eNSP路由器启动#号问题排查
1.删除拖出来的设备,重新拖出来一台---我用过[有时候好使] 2.确保Ensp的设置-工具-Virtual Box安装目录是否正确--我也遇到过[尤其是卸载掉Virtual Box重装之后] 3.确 ...
- MySQL数据库5
内容概要 pyhton操作MySQL SQL注入问题 修改表SQL语句补充 视图.触发器.储存过程 事务 流程控制 函数 索引与慢查询优化 内容详情 pyhton操作MySQL python中支持操作 ...
- Xmind头脑风暴
下图导出到excel后 上图对应的excel表格如下:
- mysql中max_connections与max_user_connections使用区别
问题描述:把max_connections和max_user_connections参数进行分析测试,顾名思义,max_connections就是负责数据库全局的连接数,max_user_connec ...
- vue华视电子身份证阅读器的使用
ie还是谷歌都是可以用的 只需要直接启用华视电子身份证阅读器的服务来的,至于服务已经上传到了网上 华视阅读器服务,下载下来解压,找到对应的华视电子读卡服务.exe文件,路径是CVR-1 ...
- 菜鸟学git的基本命令及常见错误
Git init //在当前项目工程下履行这个号令相当于把当前项目git化,变身!\ git config --global user.name "xxx" # 配置用户名 git ...
- 4.使用CFileDialog打开文件对话框,获得文件路径 -windows编程
引言:没想到2022年还有很多工业软件公司依然使用MFC,微软也一直在更新MFC的库,这次使用MFC封装的CFileDialog类,写一个获得选定文件路径,名称,扩展名的程序. 个人技术博客(文章整理 ...