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 ...
随机推荐
- Simple, Fast Malicious Multiparty Private Set Intersection-解读
文本记录阅读该论文的笔记. 这是文章框架,来自视频. 介绍 本文主要解决恶意攻击下安全的多方PSI,主要用到两大技术OPPRF和OKVS,构造合谋和不合谋的协议. 基础知识 OPPRF 这部分在OPR ...
- “极简”创建 github page 并设置域名
最简单最详细的,创建 github page 并设置域名,没有多余的步骤,并且多图,对新手特别友好 尝试用 github page 创建博客,并设置独立域名.网上找了许多教程,都太复杂.自己的创建过程 ...
- 接口测试postman深度挖掘应用③--postman终结篇
上一章节我们介绍了postman的变量测试以及导入数据测试基本上技术性的东西已经差不过了,这篇文章再系统性的介绍一下. 一.下载 官网:https://www.postman.com 1.选择需要下载 ...
- CAP 6.1 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 6.1 版本正式版,在这个版本中我们主要针对目前已经发现的几个BUG进行了修复了以及添加了一些小特性. 那么,接下来我们具体看一下吧. 总览 可能有些人还不知 ...
- 基于camunda开源流程引擎如何实现会签及会签原理解析
一.背景 市场上比较有名的开源流程引擎有osworkflow.jbpm.activiti.flowable.camunda.由于jbpm.activiti.flowable这几个流程引擎出现的比较早, ...
- 如何使用lerna进行多包(package)管理
为什么要用lerna 将大型代码仓库分割成多个独立版本化的 软件包(package)对于代码共享来说非常有用.但是,如果某些更改 跨越了多个代码仓库的话将变得很 麻烦 并且难以跟踪,并且, 跨越多个代 ...
- Vscode标题栏出现【不受支持】提示的完美解决方案
安装Fix VSCode Checksums code --install-extension lehni.vscode-fix-checksums 打开命令面板,快捷键 Ctrl + Shift + ...
- ABAP CDS - DEFINE VIEW, view_annot
Syntax ... @annotation ... Effect Specifies Annotation annotation in the definition of a CDS view of ...
- 聊聊 RPA 方向的规划:简单有价值的事情长期坚持做
「简单有价值的事情长期坚持做」 这是成功最简单,但也最难学的秘诀.不经过训练,人很难意识到时间复利的威力. 仙剑奇侠传的「十里坡剑神」和金庸群侠传的「十级野球拳」,就是简单的事情持之以恒反复做,最后就 ...
- jenkins配置自动执行sql脚本
shell脚本: bigsql="select big_version,small_version from d0mstore.db_current_version order by big ...