【BZOJ-3638327232673502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广

本文主要是介绍【BZOJ-3638327232673502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

3638: Cf172 k-Maximum Subsequence Sum

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 174  Solved: 92
[Submit][Status][Discuss]

Description

给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少。

Input

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 ≤ n1 ≤ k ≤ 20. It is guaranteed that the number of the queries to count the maximum sum of at mostk non-intersecting subsegments doesn't exceed 10000.

Output

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.

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3

Sample Output

17
25
0

HINT

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.

Source

By xiaodao

3272: Zgg吃东西

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 93  Solved: 59
[Submit][Status][Discuss]

Description

Zgg是一个吃货。他面前有一堆的食品。不妨把它看成一个序列,序列从1~N编号,第i个位置上有美味度为A[i]的食品。Zgg有很多只手(为什么他有这么多手?我也不知道)。他的手的作用,就是抓起一段连续序列中的食物,并且不限长度。总共有M个时刻,在每个时刻,可能会有一些奇怪的人将他面前的某一个位置的食物美味度改变。或者zgg突然心血来潮,想要用他的其中k只手来抓取某段区间中的食物。这时候,他就需要你告诉他,在用不超过k只手的情况下,他能获得的最大美味度是多少。

Input

第一行一个整数N,表示食品的个数,即序列长度。
第2行有N个数依次表示A[i]。
第三行一个整数M,表示有M个时刻。
接下来的M行,每行有形如0 i val
来表示有人将第i位置的食物的美味度改成了val
或者1 l r k,来表示zgg想知道,他只用k只手,在[l,r]这段区间内抓取,能获得的最大美味度。

Output

对于每次形如1 l r k的询问你都要输出一个数,表示最大美味度。

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
5
1 1 9 1
1 1 9 2
1 4 6 3
0 3 -8
1 1 9 1

Sample Output

17
25
0
10

HINT

       对于100%的数据 N,M<=100000 ,1<=k<=20.l<=l<=r<=n.数值的绝对值不会超过500.
       Zgg是可以有手空闲的…

Source

3267: KC采花

Time Limit: 30 Sec  Memory Limit: 256 MB
Submit: 65  Solved: 51
[Submit][Status][Discuss]

Description

KC在公园里种了一排花,一共有n朵,游手好闲的KC常常在公园里采花。他对每朵花都有一个美丽度鉴赏。由于对花的喜好不同,有的花分数很高,有的花分数很低,甚至会是负数。
KC很忙,每次采花的时候,不可能从第一朵花,走到第n朵。所以他会先选定一个区间[l,r](1<=l<=r<=n),作为当天的采花范围。同时为了方便采花,他总是从[l,r]中最多选出k个互不相交的子区间,将这些子区间的花全部采光。当然,他希望美丽度总和最大。
KC对花的鉴赏随着他对世界观人生观的改变而改变,他会不时地对每朵花的美丽度进行修改,可能改低,也可能提高。
KC的行为持续m天,每天的行为要么是采花,要么是改变花的美丽度。
注:(1)[l,r]的最多k个互不相同子区间可以表示成:[x1,y1],[x2,y2],...,[xt,yt],满足l<=x1<=y1<x2<=y2<...<xt<=yt<=r,且0<=t<=k。
(2)由于是KC种的花,一朵花采掉第二天会立刻生出来。

Input

第一行一个正整数n,n<=100000。
第二行n个整数a1,a2...an,表示n朵花的美丽度。|ai|<=500。
第三行一个正整数m,m<=100000。
第四行开始,接下来m行,每行表示该天KC的行为。
修改美丽度的行为用0 i val描述,表示将ai修改为val,|val|<=500。
采花行为用1 l r k描述,k<=20意义如题面。

Output

对于每个采花行为,每行一个整数表示最大的美丽度总和。

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3

Sample Output

17
25
0

HINT

100%的数据,满足n,m<=50000,k<=20,ai以及修改的val的绝对值不超过500。

Source

最大费用最大流

Solution

这道题,首先想到最大费用最大流,这类的对序列上的问题,是比较经典的问题,详见 《网络流与线性规划24题》最长k可重区间集问题

那么这道题,最标准的做法就是:

把每个数拆成两个点$I_{1}$和$I_{2}$  $I_{1}-->I_{2}$,$cap=1$,$cost=a[i]$

然后$S-->I_{1}$,$cap=INF$,$cost=0$;以及$I_{2}-->T$,$cap=INF$,$cost=0$;

并且相邻的两位连$I'_{2}-->I''_{1}$,$cap=1$,$cost=0$

那么只需要跑$K$次最大费用最大流,累加答案即可

 

但是很显然这种数据范围,这么做会TLE,那么不妨考虑对这种做法进行优化:

每次增广的过程,实质上就是取一段和最大的子序列,并将其反转

很显然对于这类序列上的操作,可以用线段树去实现,那么这道题的正确做法就明确了

费用流的构图,用线段树去手动模拟增广路程

 

线段树维护的方法,只需要维护一段区间的最大和子序列,涉及取反,所以还需要维护最小和子序列

但是发现,每进行一次取反,当前最大和子序列一定变成最小和子序列,最小和子序列一定变成最大,那么直接swap一下就好

鉴于一次询问需要增广K次,也就是一次询问要取反,所以显然需要开一个栈记录一下当前询问所反转的所有区间,在结束时还原

修改操作直接修改即可

 

总的时间复杂度是$O(knlogn)$

Code

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
inline int read()
{int x=0,f=1; char ch=getchar();while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}return x*f;
}
#define maxn 101000
int n,m,a[maxn],ans;
struct DataNode
{int lx,rx,mx,sum,pl,pr,p1,p2;void add(int pos,int x) {p1=p2=pl=pr=pos,mx=sum=lx=rx=x;}
};
struct SegmentTreeNode
{int l,r,tag;DataNode maxx,minn;void add(int x) {maxx.add(l,x); minn.add(l,-x);}
}tree[maxn<<2];
inline DataNode Merge(DataNode ls,DataNode rs)
{DataNode rt;rt.sum=ls.sum+rs.sum;rt.lx=ls.sum+rs.lx>ls.lx? ls.sum+rs.lx : ls.lx;rt.pl=ls.sum+rs.lx>ls.lx? rs.pl : ls.pl;rt.rx=rs.sum+ls.rx>rs.rx? rs.sum+ls.rx : rs.rx;rt.pr=rs.sum+ls.rx>rs.rx? ls.pr : rs.pr;rt.mx=ls.rx+rs.lx; rt.p1=ls.pr; rt.p2=rs.pl;if (rt.mx<ls.mx) rt.mx=ls.mx,rt.p1=ls.p1,rt.p2=ls.p2;if (rt.mx<rs.mx) rt.mx=rs.mx,rt.p1=rs.p1,rt.p2=rs.p2;return rt;
}
inline void Update(int now)
{tree[now].minn=Merge(tree[now<<1].minn,tree[now<<1|1].minn);tree[now].maxx=Merge(tree[now<<1].maxx,tree[now<<1|1].maxx);
}
inline void Pushdown(int now)
{if (!tree[now].tag || tree[now].l==tree[now].r) return;tree[now].tag^=1;swap(tree[now<<1].maxx,tree[now<<1].minn);swap(tree[now<<1|1].minn,tree[now<<1|1].maxx);tree[now<<1].tag^=1; tree[now<<1|1].tag^=1;        
}
inline void BuildTree(int now,int l,int r)
{tree[now].l=l; tree[now].r=r; tree[now].tag=0;if (l==r) {tree[now].add(a[l]); return;}int mid=(l+r)>>1;BuildTree(now<<1,l,mid); BuildTree(now<<1|1,mid+1,r);Update(now);
}
inline void Change(int now,int pos,int x)
{Pushdown(now);if (tree[now].l==tree[now].r) {tree[now].add(x); return;}int mid=(tree[now].l+tree[now].r)>>1;if (pos<=mid) Change(now<<1,pos,x); else Change(now<<1|1,pos,x);Update(now);
}
inline DataNode Query(int now,int L,int R)
{Pushdown(now);if (L==tree[now].l && R==tree[now].r) return tree[now].maxx;int mid=(tree[now].l+tree[now].r)>>1;if (R<=mid) return Query(now<<1,L,R);if (L>mid) return Query(now<<1|1,L,R);return Merge(Query(now<<1,L,mid),Query(now<<1|1,mid+1,R));
}
inline void Reverse(int now,int L,int R)
{Pushdown(now);if (L<=tree[now].l && R>=tree[now].r) {swap(tree[now].maxx,tree[now].minn); tree[now].tag^=1; return;}int mid=(tree[now].l+tree[now].r)>>1;if (L<=mid) Reverse(now<<1,L,R);if (R>mid) Reverse(now<<1|1,L,R);Update(now);
}
struct StackNode
{int l,r;void Push(int L,int R) {l=L,r=R;}
}stack[21];
int top;
inline void SolveAsk(int L,int R,int K)
{ans=0; top=0;while (K--){DataNode tmp=Query(1,L,R);if (tmp.mx>0) ans+=tmp.mx; else break;Reverse(1,tmp.p1,tmp.p2);stack[++top].Push(tmp.p1,tmp.p2);}for (int i=top; i>=1; i--)Reverse(1,stack[i].l,stack[i].r);printf("%d\n",ans);        
}
inline void SolveChange(int pos,int x) {Change(1,pos,x);}
void Freopen() {freopen("data.in","r",stdin);freopen("data.out","w",stdout);}
void Fclose() {fclose(stdin);fclose(stdout);}
int main()
{
//    Freopen();n=read();for (int i=1; i<=n; i++) a[i]=read();BuildTree(1,1,n);m=read();while (m--){int opt=read(),L,R,K,x,d;switch (opt){case 1 : L=read(),R=read(),K=read(); SolveAsk(L,R,K); break;case 0 : x=read(),d=read(); SolveChange(x,d); break;}}
//    Fclose();return 0;
}

3502: PA2012 Tanie linie

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 181  Solved: 36
[Submit][Status][Discuss]

Description

 n个数字,求不相交的总和最大的最多k个连续子序列。
 1<= k<= N<= 1000000。

Input

Output

Sample Input

5 2
7 -3 4 -9 5

Sample Output

13

HINT

Source

Solution

和刚刚一样,但是范围扩大了,需要开longlong,所以内存很卡,时间也很卡..

Code

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
inline int read()
{int x=0,f=1; char ch=getchar();while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}return x*f;
}
#define maxn 1000010
int n,m,a[maxn];long long ans;
struct DataNode
{long long lx,rx,mx,sum;int pl,pr,p1,p2;void add(int pos,int x) {p1=p2=pl=pr=pos,mx=sum=lx=rx=x;}
};
struct SegmentTreeNode
{int l,r,tag;DataNode maxx,minn;void add(long long x) {maxx.add(l,x); minn.add(l,-x);}
}tree[maxn*2+200000];
inline DataNode Merge(DataNode ls,DataNode rs)
{DataNode rt;rt.sum=ls.sum+rs.sum;rt.lx=ls.sum+rs.lx>ls.lx? ls.sum+rs.lx : ls.lx;rt.pl=ls.sum+rs.lx>ls.lx? rs.pl : ls.pl;rt.rx=rs.sum+ls.rx>rs.rx? rs.sum+ls.rx : rs.rx;rt.pr=rs.sum+ls.rx>rs.rx? ls.pr : rs.pr;rt.mx=ls.rx+rs.lx; rt.p1=ls.pr; rt.p2=rs.pl;if (rt.mx<ls.mx) rt.mx=ls.mx,rt.p1=ls.p1,rt.p2=ls.p2;if (rt.mx<rs.mx) rt.mx=rs.mx,rt.p1=rs.p1,rt.p2=rs.p2;return rt;
}
inline void Update(int now)
{tree[now].minn=Merge(tree[now<<1].minn,tree[now<<1|1].minn);tree[now].maxx=Merge(tree[now<<1].maxx,tree[now<<1|1].maxx);
}
inline void Pushdown(int now)
{if (!tree[now].tag || tree[now].l==tree[now].r) return;tree[now].tag^=1;swap(tree[now<<1].maxx,tree[now<<1].minn);swap(tree[now<<1|1].minn,tree[now<<1|1].maxx);tree[now<<1].tag^=1; tree[now<<1|1].tag^=1;        
}
inline void BuildTree(int now,int l,int r)
{tree[now].l=l; tree[now].r=r; tree[now].tag=0;if (l==r) {tree[now].add(a[l]); return;}int mid=(l+r)>>1;BuildTree(now<<1,l,mid); BuildTree(now<<1|1,mid+1,r);Update(now);
}
inline DataNode Query(int now,int L,int R)
{Pushdown(now);if (L==tree[now].l && R==tree[now].r) return tree[now].maxx;int mid=(tree[now].l+tree[now].r)>>1;if (R<=mid) return Query(now<<1,L,R);if (L>mid) return Query(now<<1|1,L,R);return Merge(Query(now<<1,L,mid),Query(now<<1|1,mid+1,R));
}
inline void Reverse(int now,int L,int R)
{Pushdown(now);if (L<=tree[now].l && R>=tree[now].r) {swap(tree[now].maxx,tree[now].minn); tree[now].tag^=1; return;}int mid=(tree[now].l+tree[now].r)>>1;if (L<=mid) Reverse(now<<1,L,R);if (R>mid) Reverse(now<<1|1,L,R);Update(now);
}
inline void SolveAsk(int L,int R,int K)
{while (K--){DataNode tmp=Query(1,L,R);if (tmp.mx>0) ans+=tmp.mx; else break;Reverse(1,tmp.p1,tmp.p2);}printf("%lld\n",ans);   
}
int main()
{n=read(); int K=read();for (int i=1; i<=n; i++) a[i]=read();BuildTree(1,1,n);SolveAsk(1,n,K);return 0;
}
View Code

 

转载于:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5634149.html

这篇关于【BZOJ-3638327232673502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567291

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

poj 2175 最小费用最大流TLE

题意: 一条街上有n个大楼,坐标为xi,yi,bi个人在里面工作。 然后防空洞的坐标为pj,qj,可以容纳cj个人。 从大楼i中的人到防空洞j去避难所需的时间为 abs(xi - pi) + (yi - qi) + 1。 现在设计了一个避难计划,指定从大楼i到防空洞j避难的人数 eij。 判断如果按照原计划进行,所有人避难所用的时间总和是不是最小的。 若是,输出“OPETIMAL",若

poj 2135 有流量限制的最小费用最大流

题意: 农场里有n块地,其中约翰的家在1号地,二n号地有个很大的仓库。 农场有M条道路(双向),道路i连接着ai号地和bi号地,长度为ci。 约翰希望按照从家里出发,经过若干块地后到达仓库,然后再返回家中的顺序带朋友参观。 如果要求往返不能经过同一条路两次,求参观路线总长度的最小值。 解析: 如果只考虑去或者回的情况,问题只不过是无向图中两点之间的最短路问题。 但是现在要去要回

poj 3422 有流量限制的最小费用流 反用求最大 + 拆点

题意: 给一个n*n(50 * 50) 的数字迷宫,从左上点开始走,走到右下点。 每次只能往右移一格,或者往下移一格。 每个格子,第一次到达时可以获得格子对应的数字作为奖励,再次到达则没有奖励。 问走k次这个迷宫,最大能获得多少奖励。 解析: 拆点,拿样例来说明: 3 2 1 2 3 0 2 1 1 4 2 3*3的数字迷宫,走两次最大能获得多少奖励。 将每个点拆成两个

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为