版本比较
前缀和与差分
正文
增加 138 行删除 39 行
1234123465687810119101112131415161718 行未改动36373839384041404243424445444546474849505152535441 行未改动96979899951001011029899103104105101106107103108109105106110111112113114115116117118109119120121122123112113124125126127128116129130131132133134135136137138139140141142143144145132133146147148135149150137151152153154155156140157158142159160161145146162163164149165166167168169170171155172173174175176177178179164180181182183184185170186187188189173174190191192193194195196197180198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279# $\color{RED}\Large\textbf{前缀和与差分}$# $一、\large\textbf{前缀和}$ ## $1. 一维前缀和$ $通项公式:sum_n = \sum_{i = 1}^{n} {a_i}$# 一、前缀和 ## 1. 一维前缀和 通项公式:$sum_n = \sum_{i = 1}^{n} {a_i}$ $递推公式:sum_i = sum_{i-1} + a_i$ 递推公式:$sum_i = sum_{i-1} + a_i$
$应用:l到r区间求和(包含端点)$ 应用:$l$到$r$区间求和(包含端点) $sum(l,r)=sum_r-sum_{l-1}$ ### $例题:$[$\textbf{【深进1.例1】求区间和}$](https://www.luogu.com.cn/problem/B3612/) $\large sum(l,r)=sum_r-sum_{l-1}$ __例题__:[**【深进1.例1】求区间和**](https://www.luogu.com.cn/problem/B3612/) **思路** : “区间和”问题是典型的前缀和模板题,预处理出前缀和,以后查询就是$\mathscr{O}(1)$复杂度了 :::success[AC-code] ```cpp#include <bits/stdc++.h>using namespace std;18 行未改动} ``` ::: ## $2. 二维前缀和$ $通项公式:sum_{n,m}=\sum_{i = 1}^{n} {\sum_{j=1}^{m} a_{i,j}}$ 通项公式:$sum_{n,m}=\sum_{i = 1}^{n} {\sum_{j=1}^{m} a_{i,j}}$ $递推公式:sum_{i,j} = sum_{i-1,j} + sum_{i,j-1} - sum_{i-1,j-1} + a_{i,j}$ 递推公式:$sum_{i,j} = sum_{i-1,j} + sum_{i,j-1} - sum_{i-1,j-1} + a_{i,j}$ $应用:(x1,y1)到(x2,y2)区域求和(包含端点)$ 应用:$(x1,y1)$到$(x2,y2)$区域求和(包含端点) $sum([x1,y1],[x2,y2]])=sum_{x2,y2}-sum_{x1,y2}-sum_{x2,y1}+sum_{x1,y1}$ ### $例题:$[$\textbf{[HNOI2003] 激光炸弹}$](https://www.luogu.com.cn/problem/P2280/) $\large sum([x1,y1],[x2,y2]])=sum_{x2,y2}-sum_{x1,y2}-sum_{x2,y1}+sum_{x1,y1}$ **例题**:[**[HNOI2003] 激光炸弹**](https://www.luogu.com.cn/problem/P2280/) **思路**: 先存入目标的位置, 预处理前缀和然后枚举右下端点,得到区间并计算总共有多少个目标,最多的目标即为答案 :::success[AC-code] ```cpp #include <bits/stdc++.h>using namespace std;41 行未改动} ``` ::: # $二、\large\textbf{差分}$ # 二、差分
## $1. 一维差分$ $通项公式:d_i = a_i - a_{i-1}$ ## 1. 一维差分 通项公式:$diff_i = a_i - a_{i-1}$ $性质:\sum_{i=1}^{n} d_i = a_n $ 性质:$\sum_{i=1}^{n} diff_i = a_n $
$应用:l到r区间每个元素加k(包含端点)$ 应用:$l$到$r$区间每个元素加$k$(包含端点) $d_l += k, d_{r+1} -= k$ ### $例题:$[$\textbf{序列操作}$](https://bbcoj.cn/problem/ST02DFS/) $\large diff_l += k, diff_{r+1} -= k$ **例题**:[**区间加常数**](https://www.luogu.com.cn/problem/U567826) ///////////////////////////////////////////////////////////////////////////////////////////////////待更新
:::success[AC-code] ```cpp #include<bits/stdc++.h>#include<bits/stdc++.h>using namespace std;
const int N = 1e3 + 5;int n, m;int a[100005];int d[100005];int a[N];int d[N];
int main() { cin >> n >> m; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) d[i] = a[i] - a[i - 1];
for(int i = 1; i <= m; i++) { int l, r, c; cin >> l >> r >> c; d[l] += c; d[r + 1] -= c; }
for(int i = 1; i <= n; i++) { d[i] += d[i - 1]; cout << d[i] << ' '; } return 0;} ``` ::: ## $2. 二维差分$ $通项公式:d_{i,j} = a_{}i,j - a_{i-1,j} - a_{i,j-1} + a_{i-1,j-1}$ ## 2. 二维差分 通项公式:$diff_{i,j} = a_{i,j} - a_{i-1,j} - a_{i,j-1} + a_{i-1,j-1}$ $性质:\sum_{i = 1}^{n} {\sum_{j=1}^{m} d_{i,j}}=a_{n,m}$ 性质:$\sum_{i = 1}^{n} {\sum_{j=1}^{m} diff_{i,j}}=a_{n,m}$ $应用:(x1,y1)到(x2,y2)区域每个元素加k(包含端点)$ 应用:$(x1,y1)$到$(x2,y2)$区域每个元素加k(包含端点) $\large diff_{x1,y1}+=k,diff_{x1,y2+1}-=k,diff_{x2+1,y1}-=k,diff_{x2+1,y2+1}+=k$
**例题**:[**地毯**](https://www.luogu.com.cn/problem/P3397) $d_{x1,y1}+=k,d_{x1,y2+1}-=k,d_{x2+1,y1}-=k,d_{x2+1,y2+1}+=k$///////////////////////////////////////////////////////////////////////////////////////////////////待更新
### $例题:$[$\textbf{地毯}$](https://bbcoj.cn/problem/ST02TTJ)
:::success[AC-code] ```cpp #include<bits/stdc++.h> ```cpp#include<bits/stdc++.h>using namespace std;
const int N = 5005;const int N = 5e3 + 5;int a[N][N], d[N][N];int n, m;long long ans;
int main() { cin >> n >> m; for(int i = 1; i <= m; i++) { int i1, i2, j1, j2; cin >> i1 >> j1 >> i2 >> j2; d[i1][j1]++; d[i1][j2 + 1]--; d[i2 + 1][j1]--; d[i2 + 1][j2 + 1]++; }
for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; ans += (i + j) ^ d[i][j]; cout << d[i][j] << ' '; } cout << endl; } cout << ans; return 0;} ``` ::: ## 3. 树上差分 应用:$\textbf{树上}s节点到t节点的链上每个元素加k(包含端点)$
# $\color{red}\huge\mathscr{THE}\,\,\,\mathscr{END}$$\large diff_s += k, diff_t += k, diff_{lca(a, b)} -= , diff_{fa_{lca(a, b)}}$
__例题__:[__[USACO15DEC] Max Flow P__](https://www.luogu.com.cn/problem/P3128/)
///////////////////////////////////////////////////////////////////////////////////////////////////待更新
:::success[AC-code]```cpp#include<bits/stdc++.h>using namespace std;
const int N = 5e4 + 5, LOGN = 18;int n, K, ans;int fa[LOGN][N], dep[N], diff[N];int lg[N]; int siz[N];vector<int> v[N];
void getfa(int x, int fat) { fa[0][x] = fat; dep[x] = dep[fat] + 1; for(int k = 1; k <= lg[dep[x]]; k++) { fa[k][x] = fa[k - 1][fa[k - 1][x]]; } for(auto y : v[x]) { if(y != fat) getfa(y, x); }}
int lca(int u, int v) { if(dep[u] < dep[v]) swap(u, v); for(int k = lg[dep[u]]; k >= 0; k--) { if(dep[fa[k][u]] >= dep[v]) u = fa[k][u]; } if(u == v) return u; for(int k = lg[n]; k >= 0; k--) { if(fa[k][u] != fa[k][v]) { u = fa[k][u]; v = fa[k][v]; } } return fa[0][u];}
void dfs(int x, int fat) { siz[x] = diff[x]; for(auto y : v[x]){ if(y == fat) continue; dfs(y, x); siz[x] += siz[y]; } ans = max(ans, siz[x]); return;}
int main() { cin >> n >> K; for(int i = 1; i < n; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1; getfa(1, 0); while(K--) { int s, t; cin >> s >> t; int LCA = lca(s, t); diff[s]++,diff[t]++,diff[LCA]--,diff[fa[0][LCA]]--; } dfs(1, 0); cout << ans; return 0;}```::: # The End