正文
增加 201 行删除 769 行
2,669 行未改动全部展开267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206349 行未改动全部展开355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385315 行未改动2,669 行未改动全部展开用可持久化 Treap 优化一下即可。如果递归会 [爆栈](https://www.luogu.com.cn/record/304098512),因此得用迭代版。### [ZR3682 平行世界](https://www.zhengruioi.com/problem/3682)~~原题是紫,这个题建议评黑。~~难度:8,质量:8.5不难发现哈希记录每个连通分量根节点,如果哈希值相同,那么就会在同一个连通块中。注意到只有最多 2*Q个点能够计入,因此只需记录这些点即可。:::success[code]```cppconst int N=5e5+5;int tim[N];unsigned long long h[N];int n,Q,k,tot=0;ll ans=0;unordered_map<unsigned long long,int> cnt;unordered_map<long long,int> mp;mt19937_64 rng(114514);void MAIN(){cin>>n>>k>>Q;if(k>Q){For(i,1,Q,idx,u,v){cin>>idx>>u>>v;cout<<0<<endl;}return;}const int M=2*Q+5;vector<unsigned long long> w(M);for (auto &x:w) x=rng();vector<int> sz(M,1),faa(M),ta(M),nxt(M),hd(M);function<int(int,int)>id=[&](int i,int v)->int{ll key=1ll*i*(n+1)+v;auto it=mp.find(key);if(it!=mp.end()) return it->second;int cur=++tot;sz[cur]=1;faa[cur]=ta[cur]=hd[cur]=v;nxt[cur]=-1;mp[key]=cur;h[v]+=w[cur];tim[v]++;if(tim[v]==k) ans+=cnt[h[v]]++;return cur;};function<int(int,int)>find_sub1=[&](int i,int x)->int{return x==faa[id(i,x)]?x:faa[id(i,x)]=find_sub1(i,faa[id(i,x)]);};For(i,1,Q,idx,u,v){cin>>idx>>u>>v;int p=find_sub1(idx,u),q=find_sub1(idx,v);if(p!=q){if(sz[id(idx,p)]<sz[id(idx,q)]) swap(p,q);unsigned long long dt=w[id(idx,p)]-w[id(idx,q)];for(int x=hd[id(idx,q)];x!=-1;x=nxt[id(idx,x)]){if(tim[x]==k){ans-=cnt[h[x]]-1;if(cnt[h[x]]==1) cnt.erase(h[x]);else cnt[h[x]]--;}h[x]+=dt;if(tim[x]==k) ans+=cnt[h[x]]++;}nxt[id(idx,ta[id(idx,p)])]=hd[id(idx,q)];ta[id(idx,p)]=ta[id(idx,q)];faa[id(idx,q)]=p;sz[id(idx,p)]+=sz[id(idx,q)];}cout<<ans<<endl;}}```:::### [ZR3684 最小最大](https://www.zhengruioi.com/problem/3684) & [qoj 10076](https://qoj.ac/problem/10076/statement/zh_cn)难度:8.5,质量:8不难发现 $[L,R]$ 应该满足的条件为出现了至少一次 $x$ 与 $y$,并且不能包含大于 $y$ 或者小于 $x$ 的元素。考虑根号分治。如果出现次数少,直接做即可,次数多就要预处理。先用单调栈预处理出左右两边严格大于该值与严格小于该值的位置。当 $x=y$ 时,直接找全为 $x$ 的段即可。当 $x$ 的出现次数很少,枚举 $x$ 的每个出现位置 $p$,强制 $p$ 是子区间中最左的 $x$。这样每个合法子区间被唯一确定一个"最左 $x$"。考虑 $[L,R]$ 的合法条件:不能有小于 $x$ 的元素,即 $lless_p<L\le p\le R<rless_p$;$L$ 到 $p$ 之间无 $x$,即 $L> prev_x$;不能有大于 $y$ 的元素,即二分中的 $firgt(p+1,y)>R,lstgt(p-1,y)<L$;至少包含一个 $y$,若 $prev_y\ge L$,则已包含 $y$,否则必须 $nxt_y\le R$。综上所述 $L\in[l,p],R\in[p,r]$,其中 $l=\max(lless_p+1,prev_x+1,lstgt(p-1,y)+1),r=\min(rless_p-1,firgt(p+1,y)-1)$。对着 $y$ 是否在 $[l,p]$ 中分类讨论。若 $y\in[l,p]$,贡献为 $(prev_y-l+1)\times (r-p+1)$。否则若 $nxt_y\le R$ 贡献为 $(p-l+1)\times (R-nxt_y+1)$。$x$ 出现次数多,$y$ 出现次数少的情况完全对称即可。考虑 $x,y$ 出现次数都很多。尝试预处理。记$$S(x,y)=\#\Big\{\,[l,r]\ \Big|\ \min_{i=l}^{r}a_i=x,\ \ \max_{i=l}^{r}a_i\le y\,\Big\}.$$即:$S(x,y)$ 表示 **最小值恰为 $x$、最大值不超过 $y$** 的子区间个数。那么所求的 "最小值恰为 $x$、最大值恰为 $y$" 的子区间个数为$$\#\Big\{\,[l,r]\ \Big|\ \min_{i=l}^{r}a_i=x,\ \max_{i=l}^{r}a_i=y\,\Big\}= S(x,y)-S(x,y-1).$$固定 $x$,按 $y$ 递增的顺序维护 $S(x,y)$,则可以得到答案差分。对已加入的位置组成的一个连续段 $[L,R]$($\mathrm{len}=R-L+1$),段内 **包含 $x$** 的子区间个数为$$\#\Big\{\,(l,r)\ \Big|\ L\le l\le r\le R,\ [l,r]\text{ 含 }x\,\Big\}=\binom{\mathrm{len}}{1}+\binom{\mathrm{len}}{2}-\mathrm{bad},$$其中$$\mathrm{len}=R-L+1,$$$$\mathrm{bad}=\#\Big\{\,(l,r)\ \Big|\ L\le l\le r\le R,\ [l,r]\text{ 不含 }x\,\Big\}.$$这些子区间里,**不含 $x$** 的正好有 $\mathrm{bad}$ 个,**含 $x$** 的就是剩下的部分。需要维护:| $\mathrm{len}$ | **段长**,即 $R-L+1$。 ||---|---|| $\mathrm{lgap}$ | **段左端连续不含 $x$ 的前缀长度**,即从 $L$ 起、连续不满足 $a_i=x$ 的位置个数。 || $\mathrm{rgap}$ | **段右端连续不含 $x$ 的后缀长度**,即到 $R$ 止、连续不满足 $a_i=x$ 的位置个数。 || $\mathrm{has}$ | **段内是否包含 $x$**,$0$ 表示不含,$1$ 表示至少含一个。 || $\mathrm{bad}$ | **段内不含 $x$ 的子区间数**,即 $\#\{(l,r):L\le l\le r\le R,\ [l,r]\text{ 不含 }x\}$。 |考虑如何合并。设左段为 $A$、右段为 $B$,两段相邻。合并后的段 $AB=A\cup B$:##### 长度$$\mathrm{len}_{AB}=\mathrm{len}_A+\mathrm{len}_B.$$##### 左 gap$$\mathrm{lgap}_{AB}=\begin{cases}\mathrm{lgap}_A, & \mathrm{has}_A=1\ (\text{左段含 }x,\ \text{左 gap 被截断}),\\[2pt]\mathrm{len}_A+\mathrm{lgap}_B, & \mathrm{has}_A=0\ (\text{左段全 gap, 接上右段的左 gap}).\end{cases}$$##### 右 gap$$\mathrm{rgap}_{AB}=\begin{cases}\mathrm{rgap}_B, & \mathrm{has}_B=1,\\[2pt]\mathrm{len}_B+\mathrm{rgap}_A, & \mathrm{has}_B=0.\end{cases}$$##### has$$\mathrm{has}_{AB}=\mathrm{has}_A\ \big|\ \mathrm{has}_B.$$##### bad$$\mathrm{bad}_{AB}=\mathrm{bad}_A+\mathrm{bad}_B-\mathrm{tri}(\mathrm{rgap}_A)-\mathrm{tri}(\mathrm{lgap}_B)+\mathrm{tri}(\mathrm{rgap}_A+\mathrm{lgap}_B).$$:::success[code]```cppconst int N=1e5+5,B=100;ll ans[N],ANS[1005][1005];int mx[20][N],mn[20][N];int a[N],qx[N],qy[N];vector<int> pos[N],val;int lless[N],lgreater[N],rless[N],rgreater[N];int id[N];bool vis[N];stack<int> s;int n,q;int qrymn(int l,int r){int t=__lg(r-l+1);return min(mn[t][l],mn[t][r-(1<<t)+1]);}int qrymx(int l,int r){int t=__lg(r-l+1);return max(mx[t][l],mx[t][r-(1<<t)+1]);}int firgt(int L,int x){int l=L,r=n,res=n+1;while(l<=r){int mid=l+r>>1;if(qrymx(L,mid)>x){res=mid;r=mid-1;}else l=mid+1;}return res;}int firlt(int L,int x){int l=L,r=n,res=n+1;while(l<=r){int mid=l+r>>1;if(qrymn(L,mid)<x){res=mid;r=mid-1;}else l=mid+1;}return res;}int lstgt(int R,int x){int l=1,r=R,res=0;while(l<=r){int mid=l+r>>1;if(qrymx(mid,R)>x){res=mid;l=mid+1;}else r=mid-1;}return res;}int lstlt(int R,int x){int l=1,r=R,res=0;while(l<=r){int mid=l+r>>1;if(qrymn(mid,R)<x){res=mid;l=mid+1;}else r=mid-1;}return res;}ll smallx(int x,int y){ll res=0;Rep(i,0,pos[x].size()){int p=pos[x][i],prevx=i>0?pos[x][i-1]:0;int L=max({lless[p]+1,prevx+1,lstgt(p-1,y)+1}),R=min({rless[p]-1,firgt(p+1,y)-1});if(L>p||R<p) continue;int it=lower_bound(pos[y].begin(),pos[y].end(),p)-pos[y].begin();int prevy=it>0?pos[y][it-1]:0,nxty=it<pos[y].size()?pos[y][it]:n+1;if(prevy>=L) res+=1ll*(prevy-L+1)*(R-p+1);int l2=max(L,prevy+1);if(l2<=p&&nxty<=R) res+=1ll*(p-l2+1)*(R-nxty+1);}return res;}ll smally(int x,int y){ll res=0;Rep(i,0,pos[y].size()){int p=pos[y][i],nxty=(i+1)<pos[y].size()?pos[y][i+1]:n+1;int L=max({lgreater[p]+1,lstlt(p-1,x)+1}),R=min({rgreater[p]-1,nxty-1,firlt(p+1,x)-1});if(L>p||R<p) continue;int it=lower_bound(pos[x].begin(),pos[x].end(),p)-pos[x].begin();int prevx=it>0?pos[x][it-1]:0,nxtx=it<pos[x].size()?pos[x][it]:n+1;if(prevx>=L) res+=1ll*(prevx-L+1)*(R-p+1);int l2=max(L,prevx+1);if(l2<=p&&nxtx<=R) res+=1ll*(p-l2+1)*(R-nxtx+1);}return res;}ll fa[N],len[N],lgap[N],rgap[N],bad[N];bool has[N],ac[N];int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}void unionn(int p,int q,ll &tot){if(p==q) return;tot-=(has[p]?1ll*len[p]*(len[p]+1)/2-bad[p]:0)+(has[q]?1ll*len[q]*(len[q]+1)/2-bad[q]:0);bad[p]=bad[p]+bad[q]-1ll*(rgap[p]+1)*rgap[p]/2-1ll*(lgap[q]+1)*lgap[q]/2+1ll*(rgap[p]+lgap[q]+1)*(rgap[p]+lgap[q])/2;if(!has[p])lgap[p]=len[p]+lgap[q];if(has[q]) rgap[p]=rgap[q];else rgap[p]=len[q]+rgap[p];len[p]=len[p]+len[q];has[p]|=has[q];fa[q]=p;tot+=has[p]?len[p]*(len[p]+1)/2-bad[p]:0;}void acti(int p,bool ha,ll &tot){ac[p]=1;fa[p]=p;len[p]=1,has[p]=ha;if(ha) lgap[p]=rgap[p]=bad[p]=0;else lgap[p]=rgap[p]=bad[p]=1;tot+=has[p]?1ll*len[p]*(len[p]+1)/2-bad[p]:0;if(p>1&&ac[p-1]) unionn(find(p-1),find(p),tot);if(p<n&&ac[p+1]) unionn(find(p),find(p+1),tot);}void MAIN(){cin>>n>>q;For(i,1,n){cin>>a[i];pos[a[i]].pb(i);mx[0][i]=mn[0][i]=a[i];}For(i,1,q) cin>>qx[i]>>qy[i];For(i,1,19)For(j,1,n-(1<<i)+1) mx[i][j]=max(mx[i-1][j],mx[i-1][j+(1<<(i-1))]),mn[i][j]=min(mn[i-1][j],mn[i-1][j+(1<<(i-1))]);For(i,1,n){while(!s.empty()&&a[s.top()]>=a[i]) s.pop();lless[i]=s.empty()?0:s.top();s.push(i);}while(!s.empty()) s.pop();foR(i,n,1){while(!s.empty()&&a[s.top()]>=a[i]) s.pop();rless[i]=s.empty()?n+1:s.top();s.push(i);}while(!s.empty()) s.pop();For(i,1,n){while(!s.empty()&&a[s.top()]<=a[i]) s.pop();lgreater[i]=s.empty()?0:s.top();s.push(i);}while(!s.empty()) s.pop();foR(i,n,1){while(!s.empty()&&a[s.top()]<=a[i]) s.pop();rgreater[i]=s.empty()?n+1:s.top();s.push(i);}//x=yint cnt=1;For(i,1,n){if(a[i]==a[i-1]) cnt++;else cnt=1;ans[a[i]]+=cnt;}fill(id+1,id+n+1,-1);For(x,1,n) if(pos[x].size()>B){val.pb(x),id[x]=val.size()-1;}For(i,1,q) if(qx[i]<qy[i]&&id[qx[i]]!=-1&&id[qy[i]]!=-1) vis[id[qx[i]]]=1;vector<int> v;Rep(i,0,val.size()){if(!vis[i]) continue;v.clear();ll tot=0;int x=val[i];for(auto p:pos[x]){acti(p,1,tot);v.pb(p);}ll prev=tot;For(y,x+1,n){for(int p:pos[y]){acti(p,0,tot);v.pb(p);}if(id[y]!=-1) ANS[i][id[y]]=tot-prev;prev=tot;}for(auto p:v){ac[p]=0;}}For(i,1,q){if(qx[i]==qy[i]) cout<<ans[qx[i]]<<endl;else if(pos[qx[i]].size()<=B) cout<<smallx(qx[i],qy[i])<<endl;else if(pos[qy[i]].size()<=B) cout<<smally(qx[i],qy[i])<<endl;else cout<<ANS[id[qx[i]]][id[qy[i]]]<<endl;}}```:::### [ZR3655 transform](https://www.zhengruioi.com/problem/3655) & [CF1967D Long Way to be Non-decreasing](https://www.luogu.com.cn/problem/CF1967D)难度:8.5,质量:8首先一眼二分答案。建出来内向基环树森林,树上求两点间距离即可,贪心地找即可。:::success[code]```cppll a[N],b[N],c[N];ll dfn[N],lst[N],dep[N],deg[N];ll rid[N],pir[N],rten[N];ll pre[N],sum[N];bool cir[N],vis[N];int n,m,tim,tot;vector<int> e[N];ll ringdis(int a,int b){if(a==b) return 0;int ida=pir[a],idb=pir[b];if(ida<idb) return pre[b]-pre[a];else return sum[rid[a]]-pre[a]+pre[b];}ll dis(int u,int v){if(u==v) return 0;bool c1=cir[u],c2=cir[v];if(!c1&&!c2) return (dfn[v]<=dfn[u]&&lst[v]>=lst[u])?dep[u]-dep[v]:inf;if(!c1){int ent=rten[u];if(rid[ent]!=rid[v]) return inf;return dep[u]+ringdis(ent,v);}if(!c2){return inf;}if(rid[u]!=rid[v]) return inf;return ringdis(u,v);}void dfs(int u,int fa){dfn[u]=++tim;if(u==fa){rten[u]=u;dep[u]=0;}else{rten[u]=rten[b[u]];dep[u]=dep[b[u]]+1;}for(auto v:e[u]) dfs(v,fa);lst[u]=tim;}void bld(){For(i,1,m) deg[b[i]]++;queue<int> q;For(i,1,m) if(!deg[i]) q.push(i);while(!q.empty()){int u=q.front();q.pop();if(--deg[b[u]]==0) q.push(b[u]);}For(i,1,m) if(deg[i]) cir[i]=1;For(i,1,m){if(cir[i]&&!vis[i]){int id=++tot,u=i;vector<int> node;do{node.pb(u);vis[u]=1;rid[u]=id;pir[u]=node.size()-1;u=b[u];}while(u!=i);ll sm=0;for(auto x:node){pre[x]=sm;sm++;}sum[id]=sm;}}For(i,1,m) if(!cir[i]) e[b[i]].pb(i);For(i,1,m) if(cir[i]) dfs(i,i);}bool chk(ll x){int prev=0;For(i,1,n){bool fl=0;For(y,max(prev,1),m){if(dis(a[i],y)<=x){prev=y;fl=1;break;}}if(!fl) return 0;}return 1;}ll gett(){int prev=0;ll mx=0;For(i,1,n){bool fl=0;For(y,max(prev,1),m){if(dis(a[i],y)!=inf){prev=y;mx=max(mx,dis(a[i],y));fl=1;break;}}if(!fl) return -1;}return mx;}void MAIN(){tot=tim=0;cin>>n>>m;For(i,1,m) cir[i]=dfn[i]=lst[i]=vis[i]=deg[i]=0,rid[i]=-1,e[i].clear();For(i,1,n) cin>>a[i];For(i,1,m) cin>>b[i];bld();ll l=0,r=gett();if(r==-1){cout<<r<<endl;return;}ll ans=0;while(l<=r){ll mid=l+r>>1;if(chk(mid)) r=mid-1,ans=mid;else l=mid+1;}cout<<ans<<endl;}```:::### [P17543 [JAG 2026 Summer Camp #2] Deque Bracket Optimization](https://www.luogu.com.cn/problem/P17543) & [ZR3687 括号](https://www.zhengruioi.com/problem/3687)> 给定长度为 $2n$ 的序列 $W$,求所有合法括号序列中,左括号位置对应权值和的最大值。根据括号匹配,第一个位置必须是 `(`,最后一个位置必须是 `)`。内部位置(第 $2$ 到 $n−1$ 位)中,恰好选 $n−1$ 个作为 `(`。在第 $2$ 位到 第 $2i+1$ 个位置,至少要有 $i$ 个括号。假设我们在中间每次看两个位置。每次往大根堆里放两个权值,弹出一个最大的,这个位置就是 `(`。这是静态的做法,但是动态时间复杂度就会变成 $O(Q^2\log Q)$。考虑优化。不难发现我们要维护增量。首先 $1$ 和 $3$ 随便维护。难点在 $2$。我们可以维护两个状态 $F,G$,分别表示从第一个元素开始维护和第二个元素开始维护。做完了。:::success[code]```cppint t,x,y;struct no{priority_queue<PII> out;priority_queue<PII,vector<PII>,greater<PII>> in;PII hd,tl;bool t=1;ll ans=0;void push_front(PII a,PII b){if(t) {hd=a,tl=b,t=0,ans=a.fi;return;}in.push(b);in.push(hd);PII u=in.top();in.pop();out.push(u);ans+=a.fi+b.fi-u.fi;hd=a;}void push_back(PII a,PII b){if(t) {return push_front(a,b);}out.push(tl);out.push(a);PII u=out.top();out.pop();in.push(u);ans+=u.fi;tl=b;}}F,G;void MAIN(){F=no(),G=no();int q;cin>>q;int id=0;while(q--){cin>>t>>x>>y;int idx=++id,idy=++id;if(t==1){if(F.t){F.push_front({y,idy},{x,idx});}else{G.push_front({x,idx},F.hd);F.push_front({y,idy},{x,idx});}}if(t==2){if(F.t){G=no();F.push_front({x,idx},{y,idy});}else{swap(F,G);F.push_front({x,idx},G.hd);F.push_back(G.tl,{y,idy});}}if(t==3){if(F.t){F.push_back({x,idx},{y,idy});}else{G.push_back(F.tl,{x,idx});F.push_back({x,idx},{y,idy});}}cout<<F.ans<<endl;}}```:::### [P7915 [CSP-S 2021] 回文](https://www.luogu.com.cn/problem/P7915)难度:5,质量:6.5349 行未改动全部展开}```:::### [ZR3727 序列](https://www.zhengruioi.com/problem/3727) & [QOJ10170 Data Structures Master](https://qoj.ac/problem/10170)难度:7.5,质量:8我是数据结构低手/ll笛卡尔树暴力跳是显然的。定义 $W_{u}=\sum_{x\in A}\limits\sum_{y\in B}\limits\sum_{z\in C}\limits[x,y,z\in T_u]$,其中 $T_u$ 表 $u$ 的子树。最大值在 $u$ 处取到的三元组数量即为 $W_u-W_{ls}-W_{rs}$,答案即为 $\sum_{u}a_u(W_u-W_{ls}-W_{rs})=\sum_{u}(a_u-a_{fa_u})W(u)$,其中 $a_{fa_{root}}=0$。记 $d_u=a_u-a_{fa_u}$,$X(u)$ 表示子树 $u$ 中有多少个 $A$ 序列元素,$Y(u)$ 表示子树 $u$ 中有多少个 $B$ 序列元素,$Z(u)$ 表示子树 $u$ 中有多少个 $C$ 序列元素。显然 $W_u=X(u)Y(u)Z(u)$,则 $S=\sum_{u}d_uX(u)Y(u)Z(u)$。树剖线段树维护一下即可。写这个题确实可以加深我对DS的理解/qq。:::success[code]```cpp#define mid (l+r>>1)#define ls u<<1#define rs u<<1|1#define segroot int u=1,int l=1,int r=n#define lson ls,l,mid#define rson rs,mid+1,rconst int N=3e5+5;int a[N],l[N],r[N],fa[N],top[N],son[N],sz[N],dfn[N];mint d[N],arr[N];int n,q,rt;vector<int> ch[N];struct no{mint sd,sx,sy,sz,sxy,sxz,syz,sxyz,tagx,tagy,tagz;}t[N<<2];void up(int u){t[u].sd=t[ls].sd+t[rs].sd;t[u].sx=t[ls].sx+t[rs].sx;t[u].sy=t[ls].sy+t[rs].sy;t[u].sz=t[ls].sz+t[rs].sz;t[u].sxy=t[ls].sxy+t[rs].sxy;t[u].syz=t[ls].syz+t[rs].syz;t[u].sxz=t[ls].sxz+t[rs].sxz;t[u].sxyz=t[ls].sxyz+t[rs].sxyz;}void down(int u,mint tagx,mint tagy,mint tagz){auto [oldd,oldx,oldy,oldz,oldxy,oldxz,oldyz,oldxyz,xsy,gxt,xsy1]=t[u];mint nxyz=oldxyz+tagx*oldyz+tagy*oldxz+tagz*oldxy+tagx*tagy*oldz+tagx*tagz*oldy+tagy*tagz*oldx+oldd*tagx*tagy*tagz,nxy=oldxy+oldx*tagy+oldy*tagx+oldd*tagx*tagy,nxz=oldxz+oldx*tagz+oldz*tagx+oldd*tagx*tagz,nyz=oldyz+oldz*tagy+oldy*tagz+oldd*tagz*tagy,nx=oldx+tagx*oldd,ny=oldy+tagy*oldd,nz=oldz+tagz*oldd;t[u]={oldd,nx,ny,nz,nxy,nxz,nyz,nxyz,xsy+tagx,gxt+tagy,xsy1+tagz};}void down(int u){if(t[u].tagx.x||t[u].tagy.x||t[u].tagz.x){down(ls,t[u].tagx,t[u].tagy,t[u].tagz);down(rs,t[u].tagx,t[u].tagy,t[u].tagz);t[u].tagx=0,t[u].tagy=0,t[u].tagz=0;}}void dfs1(int u){sz[u]=1;son[u]=0;for(auto v:ch[u]){dfs1(v);sz[u]+=sz[v];if(!son[u]||sz[v]>sz[son[u]]){son[u]=v;}}}int tim;void dfs2(int u,int h){dfn[u]=++tim;top[u]=h;if(son[u]) dfs2(son[u],h);for(auto v:ch[u]){if(v!=son[u]) dfs2(v,v);}}void bld(segroot){t[u].tagx=t[u].tagy=t[u].tagz=0;if(l==r){t[u].sd=arr[l];t[u].sx=t[u].sy=t[u].sz=t[u].sxy=t[u].sxz=t[u].syz=t[u].sxyz=0;return;}bld(lson),bld(rson);up(u);}void add(int ql,int qr,int tp,segroot){if(ql<=l&&qr>=r){if(tp==0) down(u,1,0,0);else if(tp==1) down(u,0,1,0);else down(u,0,0,1);return;}down(u);if(ql<=mid)add(ql,qr,tp,lson);if(qr>mid)add(ql,qr,tp,rson);up(u);}void pathadd(int u,int tp){while(top[u]!=top[rt]){add(dfn[top[u]],dfn[u],tp);u=fa[top[u]];}add(dfn[rt],dfn[u],tp);}void MAIN(){cin>>n>>q;For(i,1,n) cin>>a[i];stack<int> s;For(i,1,n){int lst=-1;while(!s.empty()&&a[s.top()]<a[i]){lst=s.top();s.pop();}if(!s.empty()){r[s.top()]=i;fa[i]=s.top();}if(lst!=-1){l[i]=lst;fa[lst]=i;}s.push(i);}For(i,1,n) if(fa[i]) ch[fa[i]].pb(i);else rt=i;dfs1(rt);dfs2(rt,rt);For(i,1,n){arr[dfn[i]]=d[i]=a[i]-(fa[i]?a[fa[i]]:0);}bld();while(q--){char c;int x,tp;cin>>c>>x;if(c=='A') tp=0;else if(c=='B') tp=1;else tp=2;pathadd(x,tp);cout<<t[1].sxyz.x<<endl;}}```:::### [P9293 [ROI 2018] Addition without carry](https://www.luogu.com.cn/problem/P9293) & [ZR3732 合唱](https://www.zhengruioi.com/problem/3732)难度:8,质量:8.5第一次场切黑喵。题意即为使得任意一个二进制位最多被一个 $b_i$ 占用,最小化 $\sum_{i}b_i$。对于每个 $a_i$,我们都记一下每一位的下一个 $1$ 的位置。segtree维护一下即可。:::success[code]```const int N=3e5+5;#define ls u<<1#define rs u<<1|1#define mid (l+r>>1)#define lson ls,l,mid#define rson rs,mid+1,r#define segroot int u=1,int l=1,int r=totstring a[N];vector<int> pos[N];int n,mx,pre[N],tot,nxt[N],rk[N],ans[N<<1];ll b[N];struct no{ll mx;int p,cnt;ll tag;};no operator+(const no &x,const no &y){return {max(x.mx,y.mx),x.mx>=y.mx?x.p:y.p,x.cnt+y.cnt,0};}struct segtree{no t[N<<2];void up(int u){t[u]=t[ls]+t[rs];}void down(int u,ll x){t[u].mx+=x,t[u].tag+=x;}void down(int u){if(t[u].tag){down(ls,t[u].tag);down(rs,t[u].tag);t[u].tag=0;}}void bld(segroot){if(l==r){t[u]={-inf,l,0,0};return;}bld(lson),bld(rson);up(u);}void add(int ql,int qr,ll x,int tp,segroot){if(ql>qr) return;if(ql<=l&&qr>=r){return down(u,x),t[u].cnt+=tp,void();}down(u);if(ql<=mid) add(ql,qr,x,tp,lson);if(qr>mid) add(ql,qr,x,tp,rson);up(u);}no qry(int ql,int qr,segroot){if(ql<=l&&qr>=r) return t[u];down(u);no res={-inf,0,0,0};if(ql<=mid) res=res+qry(ql,qr,lson);if(qr>mid) res=res+qry(ql,qr,rson);return res;}}seg;bool solve(int lim,int del){if(del>tot) return 1;auto [mx,p,xsy,gxt]=seg.qry(del,tot);if(mx>lim) return 0;if(mx<=0) return 1;int c=seg.qry(del,p).cnt;seg.add(p+1,tot,-c,0);if(nxt[p]!=inf){seg.add(nxt[p],nxt[p],inf+b[nxt[p]],1);seg.add(nxt[p]+1,tot,1,0);}int nd=mx-c+1;if(solve(nd-1,p+1)){For(i,nd,mx) ans[i]=1;return 1;}if(nxt[p]!=inf){seg.add(nxt[p],nxt[p],-(inf+b[nxt[p]]),-1);seg.add(nxt[p]+1,tot,-1,0);}if(mx+1<=lim){solve(nd,p+1);For(i,nd+1,mx+1) ans[i]=1;return 1;}seg.add(p+1,tot,c,0);return 0;}void MAIN(){cin>>n;For(i,1,n){cin>>a[i];mx=max(mx,(int)a[i].size());pre[i]=inf;reverse(a[i].begin(),a[i].end());foR(j,a[i].size()-1,0){if(a[i][j]=='1') pos[j].pb(i),tot++;}}int id=tot;fill(nxt,nxt+N,inf);Rep(i,0,mx){sort(pos[i].begin(),pos[i].end(),[](int x,int y){return pre[x]>pre[y];});for(auto j:pos[i]){nxt[id]=pre[j];pre[j]=id;b[id]=i+1;rk[j]=id--;}}seg.bld();For(i,1,n){seg.add(rk[i],rk[i],inf+b[rk[i]],1);seg.add(rk[i]+1,tot,1,0);}solve(inf,1);bool fl=0;foR(i,2*N-1,1){if(ans[i]){fl=1;}if(fl) cout<<ans[i];}}```:::### [UOJ CSP Round C.有效验题](https://uoj.ac/contest/115/problem/1123)### [qoj11126 装备组装](https://qoj.ac/contest/2046/problem/11126/statement/zh_cn) &### [DMY P5857](https://newoj.daimayuan.top/p/5857?tid=6ad82cdf6b41fdaadbb70bd0)15 行未改动2,669 行未改动全部展开267026712672267326742675267626772678349 行未改动全部展开30283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328515 行未改动2,669 行未改动全部展开用可持久化 Treap 优化一下即可。如果递归会 [爆栈](https://www.luogu.com.cn/record/304098512),因此得用迭代版。### [P7915 [CSP-S 2021] 回文](https://www.luogu.com.cn/problem/P7915)难度:5,质量:6.5349 行未改动全部展开}```:::### [P17878 中位树 / median](https://www.luogu.com.cn/problem/P17878)难度:6,质量:7天知道我场上为什么在犯糖/ll。首先看到中位数一眼二分。判断 $x$ 做中位数能不能满足,即为使得权值大于或等于 $x$ 的连通块个数严格多于权值小于 $x$ 的个数。考虑判定。考虑树形 dp。对于 $u$ 的子树,删边后会出现两类块。分别为已经封闭的块与 $u$ 所在的未封闭的块。对于每个点,我们记录已封闭块的贡献和与 $u$ 所在块的权值和。记录两个 dp 数组,分别表示连通块个数的奇偶性。朴素 dp 很好写。考虑剪枝,不难发现,因为每个块对最终贡献最多为 $\left\{0,1\right\}$,所以两个封闭块的贡献 $v1,v2$,若 $v1>v2$,则选 $v1$ 一定不劣。若 $v1=v2$,则肯定选权值和更大的。:::success[code]```cppconst int N=1e5+5;vector<int> e[N];struct no{ll sum;int so;};no f1[N],f2[N];ll w[N];int n;void add(no &res,int so,ll sum){if(res.so<so||(so==res.so&&sum>res.sum)){res={sum,so};}}void dfs(int u,int fa,ll x){f1[u]={w[u],0};f2[u]={0,-10000000};for(auto v:e[u]){if(v==fa) continue;dfs(v,u,x);no v1={0,-10000000},v2={0,-10000000};add(v1,f1[u].so+f1[v].so,f1[u].sum+f1[v].sum);add(v2,f1[u].so+f1[v].so+(f1[v].sum>=x?1:-1),f1[u].sum);add(v2,f1[u].so+f2[v].so,f1[u].sum+f2[v].sum);add(v1,f1[u].so+f2[v].so+(f2[v].sum>=x?1:-1),f1[u].sum);add(v1,f2[u].so+f2[v].so,f2[u].sum+f2[v].sum);add(v2,f2[u].so+f2[v].so+(f2[v].sum>=x?1:-1),f2[u].sum);add(v2,f2[u].so+f1[v].so,f2[u].sum+f1[v].sum);add(v1,f2[u].so+f1[v].so+(f1[v].sum>=x?1:-1),f2[u].sum);f1[u]=v1,f2[u]=v2;}}bool chk(ll x){dfs(1,0,x);if(f1[1].so+(f1[1].sum>=x?1:-1)>0) return 1;return 0;}void MAIN(){cin>>n;ll sum=0,sum1=0;For(i,1,n) e[i].clear();For(i,1,n) cin>>w[i],sum+=w[i],sum1+=(w[i]>0?w[i]:0);Rep(i,1,n,u,v){cin>>u>>v;e[u].pb(v),e[v].pb(u);}ll l=sum,r=sum1,ans=sum;while(l<=r){ll mid=l+r>>1;if(chk(mid)) l=mid+1,ans=mid;else r=mid-1;}cout<<ans<<endl;}```:::### [P17879 子序列 / sequence](https://www.luogu.com.cn/problem/P17879)首先有一个关键结论:一个序列是坏的,当且仅当存在一个位置,同时是前缀最大值和后缀最小值。:::info[证明]- 存在坏点 $\Rightarrow$ 序列是坏的。设坏点 $p$,即 $\forall 1\le i\le p,a_i<a_p,\forall p+1\le j\le n,a_j\ge a_p$。任取序列的一个极长不下降子序列 $T$。若 $p\notin T$,则可以把 $p$ 插入 $T$,每个序列都经过 $p$,没有两个不相交的 LNDS。- 不存在坏点 $\Rightarrow$ 序列是好的。令 $P$ 为前缀最大值集合,$S$ 为后缀最小值集合。所以 $P\cap S=\emptyset$,不难发现 $P$ 与 $S$ 均为 LNDS,组成一个好序列,得证。:::不难发现,若 $i$ 为好序列中的元素,则 $i$ 左侧有比它大的值,或者右侧有比它小的值。即 $i$ 一定属于至少一个逆序对。考虑将 $b_i\ge 0$ 的点都选上。若两个非负权位置 $i<j$,有 $a_i>a_j$,他们构成逆序对,$i$ 不是后缀最小值,$j$ 不是前缀最大值。所以二者都不是坏点。记 $B$ 为满足 $b_i\ge 0$ 且 $i$ 属于某个逆序对的位置 $i$。则 $i$ 的某个逆序对伙伴也在 $B$ 中,因此一定可以选 $i$,记贡献为 $sum=\sum_{i\in B} b_i$令 $R=\left\{b_i\ge 0\right\}\setminus B$,所以 $R$ 中的 $a_i$ 单调不降。对于负权点 $j$,通过逆序对可以激活 $R$ 中的一些点,这些点是一个区间。在左侧、在右侧两种情况不会同时发生。因此找到这个区间即可。因此问题转化为 $R$ 中有 $m$ 个点,每个点权值 $b_i\ge 0$,选择每个区间有一个代价 $c$,要最大化收益。$f_x$ 表示只考虑前 $x$ 个位置的最大收益。设 $pre_x=\sum_{j=0}^{x-1} f_x$,则有 $f_{x+1}=\max(f_x,\max_{[l,r]}(pre_{x+1}-c+\max_{i\in[l,r]}({f_i-pre_i}))$用树状数组维护一下逆序前缀最大值即可。:::success[code]```cppconst int N=1e6+5,M=(1<<20)+5;int a[N],b[N];int n;const ll NEG=-(1LL<<60);struct BIT{ll t[N];int sz;void init(int _sz){sz=_sz;For(i,1,sz)t[i]=NEG;}void add(int u,ll x){for(int i=u;i<=sz;i+=i&(-i))t[i]=max(t[i],x);}ll query(int u){ll res=NEG;for(int i=u;i>0;i-=i&(-i))res=max(res,t[i]);return res;}}bt;int preMax[N],sufMin[N],rA[N],head[N],nxt[N],L[N];char isR[N];ll rW[N],cost[N],pref[N],f[N];void MAIN(){cin>>n;Rep(i,0,n)cin>>a[i];Rep(i,0,n)cin>>b[i];Rep(i,0,n)isR[i]=0;preMax[0]=0;Rep(i,0,n){preMax[i+1]=preMax[i];if(b[i]>=0)preMax[i+1]=max(preMax[i+1],a[i]);}sufMin[n]=n+1;foR(i,n-1,0){sufMin[i]=sufMin[i+1];if(b[i]>=0)sufMin[i]=min(sufMin[i],a[i]);}ll base=0;int m=0;Rep(i,0,n){if(b[i]>=0){if(preMax[i]>a[i]||sufMin[i+1]<a[i]){base+=b[i];}else{isR[i]=1;rA[m]=a[i];rW[m]=b[i];++m;}}}if(m==0){cout<<base<<endl;return;}fill(head,head+m,-1);int cntR=0;int tot=0;Rep(i,0,n){if(isR[i]){++cntR;continue;}if(b[i]<0){int ai=a[i];int Lpos=upper_bound(rA,rA+m,ai)-rA;int Rpos=lower_bound(rA,rA+m,ai)-rA-1;int l=-1,r=-1;if(Lpos<cntR){l=Lpos;r=cntR-1;}else if(cntR<=Rpos){l=cntR;r=Rpos;}if(l!=-1){nxt[tot]=head[r];L[tot]=l;cost[tot]=-(ll)b[i];head[r]=tot;++tot;}}}pref[0]=0;Rep(i,0,m){pref[i+1]=pref[i]+rW[i];}bt.init(m);f[0]=0;Rep(pos,0,m){bt.add(m-pos,f[pos]-pref[pos]);f[pos+1]=f[pos];for(int id=head[pos];id!=-1;id=nxt[id]){int l=L[id];ll c=cost[id];ll mx=bt.query(m-l);if(mx>NEG/2){ll cand=pref[pos+1]-c+mx;if(cand>f[pos+1])f[pos+1]=cand;}}}ll ans=base+f[m];cout<<ans<<endl;}```:::### [UOJ CSP Round C.有效验题](https://uoj.ac/contest/115/problem/1123)> 给定一个长度为 $n$ 的序列,每次可以消去 $a_j \equiv(a_i+1)\pmod m$,问有多少个子序列可以被完全消除。不难发现,本题本质上是在图上找不共点的边集匹配问题。有一个环即 $0\rightarrow1\rightarrow2\rightarrow3\rightarrow\dots\rightarrow m-1\rightarrow0$。我们只要按照数值大小,一层一层匹配即可。考虑破环成链,枚举一个断点。假设我们现在处理 $i$ 与 $i+1$ 的匹配。假设 $i$ 出现在原序列中的下标集合为 $P$,$i+1$ 出现在原序列中的下标集合为 $Q$,设我们对 $P_j$ 与 $Q_k$ 进行匹配。那么只要不共顶点,这一层可以选出多条边。上一层 $i-1$ 和 $i$ 消完后,本层 $i$ 才能与 $i+1$ 消去,### [qoj11126 装备组装](https://qoj.ac/contest/2046/problem/11126/statement/zh_cn) &### [DMY P5857](https://newoj.daimayuan.top/p/5857?tid=6ad82cdf6b41fdaadbb70bd0)15 行未改动