【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并
题目描述
给一列数,要求支持操作: 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
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3
样例输出
17
25
0
题解
模拟费用流+线段树区间合并
一开始想了个类似于dp的线段树区间合并结果一看数据范围果断放弃了。。。看了题解才知道是模拟费用流。。。
考虑如果用费用流的话怎么处理:每个点有一个大小为点权的费用,每次选择一段区间,获得这些点权和的费用,然后反向边使得它们的费用取相反数。
这个过程需要维护最大连续子段和(及其位置)、支持区间翻转。可以使用线段树来维护。
每个节点维护这段区间的区间和,包含左端点的最大连续子段和、包含右端点的最大连续子段和、整体的最大连续子段和,以及最小连续字段和;还要维护翻转标记。同时,对于和及连续字段和还要维护出现的区间位置。
每个询问不断的找区间内最大连续子段和,如果其大于0则取出并区间取相反数(模拟增广的过程)。最后再把这些取了相反数的区间还原回来。
代码量极大。。。强烈建议使用结构体重载运算符以减少代码量。
时间复杂度$O(nk\log n)$。
- #include <cstdio>
- #include <algorithm>
- #define N 100010
- #define lson l , mid , x << 1
- #define rson mid + 1 , r , x << 1 | 1
- using namespace std;
- struct data
- {
- int v , l , r;
- data() {}
- data(int V , int L , int R) {v = V , l = L , r = R;}
- bool operator<(const data &a)const {return v < a.v;}
- data operator+(const data &a)const {return data(v + a.v , l , a.r);}
- data operator-()const {return data(-v , l , r);}
- }now , sta[25];
- struct seg
- {
- data vsum , lmax , lmin , rmax , rmin , tmax , tmin;
- int rev;
- seg() {}
- seg(int v , int p)
- {
- vsum = data(v , p , p) , rev = 0;
- if(v > 0)
- {
- lmax = rmax = tmax = data(v , p , p);
- lmin = data(0 , p , p - 1) , rmin = data(0 , p + 1 , p) , tmin = data(0 , 0 , 0);
- }
- else
- {
- lmax = data(0 , p , p - 1) , rmax = data(0 , p + 1 , p) , tmax = data(0 , 0 , 0);
- lmin = rmin = tmin = data(v , p , p);
- }
- }
- seg operator+(const seg &a)const
- {
- seg ans;
- ans.vsum = vsum + a.vsum;
- ans.lmax = max(lmax , vsum + a.lmax) , ans.lmin = min(lmin , vsum + a.lmin);
- ans.rmax = max(a.rmax , rmax + a.vsum) , ans.rmin = min(a.rmin , rmin + a.vsum);
- ans.tmax = max(rmax + a.lmax , max(tmax , a.tmax)) , ans.tmin = min(rmin + a.lmin , min(tmin , a.tmin));
- ans.rev = 0;
- return ans;
- }
- }a[N << 2];
- inline void pushup(int x)
- {
- a[x] = a[x << 1] + a[x << 1 | 1];
- }
- inline void rever(int x)
- {
- swap(a[x].lmax , a[x].lmin) , swap(a[x].rmax , a[x].rmin) , swap(a[x].tmax , a[x].tmin);
- a[x].vsum = -a[x].vsum;
- a[x].lmax = -a[x].lmax , a[x].lmin = -a[x].lmin;
- a[x].rmax = -a[x].rmax , a[x].rmin = -a[x].rmin;
- a[x].tmax = -a[x].tmax , a[x].tmin = -a[x].tmin;
- a[x].rev ^= 1;
- }
- inline void pushdown(int x)
- {
- if(a[x].rev) rever(x << 1) , rever(x << 1 | 1) , a[x].rev = 0;
- }
- void build(int l , int r , int x)
- {
- if(l == r)
- {
- int v;
- scanf("%d" , &v) , a[x] = seg(v , l);
- return;
- }
- int mid = (l + r) >> 1;
- build(lson) , build(rson);
- pushup(x);
- }
- void modify(int p , int v , int l , int r , int x)
- {
- if(l == r)
- {
- a[x] = seg(v , l);
- return;
- }
- pushdown(x);
- int mid = (l + r) >> 1;
- if(p <= mid) modify(p , v , lson);
- else modify(p , v , rson);
- pushup(x);
- }
- void update(int b , int e , int l , int r , int x)
- {
- if(b <= l && r <= e)
- {
- rever(x);
- return;
- }
- pushdown(x);
- int mid = (l + r) >> 1;
- if(b <= mid) update(b , e , lson);
- if(e > mid) update(b , e , rson);
- pushup(x);
- }
- seg query(int b , int e , int l , int r , int x)
- {
- if(b <= l && r <= e) return a[x];
- pushdown(x);
- int mid = (l + r) >> 1;
- if(e <= mid) return query(b , e , lson);
- else if(b > mid) return query(b , e , rson);
- else return query(b , e , lson) + query(b , e , rson);
- }
- int main()
- {
- int n , m , opt , x , y , z , tot , ans;
- scanf("%d" , &n);
- build(1 , n , 1);
- scanf("%d" , &m);
- while(m -- )
- {
- scanf("%d%d%d" , &opt , &x , &y);
- if(opt == 1)
- {
- scanf("%d" , &z) , tot = ans = 0;
- while(tot < z)
- {
- now = query(x , y , 1 , n , 1).tmax;
- if(now.v <= 0) break;
- ans += now.v , update(now.l , now.r , 1 , n , 1);
- sta[++tot] = now;
- }
- printf("%d\n" , ans);
- while(tot) update(sta[tot].l , sta[tot].r , 1 , n , 1) , tot -- ;
- }
- else modify(x , y , 1 , n , 1);
- }
- return 0;
- }
【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并的更多相关文章
- BZOJ.3638.CF172 k-Maximum Subsequence Sum(模拟费用流 线段树)
题目链接 各种zz错误..简直了 /* 19604kb 36292ms 题意:选$k$段不相交的区间,使其权值和最大. 朴素线段树:线段树上每个点维护O(k)个信息,区间合并时O(k^2),总O(mk ...
- Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]
洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- BZOJ3638[Codeforces280D]k-Maximum Subsequence Sum&BZOJ3272Zgg吃东西&BZOJ3267KC采花——模拟费用流+线段树
题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains inte ...
- CF280D-k-Maximum Subsequence Sum【模拟费用流,线段树】
正题 题目链接:https://www.luogu.com.cn/problem/CF280D 题目大意 一个长度为\(n\)的序列,\(m\)次操作 修改一个数 询问一个区间中选出\(k\)段不交子 ...
- BZOJ2040[2009国家集训队]拯救Protoss的故乡——模拟费用流+线段树+树链剖分
题目描述 在星历2012年,星灵英雄Zeratul预测到他所在的Aiur行星在M天后会发生持续性暴雨灾害,尤其是他们的首都.而Zeratul作为星灵族的英雄,当然是要尽自己最大的努力帮助星灵族渡过这场 ...
- 【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)
[BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交 ...
- HDU5107---K-short Problem (线段树区间 合并、第k大)
题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...
- BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图
Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i]]这么多段长度为1时间中选出一个时间进行抢劫,并计划抢 ...
随机推荐
- 使用Git操作码云
一.安装并配置 .安装git 下载地址: 官方网站:https://git-for-windows.github.io/ 国内镜像:https://pan.baidu.com/s/1kU5OCOB#l ...
- margin与padding大比拼
用margin还是用padding这个问题相信是每个学css的人都想要去深入了解的. CSS边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距 ...
- DNS的主从,转发与负载功能
接着原来<DNS原理与应用>的文章,本章内容主要通过实现DNS的主从,转发,及基于域名解析不同的ip实现后端服务负载均衡的效果.最后再实现DNS的高级功能:类似CDN原理实现基于IP实现区 ...
- kubernetes基础架构及原理
kubernetes简称“k8s” 其中“8”代表的是“k”和“s”中间的8个字母. k8s是Google公司开发的Borg项目中独立出来的容器编排工具,然后将其捐献给CNCF这个组织,然后发扬光大. ...
- Sublime Text3的快捷键和插件
今天重装了一下Sublime Text3,发现了一个不错的网站,关于Sublime Text3的插件安装介绍的很详细,还有右键增强菜单和浏览器打开快捷键的创建.奉上链接 http://www.cnbl ...
- scrapy--Cookies
大家好,之前看到的关于cookies的应用,由于有段时间没看,再看的时候花了一些时间,来给大家总结下.本文是根据:"http://www.bubuko.com/infodetail-2233 ...
- Python中的文件和目录操作实现
Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...
- 创建数据库配置文件ini(转)
一.有必要了解INI文件的结构: ;注释 [小节名] 关键字=值 ... ---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. ---- 值的类型有三种:字符串 ...
- Hive学习路线图
- PTA 7-10(图) 旅游规划 最短路问题
7-10(图) 旅游规划 (25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果 ...