CF280D k-Maximum Subsequence Sum
题目链接:洛谷
题目大意:【题意翻译已经够直白了】
首先,相信大家一开始都是想去直接dp,但是发现复杂度不对。
于是我们考虑一个黑科技:模拟费用流(相信大部分人看见数据范围就绝对不会想到费用流)
我们考虑进行拆点,设i->i':(1,a[i]),i'->i+1:(1,0),而且总流量$\leq k$,然后就跑最大费用最大流。
这显然会$T$成sb,不过这个图比较简单,我们可以考虑手玩一下。
然后就发现每次的增广路就是这个区间里的最大和的子段对应的路径,然后再把这个最大和的子段里面所有数取相反数(反向边)。
然后就可以用线段树维护,这个线段树需要以下操作。
1.单点修改
2.对于区间[l,r],先找出这个区间的最大和的子段,然后把这个子段取相反数。
不过码量让人想哭。。。(好不容易调出来,纪念一下。
#include<bits/stdc++.h>
#define Rint register int
using namespace std;
const int N = ;
struct Node {
int L, R, sum, maxv, maxl, maxr, minv, minl, minr, lmax, lmaxid, lmin, lminid, rmax, rmaxid, rmin, rminid;
bool rev;
inline Node(int pos = , int val = ){
L = R = maxl = maxr = minl = minr = lmaxid = lminid = rmaxid = rminid = pos;
sum = maxv = minv = lmax = lmin = rmax = rmin = val;
rev = false;
}
} seg[N << ], q[];
inline Node merge(const Node &a, const Node &b){
Node now;
now.L = a.L; now.R = b.R;
now.sum = a.sum + b.sum;
if(a.lmax >= a.sum + b.lmax) now.lmax = a.lmax, now.lmaxid = a.lmaxid;
else now.lmax = a.sum + b.lmax, now.lmaxid = b.lmaxid;
if(a.lmin <= a.sum + b.lmin) now.lmin = a.lmin, now.lminid = a.lminid;
else now.lmin = a.sum + b.lmin, now.lminid = b.lminid;
if(b.rmax >= b.sum + a.rmax) now.rmax = b.rmax, now.rmaxid = b.rmaxid;
else now.rmax = b.sum + a.rmax, now.rmaxid = a.rmaxid;
if(b.rmin <= b.sum + a.rmin) now.rmin = b.rmin, now.rminid = b.rminid;
else now.rmin = b.sum + a.rmin, now.rminid = a.rminid;
now.maxv = a.rmax + b.lmax; now.maxl = a.rmaxid; now.maxr = b.lmaxid;
if(now.maxv < a.maxv) now.maxv = a.maxv, now.maxl = a.maxl, now.maxr = a.maxr;
if(now.maxv < b.maxv) now.maxv = b.maxv, now.maxl = b.maxl, now.maxr = b.maxr;
now.minv = a.rmin + b.lmin; now.minl = a.rminid; now.minr = b.lminid;
if(now.minv > a.minv) now.minv = a.minv, now.minl = a.minl, now.minr = a.minr;
if(now.minv > b.minv) now.minv = b.minv, now.minl = b.minl, now.minr = b.minr;
now.rev = false;
return now;
}
inline void pushup(int x){seg[x] = merge(seg[x << ], seg[x << | ]);}
inline void rev(Node &a){
a.rev ^= ;
a.sum = -a.sum;
swap(a.lmax, a.lmin); swap(a.lmaxid, a.lminid);
a.lmax = -a.lmax; a.lmin = -a.lmin;
swap(a.rmax, a.rmin); swap(a.rmaxid, a.rminid);
a.rmax = -a.rmax; a.rmin = -a.rmin;
swap(a.maxv, a.minv); swap(a.maxl, a.minl); swap(a.maxr, a.minr);
a.maxv = -a.maxv; a.minv = -a.minv;
}
inline void pushdown(int x){
if(seg[x].rev){
rev(seg[x << ]);
rev(seg[x << | ]);
seg[x].rev = false;
}
}
int n, m, a[N];
inline void build(int x, int L, int R){
if(L == R){
seg[x] = Node(L, a[L]);
return;
}
int mid = L + R >> ;
build(x << , L, mid);
build(x << | , mid + , R);
pushup(x);
}
inline void change(int x, int L, int R, int pos, int val){
if(L == R){
seg[x] = Node(L, val);
return;
}
int mid = L + R >> ;
pushdown(x);
if(pos <= mid) change(x << , L, mid, pos, val);
else change(x << | , mid + , R, pos, val);
pushup(x);
}
inline void modify(int x, int L, int R, int l, int r){
if(l <= L && R <= r){
rev(seg[x]);
return;
}
int mid = L + R >> ;
pushdown(x);
if(l <= mid) modify(x << , L, mid, l, r);
if(mid < r) modify(x << | , mid + , R, l, r);
pushup(x);
}
inline Node query(int x, int L, int R, int l, int r){
if(l <= L && R <= r) return seg[x];
int mid = L + R >> ;
pushdown(x);
if(r <= mid) return query(x << , L, mid, l, r);
else if(mid < l) return query(x << | , mid + , R, l, r);
else return merge(query(x << , L, mid, l, r), query(x << | , mid + , R, l, r));
}
//inline void dfs(int x, int L, int R){
// if(L == R){
// printf("%d ", seg[x].maxv);
// return;
// }
// int mid = L + R >> 1;
// pushdown(x);
// dfs(x << 1, L, mid);
// dfs(x << 1 | 1, mid + 1, R);
//}
int main(){
scanf("%d", &n);
for(Rint i = ;i <= n;i ++) scanf("%d", a + i);
build(, , n);
scanf("%d", &m);
while(m --){
int opt, x, y, k;
scanf("%d%d%d", &opt, &x, &y);
if(opt == )
change(, , n, x, y);
else {
scanf("%d", &k);
int ans = , pos = k;
for(Rint i = ;i <= k;i ++){
q[i] = query(, , n, x, y);
if(q[i].maxv <= ){pos = i - ; break;}
modify(, , n, q[i].maxl, q[i].maxr);
// dfs(1, 1, n); puts("");
ans += q[i].maxv;
}
for(Rint i = ;i <= pos;i ++) modify(, , n, q[i].maxl, q[i].maxr);
printf("%d\n", ans);
}
}
}
CF280D k-Maximum Subsequence Sum的更多相关文章
- 中国大学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 ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
随机推荐
- IDEA使用笔记(九)——设置文件注释
方式一:后写文件描述信息 1:设置——如下图所示 2:验证——创建个类试试 3:验证——结果如下 4:其他,所有注释模版中包含 #parse("File Header.java") ...
- 免费ss账号网站
下面网址按排序顺序优先使用,数字越小优先级越高 1,https://io.freess.today/ 2,https://free-ss.site/ 3,https://ss.freess.org/ ...
- Python PhantomJS 爬虫 示例
from selenium import webdriver# 请求url url = "https://auctions.freemansauction.com/auction-lot-d ...
- Unity3D中录制和输出wav文件
近期在做视频录制方面的事情,看了下音频的录制和输出.主要参考官方的FrameCapturer: https://github.com/unity3d-jp/FrameCapturer wav文件结构较 ...
- Mybatis(五) 延迟加载和缓存机制(一级二级缓存)
踏踏实实踏踏实实,开开心心,开心是一天不开心也是一天,路漫漫其修远兮. --WH 一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的 ...
- halcon 特征测量
Features 1. line_orientation 功能:计算线的方位. 2. line_position 功能:计算一条线的重心.长度和方位. 3. partition_lines ...
- NuGet Packages are missing,This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.
错误内容 This project references NuGet package(s) that are missing on this computer. Use NuGet Package R ...
- Java开发面试题整理(2019春招)
一.Java基础部分 1. HashMap和Hashtable各有什么特点,它们有什么区别?(必背题,超级重要) HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们 ...
- 深入理解String类详解
1.Stringstr = "eee" 和String str = new String("eee")的区别 先看一小段代码, 1 public static ...
- python什么时候加self,什么时候不加self
1.self是什么,一般都说指对象本身,这样说了没了用,说了后还是很难懂,因为这样说了后,仍然完全搞不清楚,什么时候变量前需要加self,什么时候不需要加self. 造成很多人,已经怕了self,不停 ...