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

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

分别记做: \(A_1,A_2,...,A_n\)\(B_1,B_2,...,B_n\)

连边: \((A_i\rightarrow A_{i+1}),(B_i\rightarrow B_{i+1})\),容量分别为 \(x_i\)\(y_i\)

\(A\) 点集和 \(B\) 点集有 \(m\) 条边: \((A_{u_i}\rightarrow B_{v_i})\),容量为 \(w_i\)

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

每次操作后输出 \(A_1\rightarrow B_n\) 的最大流

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

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

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

设这两条边为 \((A_x\rightarrow A_{x+1}),(B_y\rightarrow B_(y+1))\),那么 \((A_u\rightarrow B+v)(u \le x,y < v)\) 都属于割集

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

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

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

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

时间复杂度 \(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.逆向思维?
*/


posted @ 2019-02-28 19:25  harryhqg  阅读(192)  评论(0编辑  收藏  举报