BZOJ3638[Codeforces280D]k-Maximum Subsequence Sum&BZOJ3272Zgg吃东西&BZOJ3267KC采花——模拟费用流+线段树
题目描述
给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少。
输入
The first line contains integer n (1 ≤ n ≤ 105), showing how many numbers the sequence has. The next line contains n integers a1, a2, ..., an (|ai| ≤ 500).
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The next m lines contain the queries in the format, given in the statement.
All changing queries fit into limits: 1 ≤ i ≤ n, |val| ≤ 500.
All queries to count the maximum sum of at most k non-intersecting subsegments fit into limits: 1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 20. It is guaranteed that the number of the queries to count the maximum sum of at most k non-intersecting subsegments doesn't exceed 10000.
输出
For each query to count the maximum sum of at most k non-intersecting subsegments print the reply — the maximum sum. Print the answers to the queries in the order, in which the queries follow in the input.
样例输入
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3
样例输出
25
0
提示
In the first query of the first example you can select a single pair (1, 9). So the described sum will be 17.
Look at the second query of the first example. How to choose two subsegments? (1, 3) and (7, 9)? Definitely not, the sum we could get from (1, 3) and (7, 9) is 20, against the optimal configuration (1, 7) and (9, 9) with 25.
The answer to the third query is 0, we prefer select nothing if all of the numbers in the given interval are negative.
首先这个问题可以用费用流来解决:源点向每个点连边,容量为$1$,费用为$0$;第$i$个点向第$i+1$个点连边,容量为$1$,费用为$a_{i}$,每个点向汇点连边,容量为$1$,费用为$0$。可以知道每次增广一定是一段路径(即序列的最大连续子段和),然后将这段路径建反向边。那么我们可以模拟这个过程:每次取序列的最大连续子段和并将这一段的权值取反,假设第一次取了$[1,3]$而第二次取了$[3,5]$,那么相当于第一次取了$[1,2]$第二次取了$[4,5]$,中间的$3$在两次选取中权值抵消掉了。因为每次是贪心的取最大连续的一段,所以不会存在两次取的区间左端点或右端点相同,也就保证了每次取一个区间一定会增加一个子段。因为要取反,我们还要再维护最小连续子段和,然后在区间取反时将最大连续子段和与最小连续子段和调换一下即可。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int opt;
int l,r;
int x,k;
int ans;
int rev[400010];
struct miku
{
int x,l,r;
miku(){};
miku(int X,int L,int R){x=X,l=L,r=R;}
};
queue<miku>q;
struct Miku
{
miku smx,smn,lmx,lmn,rmx,rmn,sum;
}tr[400010];
bool operator <(miku a,miku b){return a.x<b.x;}
miku operator +(miku a,miku b){return miku(a.x+b.x,a.l,b.r);}
inline Miku pushup(Miku ls,Miku rs)
{
Miku rt;
rt.sum=ls.sum+rs.sum;
rt.smx=max(max(ls.smx,rs.smx),ls.rmx+rs.lmx);
rt.smn=min(min(ls.smn,rs.smn),ls.rmn+rs.lmn);
rt.lmx=max(ls.lmx,ls.sum+rs.lmx);
rt.lmn=min(ls.lmn,ls.sum+rs.lmn);
rt.rmx=max(rs.rmx,ls.rmx+rs.sum);
rt.rmn=min(rs.rmn,ls.rmn+rs.sum);
return rt;
}
inline void flip(int rt)
{
swap(tr[rt].smx,tr[rt].smn);
swap(tr[rt].lmx,tr[rt].lmn);
swap(tr[rt].rmx,tr[rt].rmn);
rev[rt]^=1;
tr[rt].sum.x*=-1;
tr[rt].smx.x*=-1;
tr[rt].smn.x*=-1;
tr[rt].lmx.x*=-1;
tr[rt].lmn.x*=-1;
tr[rt].rmx.x*=-1;
tr[rt].rmn.x*=-1;
}
inline void pushdown(int rt)
{
if(rev[rt])
{
rev[rt]^=1;
flip(rt<<1);
flip(rt<<1|1);
}
}
inline void build(int rt,int l,int r)
{
if(l==r)
{
scanf("%d",&x);
tr[rt].sum=tr[rt].smx=tr[rt].smn=tr[rt].lmx=tr[rt].lmn=tr[rt].rmx=tr[rt].rmn=miku(x,l,r);
return ;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
tr[rt]=pushup(tr[rt<<1],tr[rt<<1|1]);
}
inline void change(int rt,int l,int r,int k,int x)
{
if(l==r)
{
tr[rt].sum=tr[rt].smx=tr[rt].smn=tr[rt].lmx=tr[rt].lmn=tr[rt].rmx=tr[rt].rmn=miku(x,l,r);
return ;
}
pushdown(rt);
int mid=(l+r)>>1;
if(k<=mid)
{
change(rt<<1,l,mid,k,x);
}
else
{
change(rt<<1|1,mid+1,r,k,x);
}
tr[rt]=pushup(tr[rt<<1],tr[rt<<1|1]);
}
inline void reverse(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
flip(rt);
return ;
}
pushdown(rt);
int mid=(l+r)>>1;
if(L<=mid)
{
reverse(rt<<1,l,mid,L,R);
}
if(R>mid)
{
reverse(rt<<1|1,mid+1,r,L,R);
}
tr[rt]=pushup(tr[rt<<1],tr[rt<<1|1]);
}
inline Miku query(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
return tr[rt];
}
pushdown(rt);
int mid=(l+r)>>1;
if(R<=mid)
{
return query(rt<<1,l,mid,L,R);
}
else if(L>mid)
{
return query(rt<<1|1,mid+1,r,L,R);
}
else
{
return pushup(query(rt<<1,l,mid,L,R),query(rt<<1|1,mid+1,r,L,R));
}
}
int main()
{
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
while(m--)
{
scanf("%d",&opt);
if(!opt)
{
scanf("%d%d",&k,&x);
change(1,1,n,k,x);
}
else
{
scanf("%d%d%d",&l,&r,&k);
ans=0;
while(k--)
{
Miku res=query(1,1,n,l,r);
if(res.smx.x<0)
{
break;
}
ans+=res.smx.x;
q.push(res.smx);
reverse(1,1,n,res.smx.l,res.smx.r);
}
while(!q.empty())
{
miku res=q.front();
q.pop();
reverse(1,1,n,res.l,res.r);
}
printf("%d\n",ans);
}
}
}
BZOJ3638[Codeforces280D]k-Maximum Subsequence Sum&BZOJ3272Zgg吃东西&BZOJ3267KC采花——模拟费用流+线段树的更多相关文章
- 【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并
题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains inte ...
- BZOJ.3638.CF172 k-Maximum Subsequence Sum(模拟费用流 线段树)
题目链接 各种zz错误..简直了 /* 19604kb 36292ms 题意:选$k$段不相交的区间,使其权值和最大. 朴素线段树:线段树上每个点维护O(k)个信息,区间合并时O(k^2),总O(mk ...
- CF280D-k-Maximum Subsequence Sum【模拟费用流,线段树】
正题 题目链接:https://www.luogu.com.cn/problem/CF280D 题目大意 一个长度为\(n\)的序列,\(m\)次操作 修改一个数 询问一个区间中选出\(k\)段不交子 ...
- 中国大学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 ...
随机推荐
- koa入门
创建koa2工程 首先初始化项目 npm init -y 项目名称 安装koa $ npm i koa 我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- anaconda 出现add 。。。进不去
找到.condarc 文件 C:\Users\leiyi内 把里面内容替换为 channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pk ...
- scrapy之多环境的选择使用
scrapy之多环境的选择使用 个人主机主机上可能存在多个python环境,当在终端中使用scrapy时,容易产生错误,无法使用到自己想使用的那个python,如何解决这个问题呢? 出现这类问题时,直 ...
- 【问题解决方案】The MathType Dll cannot be found 问题解决方案
先贴几个可能的方法: 如何解决MathPage.wll或MathType.dll文件找不到问题 The MathType Dll cannot be found 问题解决办法 如果还搞不定,试试卸载重 ...
- O(N) 求数组中最大子串和
int MaxSubSum3(int *arr, int len) { int i; long long MaxSum = 0; long long CurSum = 0; for(int i = 0 ...
- 父级div宽度100%,子级一个div宽度固定,另一个宽度自适应
<!DOCTYPE html> <html> <head> <title>布局测试</title> <style type=" ...
- MySQL查询优化注意下面的四个细节
原文:http://bbs.landingbj.com/t-0-244231-1.html 在任何一个数据库中,查询优化都是不可避免的一个话题.对于数据库工程师来说,优化工作是最有挑战性的工作.MyS ...
- [官网]How to configure the Microsoft Distributed Transaction Coordinator (MSDTC) on Linux
How to configure the Microsoft Distributed Transaction Coordinator (MSDTC) on Linux APPLIES TO: SQL ...
- vue中的跨域问题
https://segmentfault.com/a/1190000011072725(原文) 使用vue-axios和vue-resource解决vue中调用网易云接口跨域的问题 注(api很重 ...