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 ...
随机推荐
- JS倒计时(刷新页面不影响)的实现思路
最近在做一个项目,用到了点击按钮实现倒计时,这个用js来实现很简单.但是遇到了一个问题 页面刷新后js重新加载导致 倒计时重新开始,或者直接初始化了 后来通过 cookie 保存来实现了js倒计时,关 ...
- Mysql命令行插入字段超长不报错,而jdbc报错问题分析
异常信息 exception.ServiceException: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long ...
- frp 用于内网穿透的基本配置和使用
frp 用于内网穿透的基本配置和使用 今天是端午节,先祝端午安康! frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP.UDP.HTTP.HTTPS 等多种协议.可以将内网服务以安全.便 ...
- Redis初启(一)
1.数据库存存储性能优化 在mysql的文章专题中我写过了关于传统关系型数据库的一些优化思路,整体来说,通过优化之后能够提升程序访问数据库的计算性能.但是还是有一些情况,即便是优化之后,使用传统关系型 ...
- BUUCTF-镜子里的世界
镜子里面的世界 16进制看了下没有东西,binwalk分离了一下也没发现其他的,使用stegsolve查看即可发现.
- 关于个人全栈项目【臻美IT】博客类出现的问题以及解决方法
每做一个项目,要记得写下心得哦,别偷懒啊!先上网址:https://www.maomin.club/ 这个项目属于博客类的,因为百度审核的问题就大体做了下,就当来练练手,里面文章链接的是CSDN的博客 ...
- python新建一个目录
源码部分 import os # 创建目录 def mkdir(path): isExists = os.path.exists(path) if not isExists: os.makedirs( ...
- QT工程构建目录下,将生成的中间文件和可执行文件分离
在QT工程中,当我们选择了构建目录后,编译生成程序后,总会发现在debug目录下会有混淆着各类文件,如下图 很多时候,我们又仅仅只需要可执行文件或者自定义的动态链接库.如下图 当然,如果不觉得麻烦,有 ...
- RPA应用场景-产品主数据同步
场景概述 产品主数据同步 所涉系统名称 产品管理系统.SAP系统 人工操作(时间/次) 35分钟 所涉人工数量 3 操作频率 不定时 场景流程1.登录收购品牌产品管理系统 2.根据时间.产品分类等选择 ...
- TypeScript ReadonlyArray(只读数组类型) 详细介绍
1.ReadonlyArray 简介 在TypeScript中,除了Array<T>类型,还有一个ReadonlyArray<T>类型,ReadonlyArray类型和Arra ...