Codeforces 908G Yet Another Maxflow Problem (最小割定理,线段树)

给出一张图,点集被分为两个部分,记做 A A A B B B,每个部分有 n n n 个点

分别记做: A 1 , A 2 , . . . , A n A_1,A_2,...,A_n A1,A2,...,An B 1 , B 2 , . . . , B n B_1,B_2,...,B_n B1,B2,...,Bn

连边: ( A i → A i + 1 ) , ( B i → B i + 1 ) (A_i\rightarrow A_{i+1}),(B_i\rightarrow B_{i+1}) (AiAi+1),(BiBi+1),容量分别为 x i x_i xi y i y_i yi

A A A 点集和 B B B 点集有 m m m 条边: ( A u i → B v i ) (A_{u_i}\rightarrow B_{v_i}) (AuiBvi),容量为 w i w_i wi

接下来有 q q q 次操作,每次操作把 ( A t i → A t i + 1 ) (A_{t_i}\rightarrow A_{t_{i+1}}) (AtiAti+1) 的容量改为 p i p_i pi

每次操作后输出 A 1 → B n A_1\rightarrow B_n A1Bn 的最大流

最大流转最小割的以下结论:

  • A A A 中,若割掉了 ( A x → A x + 1 ) (A_x\rightarrow A_{x+1}) (AxAx+1),那么割掉 ( A y → A y + 1 )   ( y > x ) (A_y\rightarrow A_{y+1})~(y>x) (AyAy+1) (y>x) 没有意义
  • B B B 中,若割掉了 ( B x → B x + 1 ) (B_x\rightarrow B_{x+1}) (BxBx+1),那么割掉 ( B y → B y + 1 )   ( y &lt; x ) (B_y\rightarrow B_{y+1})~(y&lt;x) (ByBy+1) (y<x) 没有意义

因此 A A A 中至多有 1 1 1 条边属于割集, B B B 中至多 1 1 1 条边属于割集

设这两条边为 ( A x → A x + 1 ) , ( B y → B ( y + 1 ) ) (A_x\rightarrow A_{x+1}),(B_y\rightarrow B_(y+1)) (AxAx+1)(ByB(y+1)),那么 ( A u → B + v ) ( u ≤ x , y &lt; v ) (A_u\rightarrow B+v)(u \le x,y &lt; v) (AuB+v)(ux,y<v) 都属于割集

那么在 A A A 中从小到大枚举 A x A_x Ax,问题在于在 B B B 中找到最优决策点 y y y 使得剩余代价最小

没加入一条 ( A u → B v ) (A_u\rightarrow B_v) (AuBv) 的边,对答案造成的影响为连续一段,相当于区间加;

那么可以用线段树维护出对于所有 x x x 的剩余代价的最小值

注意到修改 A A A 边容量,这个剩余代价最大值不会改变

时间复杂度 O ( ( n + q )   l o g   n ) O((n+q)~log~n) O((n+q) log n)

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int a[N], b[N], c[N] ;
vii e[N] ;
int n, m, q ;

class Seg {
public:
	struct Var {
		int l, r, v, tag ;
	} tr[N << 2] ;
	#define ls(x) x << 1
	#define rs(x) x << 1 | 1
	#define l(x) tr[x].l
	#define r(x) tr[x].r
	#define v(x) tr[x].v
	#define tag(x) tr[x].tag
	void pushup(int x) {
		v(x) = min(v(ls(x)), v(rs(x))) ;
	}
	void pushdown(int x) {
		if (tag(x)) {
			tag(ls(x)) += tag(x) ;
			tag(rs(x)) += tag(x) ;
			v(ls(x)) += tag(x) ;
			v(rs(x)) += tag(x) ;
			tag(x) = 0 ;
		}
	}
	void build(int x, int l, int r, ll a[]) {
		l(x) = l, r(x) = r, tag(x) = 0 ;
		if (l == r) {
			v(x) = a[l] ;
			return ;
		}
		int mid = (l + r) >> 1 ;
		build(ls(x), l, mid, a) ;
		build(rs(x), mid + 1, r, a) ;
		pushup(x) ;
	}
	void modify(int x, int l, int r, int c) {
		if (l <= l(x) && r(x) <= r) {
			v(x) += c ;
			tag(x) += c ;
			return ;
		}
		pushdown(x) ;
		int mid = (l(x) + r(x)) >> 1 ;
		if (l <= mid) modify(ls(x), l, r, c) ;
		if (mid < r) modify(rs(x), l, r, c) ;
		pushup(x) ;
	}
	int query() {
		return v(1) ;
	}
} t ;

signed main(){
	scanf("%lld%lld%lld", &n, &m, &q) ;
	rep(i, 1, n - 1) scanf("%lld%lld", &a[i], &b[i + 1]) ;
	t.build(1, 1, n, b) ;
	rep(i, 1, m) {
		int x, y, z ; scanf("%lld%lld%lld", &x, &y, &z) ;
		e[x].pb(mp(y, z)) ;
	}
	rep(i, 1, n) {
		rep(j, 0, siz(e[i]) - 1){
			int x = e[i][j].fi, y = e[i][j].se ;
			t.modify(1, 1, x, y) ;
		}
		c[i] = t.query() + a[i] ;
	}
	t.build(1, 1, n, c) ;
	printf("%lld\n", t.query()) ;
	while (q--) {
		int x, y ; scanf("%lld%lld", &x, &y) ;
		t.modify(1, x, x, y - a[x]) ;
		a[x] = y ;
		printf("%lld\n", t.query()) ;
	}
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值