正文
增加 485 行删除 11 行
12345627 行未改动34353637383940414243444546474849505152535455565758596061### [P3452](https://www.luogu.com.cn/problem/P3452)很容易观察到是求反图的联通块个数、大小。下面讲接下来怎么做。反图其实相当于是,每个点都会向 $d_i +1$ 个区间连边($d$ 度数),考虑**建线段树**,每个点存的是这个区间的点都被连上的另一个点。27 行未改动add(idl); add(idr);}```### [P5994](https://www.luogu.com.cn/problem/P5994)确定一个杯子下面的情况,相当于知道 $s_i - s_{i-1}$ 的值,所以花 $c_{ij}$ 可以知道 $s_{i-1},s_j$ 关系,那么连边,最后使得 $s_i$ 连通即可,相当于**最小生成树**。类似的有 [AT_abc238_e](https://www.luogu.com.cn/problem/AT_abc238_e)。### [P10532](https://www.luogu.com.cn/problem/P10532)诈骗题,,其实就是数了 $(i,j)$ 使得 $1 \le i,j \le n \land i,j \in\mathbb{Z}$,输出 $n^2$。### [P2898](https://www.luogu.com.cn/problem/P2898)讨论下不合法情况:1. $x$ 交集为空2. $x$ 交集被 所有比她大的数的区间 的并集覆盖发现二分不合法处,线段树维护就可以了。### [P15251](https://www.luogu.com.cn/problem/P15251)转化一下,令 $p_i = \sum_{j=1}^{pr_j \le 100}2^{j-1}[pr_j^2 | a_i]$,即把每个数的平方质因子压位标记为 $1$,那么 $\gcd(a_i,a_j)\ \text{is free} \Leftrightarrow p_i\ \& \ p_j =0$。根据值域 $10^4$,只有 $2 \sim 100$ 中的 $25$ 个质数可能成为平方质因子,并且一个数最多只有 $3$ 个平方质因子,并且拥有平方质因子的不同情况只有 $60$ 种。正难则反!考虑算 $[l,r]$ 中 $a_i \ \& \ a_j >0$ 的对数。根据那个什么什么图,答案就是 至少有一位均为 $1$ 的对数之和 减 至少有两位均为 $1$ 的对数之和 加 有三位均为 $1$ 的对数之和,这个对于每种 拥有平方质因子的不同情况 分别开一个树状数组就好了。所以做完了,时间复杂度 $O(nk \log n)$,空间复杂度 $O(nk)$,其中 $k = \sum_{i=2}^{\lfloor\sqrt{V}\rfloor}[\mu(i) \ne 0]$,$V = 10^4$ 时 $k=60$。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732827 行未改动356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535施工中!/fendou## 数学+ [P10532](https://www.luogu.com.cn/problem/P10532):::info[sol]诈骗题,,其实就是数了 $(i,j)$ 使得 $1 \le i,j \le n \land i,j \in\mathbb{Z}$,输出 $n^2$。:::+ [P13635](https://www.luogu.com.cn/problem/P13635):::info[sol]注意到 $\gcd(a,b)=\gcd(a,b-a)$。所以是对称的。做一做就好了。:::+ [P15251](https://www.luogu.com.cn/problem/P15251):::info[sol]转化一下,令 $p_i = \sum_{j=1}^{pr_j \le 100}2^{j-1}[pr_j^2 | a_i]$,即把每个数的平方质因子压位标记为 $1$,那么 $\gcd(a_i,a_j)\ \text{is free} \Leftrightarrow p_i\ \& \ p_j =0$。根据值域 $10^4$,只有 $2 \sim 100$ 中的 $25$ 个质数可能成为平方质因子,并且一个数最多只有 $3$ 个平方质因子,并且拥有平方质因子的不同情况只有 $60$ 种。正难则反!考虑算 $[l,r]$ 中 $a_i \ \& \ a_j >0$ 的对数。根据那个什么什么图,答案就是 至少有一位均为 $1$ 的对数之和 减 至少有两位均为 $1$ 的对数之和 加 有三位均为 $1$ 的对数之和,这个对于每种 拥有平方质因子的不同情况 分别开一个树状数组就好了。所以做完了,时间复杂度 $O(nk \log n)$,空间复杂度 $O(nk)$,其中 $k = \sum_{i=2}^{\lfloor\sqrt{V}\rfloor}[\mu(i) \ne 0]$,$V = 10^4$ 时 $k=60$。:::+ [P3327](https://www.luogu.com.cn/problem/P3327)待补。+ [P6810](https://www.luogu.com.cn/problem/P6810)待补。+ [P16962](https://www.luogu.com.cn/problem/P16962)待补。+ [P5179](https://www.luogu.com.cn/problem/P5179)待补。+ [P6624](https://www.luogu.com.cn/problem/P6624)待补。+ [P14582](https://www.luogu.com.cn/problem/P14582)待补。+ [P11918](https://www.luogu.com.cn/problem/P11918):::info[sol]首先按 $p_i$ 排序,选的一定是一段前缀作答。设 $dp_{i,j}$ 为前 $i$ 题得 $j$ 分的概率,暴力转移,$<\varepsilon$ 的项直接舍去,精细实现即可通过。证明的话…这就是为什么分在数论里而且是黑了。:::+ [P9401](https://www.luogu.com.cn/problem/P9401)待补。+ [P16368](https://www.luogu.com.cn/problem/P16368)待补。+ [P15632](https://www.luogu.com.cn/problem/P15632)待补。+ [P5655](https://www.luogu.com.cn/problem/P5655):::info[sol]有一个[博客](https://www.cnblogs.com/Cry-For-theMoon/p/18426320)。:::## 贪心+ [P8775](https://www.luogu.com.cn/problem/P8775)待补。+ [P10238](https://www.luogu.com.cn/problem/P10238)待补。+ [P2102](https://www.luogu.com.cn/problem/P2102)待补。+ [P7974](https://www.luogu.com.cn/problem/P7974)待补。+ [P14835](https://www.luogu.com.cn/problem/P14835)待补。+ [P14015](https://www.luogu.com.cn/problem/P14015):::info[sol]有点强。突破口:把奇数位不是 `2` 的**反转**(`^=1`),即可把规则改为“每次可删掉相邻的不同数,`2` 是什么怎么删都可以”。这样就可以做了,怎么回事呢。最后剩下的一定是一坨**相同**的东西。分讨 $|t_0 - t_1|$ 和 $t_2$ 的关系即可。```cout<<(abs(p[0]-p[1])>p[2]?abs(p[0]-p[1])-p[2]:(abs(p[0]-p[1])+p[2])&1)<<"\n";```:::+ [P10731](https://www.luogu.com.cn/problem/P10731)待补。+ [AT_abc457_g](https://www.luogu.com.cn/problem/AT_abc457_g)待补。+ [CF1260E](https://www.luogu.com.cn/problem/CF1260E)待补。+ [P12504](https://www.luogu.com.cn/problem/P12504)待补。### 反悔贪心+ [P16279](https://www.luogu.com.cn/problem/P16279)待补。+ [P3045](https://www.luogu.com.cn/problem/P3045)待补。+ [CF730I](https://www.luogu.com.cn/problem/CF730I)/[AT_agc018_c](https://www.luogu.com.cn/problem/AT_agc018_c)待补。放个代码。:::info[代码]```cpp#include<bits/stdc++.h>#define i128 __int128#define ll long long#define ull unsigned long long#define db double#define ldb long double#define Pii pair<int,int>#define fi first#define se second#define f(x,y) fixed<<setprecision(y)<<x#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,rp,stdin),p1==p2)?EOF:*p1++)using namespace std;const int N=3e4+10;const int inf=1.01e9;const int rp=1e6+10;int n,x,y,fr[N],a[N],b[N],p[N]; ll ans;priority_queue<Pii> q1,q2,q3;char buf[rp],*p1=buf,*p2=buf;inline int read(){int x=0,f=1; char c=0;while(!isdigit(c)) {if(c=='-') f=-1; c=gc();}while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=gc();return x*f;}inline char read_ch(){char c=0;while((!isalpha(c))&&(!isdigit(c))) c=gc();return c;}inline int ge(priority_queue<Pii> &q){return q.empty()?-inf:q.top().fi;}signed main(){cin.tie(0)->sync_with_stdio(0);n=read(); x=read(); y=read(); int u,v,w;for(int i=1;i<=n;++i){u=read(); v=read(); w=read();ans+=w; a[i]=u-w; b[i]=v-w; p[i]=i;}sort(p+1,p+n+1,[](int q,int w){return a[q]>a[w];});for(int i=1;i<=x;++i){fr[p[i]]=1; ans+=a[p[i]];q1.push({b[p[i]]-a[p[i]],p[i]});}for(int i=x+1;i<=n;++i)q2.push({a[p[i]],p[i]}),q3.push({b[p[i]],p[i]});while(y--){while((!q1.empty())&&fr[q1.top().se]!=1) q1.pop();while((!q2.empty())&&fr[q2.top().se]) q2.pop();while((!q3.empty())&&fr[q3.top().se]) q3.pop();int v1=ge(q1)+ge(q2),v2=ge(q3); ans+=max(v1,v2);if(v1>v2){fr[q1.top().se]=2; fr[q2.top().se]=1;q1.push({b[q2.top().se]-a[q2.top().se],q2.top().se});}else fr[q3.top().se]=2;}cout<<ans;return 0;}```:::## 构造+ [P3540](https://www.luogu.com.cn/problem/P3540):::info[sol][here](https://www.luogu.com.cn/article/kgezpl3y).:::+ [P17338](https://www.luogu.com.cn/problem/P17338)待补。+ [P10297](https://www.luogu.com.cn/problem/P10297)待补。+ [P10644](https://www.luogu.com.cn/problem/P10644)待补。+ [CF1198C](https://www.luogu.com.cn/problem/CF1198C)待补。+ [P13695](https://www.luogu.com.cn/problem/P13695)待补。+ [P16902](https://www.luogu.com.cn/problem/P16902)待补。+ [P14662](https://www.luogu.com.cn/problem/P14662)待补。+ [CF1515F](https://www.luogu.com.cn/problem/CF1515F)待补。+ [P17333](https://www.luogu.com.cn/problem/P17333)待补。+ [P14942](https://www.luogu.com.cn/problem/P14942)待补。+ [P9731](https://www.luogu.com.cn/problem/P9731)待补。+ [P16325](https://www.luogu.com.cn/problem/P16325)待补。+ [P12336](https://www.luogu.com.cn/problem/P12336)待补。## 图论+ [P2582](https://www.luogu.com.cn/problem/P2582)待补。+ [CF888G](https://www.luogu.com.cn/problem/CF888G)待补。+ [P7984](https://www.luogu.com.cn/problem/P7984)待补。+ [P4366](https://www.luogu.com.cn/problem/P4366)待补。### 反图+ [P3452](https://www.luogu.com.cn/problem/P3452):::info[sol]很容易观察到是求反图的联通块个数、大小。下面讲接下来怎么做。反图其实相当于是,每个点都会向 $d_i +1$ 个区间连边($d$ 度数),考虑**建线段树**,每个点存的是这个区间的点都被连上的另一个点。27 行未改动add(idl); add(idr);}```:::+ [P1285](https://www.luogu.com.cn/problem/P1285)待补。### 图论建模+ [P5994](https://www.luogu.com.cn/problem/P5994):::info[sol]确定一个杯子下面的情况,相当于知道 $s_i - s_{i-1}$ 的值,所以花 $c_{ij}$ 可以知道 $s_{i-1},s_j$ 关系,那么连边,最后使得 $s_i$ 连通即可,相当于**最小生成树**。类似的有 [AT_abc238_e](https://www.luogu.com.cn/problem/AT_abc238_e)。:::+ [CF547D](https://www.luogu.com.cn/problem/CF547D)待补。+ [P16443](https://www.luogu.com.cn/problem/P16443)待补。+ [P16458](https://www.luogu.com.cn/problem/P16458)待补。+ [P9140](https://www.luogu.com.cn/problem/P9140)待补。+ [AT_agc056_c](https://www.luogu.com.cn/problem/AT_agc056_c)待补。## 杂题我也不知道塞在哪儿。+ [P2898](https://www.luogu.com.cn/problem/P2898):::info[sol]讨论下不合法情况:1. $x$ 交集为空;2. $x$ 交集被 所有比她大的数的区间 的并集覆盖。发现二分不合法处,线段树维护就可以了。:::+ [P10059](https://www.luogu.com.cn/problem/P10059):::info[sol]注意到是极差,有单调性:区间长度越大,极差越大。因此 $L=n-k+1$ 时必能取到最大的 $X$。算出来,用 $X$ 再二分 $L$ 即可。:::+ [P12012](https://www.luogu.com.cn/problem/P12012):::info[sol]无交集是不必要的,因为可以抵消,保证两集合不同即可。那么,选集合有 $2^{len}$ 种方案,集合的总贡献有 $vlen$ 种,当 $2^{len}>vlen$ 即 $len>13$ 时一定有解。$len \le 13$ 跑折半搜索 / `bitset` 优化背包即可。:::+ [P10440](https://www.luogu.com.cn/problem/P10440):::info[sol]黑降绿是什么题。考虑枚举 $i$,询问 $x=\text{qry}(1,i)$。再枚举 $j$,询问 $y=\text{qry}(x,j)$(即询问邻域),当 $y$ 作为过 $x$ 时,说明其在 $x$ 邻域中离 $1$ 最近,即 $x$ 的父亲,退出即可。否则就确定了 $y$ 的父亲,继续询问。那么对除 $1$ 外每个点均摊询问 $2$ 次,共 $2n-2$ 次。:::+ [P16970](https://www.luogu.com.cn/problem/P16970):::info[sol]丢到 k 叉 trie 上,就是向父亲或 0 儿子走代价为 1。由于其树高 log,所以快速维护查询的重心状物是可行的。代码据说不好写,所以没写。/咦:::+ [P3295](https://www.luogu.com.cn/problem/P3295):::info[sol]考虑这个并查集维护 `fa[x]` 的实际上是类似 $\max$ 而非 $+$ 的信息。所以使用 st 表,先在每层连上,再下传。:::+ [P6087](https://www.luogu.com.cn/problem/P6087):::info[sol]先不考虑 $j-i+1 \ge L$,则**极值一定在区间边界**。那么那些 $j-i+1 < L$ 的区间,只放宽到 $j-i+1=L$ 才最优。单调队列算这部分。否则式子变成 $\frac{|a_i-a_j|}{j-i+K}$,二分 $\frac{|a_i-a_j|}{j-i+K} \ge \text{mid}$,并分讨 $a_i$ 与 $a_j$ 关系拆绝对值。比如令 $a_j \ge a_i$,则 $(a_j - \text{mid}j)-(a_i - \text{mid}i) \ge \text{mid}K$。也是单调队列。:::+ [P16601](https://www.luogu.com.cn/problem/P16601):::info[sol]乐,咋瞪这么久。矩阵快速幂的本质,是将原来的 $1 \times n$ 矩阵 $A$ 与 $\log$ 个 $n \times n$ 矩阵 $B^{2^i}$ 乘起来。平时只求一遍,预处理不优,但此处应当预处理这 $\log$ 个 $B^{2^i}$,询问时用 $A$ 乘 $B^{2^i}$,每乘一遍是 $O(n^2)$ 的。因此就成了 $O(V^3\log t+ mV^2\log t)$。:::+ [P7913](https://www.luogu.com.cn/problem/P7913):::info[sol][here.](https://www.luogu.com.cn/article/h8r7mrvj):::+ [P15793](https://www.luogu.com.cn/problem/P15793):::info[sol]要使第 $i$ 个花灵不被接触,那么就要增加 $\text{\textcolor{#66CCFF}{1}}$ 接触到其它花灵的次数。所以 $i$ 放到最后肯定更优。邻项交换,发现前面的顺序不会改变 $\text{\textcolor{#66CCFF}{1}}$ 每种接触到其它花灵次数的期望。所以先转移完 $n$ 个,每次要算 $i$ 时再退背包退掉 $i$。:::+ [P8108](https://www.luogu.com.cn/problem/P8108):::info[sol]考虑相邻两列,可以选出一个公共子序列使得这部分两边一次消除。那么总的答案就是 $n^2$ 减相邻两列最长公共子序列长度之和。由于数据随机生成枚举配对再树状数组是对的。:::+ [P17024](https://www.luogu.com.cn/problem/P17024)待补。+ [()P9035](http://10.10.255.150/p/P9035)待补。