给出一张图,点集被分为两个部分,记做 AAA 和 BBB,每个部分有 nnn 个点
分别记做: A1,A2,...,AnA_1,A_2,...,A_nA1,A2,...,An 和 B1,B2,...,BnB_1,B_2,...,B_nB1,B2,...,Bn
连边: (Ai→Ai+1),(Bi→Bi+1)(A_i\rightarrow A_{i+1}),(B_i\rightarrow B_{i+1})(Ai→Ai+1),(Bi→Bi+1),容量分别为 xix_ixi 和 yiy_iyi
AAA 点集和 BBB 点集有 mmm 条边: (Aui→Bvi)(A_{u_i}\rightarrow B_{v_i})(Aui→Bvi),容量为 wiw_iwi
接下来有 qqq 次操作,每次操作把 (Ati→Ati+1)(A_{t_i}\rightarrow A_{t_{i+1}})(Ati→Ati+1) 的容量改为 pip_ipi
每次操作后输出 A1→BnA_1\rightarrow B_nA1→Bn 的最大流
最大流转最小割的以下结论:
- 在 AAA 中,若割掉了 (Ax→Ax+1)(A_x\rightarrow A_{x+1})(Ax→Ax+1),那么割掉 (Ay→Ay+1) (y>x)(A_y\rightarrow A_{y+1})~(y>x)(Ay→Ay+1) (y>x) 没有意义
- 在 BBB 中,若割掉了 (Bx→Bx+1)(B_x\rightarrow B_{x+1})(Bx→Bx+1),那么割掉 (By→By+1) (y<x)(B_y\rightarrow B_{y+1})~(y<x)(By→By+1) (y<x) 没有意义
因此 AAA 中至多有 111 条边属于割集,BBB 中至多 111 条边属于割集
设这两条边为 (Ax→Ax+1),(By→B(y+1))(A_x\rightarrow A_{x+1}),(B_y\rightarrow B_(y+1))(Ax→Ax+1),(By→B(y+1)),那么 (Au→B+v)(u≤x,y<v)(A_u\rightarrow B+v)(u \le x,y < v)(Au→B+v)(u≤x,y<v) 都属于割集
那么在 AAA 中从小到大枚举 AxA_xAx,问题在于在 BBB 中找到最优决策点 yyy 使得剩余代价最小
没加入一条 (Au→Bv)(A_u\rightarrow B_v)(Au→Bv) 的边,对答案造成的影响为连续一段,相当于区间加;
那么可以用线段树维护出对于所有 xxx 的剩余代价的最小值
注意到修改 AAA 边容量,这个剩余代价最大值不会改变
时间复杂度 O((n+q) log 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.逆向思维?
*/