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

本文介绍了一种针对最大流最小割问题的高效算法优化方案,通过线段树维护和决策点选择,实现O((n+q) log n)的时间复杂度。讨论了在特定图结构下,如何通过枚举和维护剩余代价最小值来快速求解最大流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给出一张图,点集被分为两个部分,记做 AAABBB,每个部分有 nnn 个点

分别记做: A1,A2,...,AnA_1,A_2,...,A_nA1,A2,...,AnB1,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})(AiAi+1),(BiBi+1),容量分别为 xix_ixiyiy_iyi

AAA 点集和 BBB 点集有 mmm 条边: (Aui→Bvi)(A_{u_i}\rightarrow B_{v_i})(AuiBvi),容量为 wiw_iwi

接下来有 qqq 次操作,每次操作把 (Ati→Ati+1)(A_{t_i}\rightarrow A_{t_{i+1}})(AtiAti+1) 的容量改为 pip_ipi

每次操作后输出 A1→BnA_1\rightarrow B_nA1Bn 的最大流

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

  • AAA 中,若割掉了 (Ax→Ax+1)(A_x\rightarrow A_{x+1})(AxAx+1),那么割掉 (Ay→Ay+1) (y>x)(A_y\rightarrow A_{y+1})~(y>x)(AyAy+1) (y>x) 没有意义
  • BBB 中,若割掉了 (Bx→Bx+1)(B_x\rightarrow B_{x+1})(BxBx+1),那么割掉 (By→By+1) (y&lt;x)(B_y\rightarrow B_{y+1})~(y&lt;x)(ByBy+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))(AxAx+1)(ByB(y+1)),那么 (Au→B+v)(u≤x,y&lt;v)(A_u\rightarrow B+v)(u \le x,y &lt; v)(AuB+v)(ux,y<v) 都属于割集

那么在 AAA 中从小到大枚举 AxA_xAx,问题在于在 BBB 中找到最优决策点 yyy 使得剩余代价最小

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

那么可以用线段树维护出对于所有 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.逆向思维?
*/


### 关于 Codeforces 平台上与威尔逊定理相关的编程问题及解决方案 #### 威尔逊定理简介 威尔逊定理是一个重要的数论结论,它表明:如果 $p$ 是一个质数,则 $(p-1)! \equiv -1 \ (\text{mod}\ p)$。换句话说,$(p-1)! + 1$ 可被 $p$ 整除[^5]。这一性质在许多涉及质数检测和大数运算的算法竞赛题目中有广泛的应用。 --- #### 经典题目解析与实现 以下是几道典型的 Codeforces 题目以及它们基于威尔逊定理的解决方法: --- ##### **题目一:Codeforces 1295D** 该题的核心在于判断给定区间内有多少个数满足某种特殊条件,而这些条件可以通过威尔逊定理简化为对阶乘取模的结果分析[^6]。 ###### 解决方案 为了高效地处理大规模数据,我们可以预先计算出所有可能需要用到的阶乘值及其对应的模意义下的结果。具体代码如下所示: ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; ll factorial_mod(ll n, ll p) { if (n >= p) return 0; ll result = 1; for (ll i = 1; i <= n; ++i) { result = (result * i) % p; } return result; } bool is_prime_wilson(ll p) { if (factorial_mod(p - 1, p) != p - 1) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll l, r; cin >> l >> r; int count = 0; for (ll i = max(l, 2LL); i <= r; ++i) { if (is_prime_wilson(i)) { count++; } } cout << count << "\n"; } ``` 这段程序首先定义了一个用于计算阶乘并对指定素数取模的功能函数 `factorial_mod`,接着借助这个工具实现了依据威尔逊定理判定某个自然数是否为素数的逻辑[^6]。 --- ##### **题目二:Prime Number Theorem Application** 虽然这不是一道具体的 Codeforces 题目名称,但它代表了一类常见的挑战场景——即如何利用包括但不限于威尔逊在内的各种经典数学理论去优化复杂度较高的枚举过程[^7]。 ###### 实现细节 当面临需要频繁验证多个候选数字是否属于素数集合的任务时,单纯依靠试除法往往难以达到理想的时间效率标准。此时引入诸如埃拉托斯特尼筛法或者更高级别的线性筛技术固然是一种不错的选择;然而,在某些特定条件下(例如仅需关心少量极大数值的情况),直接调用威尔逊定理反而能带来意想不到的优势。 示例代码片段展示如下: ```python def wilsons_theorem_test(n): from math import factorial if n < 2: return False elif pow(factorial(n - 1), 1, n) == n - 1: return True else: return False # Example Usage print(wilsons_theorem_test(11)) # Output: True ``` 在这里我们采用了 Python 内置库中的高精度整数运算能力来模拟整个流程,尽管如此仍需注意实际比赛中应尽量避免因过度依赖此类特性而导致性能瓶颈的发生[^7]。 --- #### 总结 综上所述,通过对威尔逊定理深入理解并灵活运用到不同类型的算法设计当中,不仅可以帮助参赛者更好地应对那些表面上看似棘手但实际上蕴含深刻规律可循的问题,而且还有助于培养更加严谨缜密的思维习惯! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值