版本比较
莫队(存在讲解能力堪忧的讲解视频)
标题
增加 1 行删除 1 行
11莫队啊,你崛起罢!莫队(存在讲解能力堪忧的讲解视频)正文
增加 1137 行删除 10 行
12345678391011121314151644 行未改动61626364576566596768697071462 行未改动全部展开5345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136141,282 行未改动全部展开189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229975 行未改动237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125321911253325342535253625372538253925402541254219212543254425452546192525472548192725492550255125522553217 行未改动全部展开2771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056362 行未改动全部展开341934203421342225233423342434253426342760 行未改动348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724290 行未改动全部展开4015401640174018289240194020402140224023159 行未改动全部展开4183418441854186306041874188418941904191615 行未改动全部展开[题单](https://www.luogu.com.cn/training/1012230)
[讲解视频](https://www.bilibili.com/video/BV1CDM96jEup)
---
是吉祥物讲课用的东西,所以只有一些很简单的例题,之后可能会在二轮复习时重构,并加入一些更加有难度的题目。
这里目前有 $34$ 道题,按照 lsy 老师的话来说,用来见多识广。Upd:重构了,加了一些题目,而且吉祥物马上就要退役了,应该也没什么时间再加题了,顺便就发出来吧。
这里目前有 $42$ 道题,按照 lsy 老师的话来说,用来见多识广。
前面有很多比较简单的普通莫队题可以用来练手,后面的题目会稍微有一点思维难度。
带修莫队、回滚莫队、树上莫队、二次离线莫队题目很少,看完之后还可以自己进行额外的练习。
44 行未改动- 若为偶数,将 $r$ 从小到大排序。
:::
假设我们有 $B$ 块。假设我们块长为 $B$。
考虑两个指针的移动。考虑两个指针的移动,以下考虑的是均摊情况。
- 左指针每一次询问最多移动 $B$ 次。
- 右指针每一块内最多移动 $n$ 次。462 行未改动全部展开}```:::
---
这题还有另外一个做法。
考虑对值域分块,记录出现次数。
可以把时间复杂度平衡到 $\Theta(n \sqrt n)$。
:::success[代码]```cpp#include<bits/stdc++.h>using namespace std;
const int N=2e5+15,K=505;int a[N],belong[N],ans[N];struct edge{int l,r,id;}b[N];struct Val_Block{//值域分块,支持插入、删除、查询第一个没有出现的值 int belong[N],cnt[N],sum[N],st[K],ed[K],block,num;
inline void init(int n){ block=(int)sqrt(n);num=n/block; for(int i=0;i<=n;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } st[0]=0;//注意第 0 块的信息是从 0 开始的 }
inline void add(int x){if(cnt[x]==0) sum[belong[x]]++;cnt[x]++;} inline void del(int x){cnt[x]--;if(cnt[x]==0) sum[belong[x]]--;}
inline int ask(){ for(int i=0;i<=num;i++) if(sum[i]!=ed[i]-st[i]+1) for(int j=st[i];j<=ed[i];j++) if(cnt[j]==0) return j; }}Block;
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (edge x,edge y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return (belong[x.l]?(x.r<y.r):(x.r>y.r));}
int main(){ int n,m;cin>>n>>m;int block=(int)sqrt(n);Block.init(200001);//加 1 保证可以找到答案 for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) belong[i]=i/block; for(int i=1;i<=m;i++) b[i]={read(),read(),i}; sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,Block.add(a[l]); while(r<b[i].r) r++,Block.add(a[r]); while(l<b[i].l) Block.del(a[l]),l++; while(r>b[i].r) Block.del(a[r]),r--; ans[b[i].id]=Block.ask(); } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}```:::
但是奇特的是这个 $n \sqrt n$ 跑不过 $\frac{n^2}{w}$,真就 `bitset` 王朝了。
### [[CQOI2018] 异或序列](https://www.luogu.com.cn/problem/P4462)
$\oplus$ 是支持前缀和的,对于 $a_x \oplus a_{x+1} \oplus ... \oplus a_y=k$,在处理 $\oplus$ 的前缀和后,等价于 $\textrm{sum}_{x-1} \oplus \textrm{sum}_y = k$。
1,282 行未改动全部展开}```:::
### [[AHOI2013] 作业](https://www.luogu.com.cn/problem/P4396)
666,还有第三关。
对值域分块,维护数的出现次数和数的种类数即可。
:::success[代码]```cpp//莫队小姐你带我走吧!!!#include<bits/stdc++.h>using namespace std;
const int N=1e5+5;struct edge{int l,r,x,y,id;}b[N];int a[N],belong[N],ans1[N],ans2[N];struct Val_Block{//值域分块 static const int K=355; int belong[N],st[K],ed[K]; int cnt1[N],sum1[N],cnt2[N],sum2[N]; /* cnt1 和 sum1 处理数的个数 cnt2 和 sum2 处理数的种类数 st 和 ed 记录每一块的起点和终点 */
inline void init(){ int block=(int)sqrt(100000); for(int i=1;i<=100000;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } }
inline void add(int x){ cnt1[x]++;sum1[belong[x]]++; if(cnt1[x]==1) cnt2[x]++,sum2[belong[x]]++; }
inline void del(int x){ if(cnt1[x]==1) cnt2[x]--,sum2[belong[x]]--; cnt1[x]--;sum1[belong[x]]--; }
inline int ask1(int x,int y){//查询 [x,y] 的数的数量 if(belong[x]==belong[y]){ int ans=0; for(int i=x;i<=y;i++) ans+=cnt1[i]; return ans; } int ans=0; for(int i=x;i<=ed[belong[x]];i++) ans+=cnt1[i]; for(int i=belong[x]+1;i<=belong[y]-1;i++) ans+=sum1[i]; for(int i=st[belong[y]];i<=y;i++) ans+=cnt1[i]; return ans; }
inline int ask2(int x,int y){//查询 [x,y] 的数的种类数 if(belong[x]==belong[y]){ int ans=0; for(int i=x;i<=y;i++) ans+=cnt2[i]; return ans; } int ans=0; for(int i=x;i<=ed[belong[x]];i++) ans+=cnt2[i]; for(int i=belong[x]+1;i<=belong[y]-1;i++) ans+=sum2[i]; for(int i=st[belong[y]];i<=y;i++) ans+=cnt2[i]; return ans; }}Block;
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (edge x,edge y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return ((belong[x.r])?(x.r<y.r):(x.r>y.r));}
int main(){ int n,m;cin>>n>>m;int block=(int)sqrt(n);Block.init(); for(int i=1;i<=n;i++) a[i]=read(),belong[i]=i/block; for(int i=1;i<=m;i++) b[i]={read(),read(),read(),read(),i}; sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,Block.add(a[l]); while(r<b[i].r) r++,Block.add(a[r]); while(l<b[i].l) Block.del(a[l]),l++; while(r>b[i].r) Block.del(a[r]),r--; ans1[b[i].id]=Block.ask1(b[i].x,b[i].y); ans2[b[i].id]=Block.ask2(b[i].x,b[i].y); } for(int i=1;i<=m;i++) cout<<ans1[i]<<" "<<ans2[i]<<"\n"; return 0;}```:::
### [Tree and Queries](https://www.luogu.com.cn/problem/CF375D)
666,还有第四关。
查询子树,不强制在线,考虑使用 dfn 序拍平后直接做莫队。
这里查询的是出现次数,我们直接对出现次数分块即可。
:::success[代码]```cpp#include<bits/stdc++.h>using namespace std;
const int N=1e5+5,M=2e5+5;int a[N],dfn[N],sz[N],idx;int belong[N],ans[N],A[N];int ver[M],ne[M],head[N],tot;struct node{int l,r,k,id;}b[N];struct item{ static const int M=1005; int belong[N],st[M],ed[M]; int a[N],cnt[N],sum[M],block,num;
inline void init(int n=100000){ block=(int)sqrt(n);num=n/block; for(int i=1;i<=n;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } } inline void add(int x){ if(cnt[x]!=0) sum[belong[cnt[x]]]--,a[cnt[x]]--; cnt[x]++; sum[belong[cnt[x]]]++,a[cnt[x]]++; }
inline void del(int x){ sum[belong[cnt[x]]]--,a[cnt[x]]--; cnt[x]--; if(cnt[x]!=0) sum[belong[cnt[x]]]++,a[cnt[x]]++; }
inline int ask(int x){ int ans=0,id=-1; for(int i=0;i<=num;i++){ if(ed[i]<=x) ans+=sum[i]; else{id=i;break;} } if(id==-1) return ans; for(int i=st[id];i<=x;i++) ans+=a[i]; return ans; }}Block;/*a 是原本的颜色,A 是按照 dfn 的颜色dfn 和 sz 用来处理子树信息查询*/
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline void add(int x,int y){ ver[++tot]=y; ne[tot]=head[x],head[x]=tot;}
inline void dfs(int x,int fa){ dfn[x]=++idx;sz[x]=1;A[idx]=a[x]; for(int i=head[x];i;i=ne[i]){int y=ver[i];if(y==fa) continue;dfs(y,x);sz[x]+=sz[y];}}
inline bool operator < (node x,node y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return (belong[x.l]&1)?(x.r<y.r):(x.r>y.r);}
int main(){ int n,m;cin>>n>>m;int block=(int)sqrt(n); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) belong[i]=i/block; for(int i=1;i<n;i++){ int x=read(),y=read(); add(x,y);add(y,x); } dfs(1,0);Block.init(); for(int i=1;i<=m;i++){ int x=read(),k=read(); b[i]={dfn[x],dfn[x]+sz[x]-1,k,i}; } sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,Block.add(A[l]); while(r<b[i].r) r++,Block.add(A[r]); while(l<b[i].l) Block.del(A[l]),l++; while(r>b[i].r) Block.del(A[r]),r--; ans[b[i].id]=Block.ask(100000)-Block.ask(b[i].k-1); } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}```:::
### [Cut and Stick](https://www.luogu.com.cn/problem/CF1514D)
666,还有第五关。
我不知道啊,看到出现次数我就来了。
首先,不存在绝对众数的话答案一定为 $1$,这十分显然。
---
反之,我们尝试去构造答案。
显然,一个绝对众数带一个别的数一定是不劣的。
那么,就让剩下的部分独立一组就好了。
即答案为绝对众数数量减去其他数数量。
---
考虑这需要我们实现什么功能:查询一段区间内是否有数,如果有,输出其值,保证只有一个。
直接莫队套分块,对出现次数分块即可。
:::success[代码]```cpp#include<bits/stdc++.h>using namespace std;
const int N=3e5+5;int belong[N],a[N],ans[N];struct node{int l,r,id;}b[N];struct item{//对出现次数分块 static const int M=1000; int belong[N],st[M],ed[M]; int a[N],cnt[N],sum[M],block,num;
inline void init(int n){ block=(int)sqrt(n);num=n/block; for(int i=1;i<=n;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } }
inline void add(int x){ if(cnt[x]!=0) sum[belong[cnt[x]]]--,a[cnt[x]]--; cnt[x]++;a[cnt[x]]++;sum[belong[cnt[x]]]++; }
inline void del(int x){ sum[belong[cnt[x]]]--;a[cnt[x]]--,cnt[x]--; if(cnt[x]!=0) sum[belong[cnt[x]]]++,a[cnt[x]]++; }
inline int ask(int x){//查询 x 到 n 是否有值 int id=-1; for(int i=x;i<=ed[belong[x]];i++) if(a[i]) return i;//散块 for(int i=belong[x]+1;i<=num;i++) if(sum[i]){id=i;break;}//整块 if(id==-1) return 0;//不存在 for(int i=st[id];i<=ed[id];i++) if(a[i]) return i;//遍历存在答案的整块 }}Block;
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (node x,node y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return ((belong[x.l]&1)?(x.r<y.r):(x.r>y.r));}
signed main(){ int n,m;cin>>n>>m; int block=(int)sqrt(n);Block.init(n); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) belong[i]=i/block; for(int i=1;i<=m;i++) b[i]={read(),read(),i}; sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,Block.add(a[l]); while(r<b[i].r) r++,Block.add(a[r]); while(l<b[i].l) Block.del(a[l]),l++; while(r>b[i].r) Block.del(a[r]),r--; int len=b[i].r-b[i].l+1; int now=Block.ask(len/2+(len%2!=0)+1); if(now==0) ans[b[i].id]=1;//不存在绝对众数 else ans[b[i].id]=now-(len-now);//绝对众数数量-其他数数量 } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}```:::
### [One Occurrence](https://www.luogu.com.cn/problem/CF1000F)
区间问题,没有强制在线,考虑莫队。
显然可以使用 STL 配合莫队做到理论 $O(n \sqrt n)$,但是这样常数太大,我们需要压缩插入和删除的常数。
这也可以算作是平衡复杂度,直接上分块。因为这里是出现次数,我们使用值域分块会更有前途。
直接记录每一块里是否有出现次数为 $1$ 的即可,可以将时间复杂度平衡到小常数的 $O(n \sqrt n+m \sqrt n)$,这里视 $n,m,V$ 同阶。
:::success[代码]```cpp#include<bits/stdc++.h>using namespace std;
const int N=5e5+5;int a[N],belong[N],ans[N];struct node{int l,r,id;}b[N];struct item{ static const int M=1005; int a[N],sum[M],block,num; int belong[N],st[M],ed[M]; /*a 是每一个数的出现次数,sum 是每一块有几个 1*/
inline void init(int n=500000){ block=(int)sqrt(n);num=n/block; for(int i=1;i<=n;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } }
inline void add(int x){ if(a[x]==1) sum[belong[x]]--; a[x]++; if(a[x]==1) sum[belong[x]]++; }
inline void del(int x){ if(a[x]==1) sum[belong[x]]--; a[x]--; if(a[x]==1) sum[belong[x]]++; }
inline int ask(){ for(int i=0;i<=num;i++){ if(sum[i]==0) continue;//没有 1,不处理 for(int j=st[i];j<=ed[i];j++) if(a[j]==1) return j;//存在 1,将其找到 } return 0;//不存在任何这样的整数 }}Block;
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (node x,node y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return ((belong[x.l]&1)?(x.r<y.r):(x.r>y.r));}
int main(){ int n;cin>>n;int block=(int)sqrt(n); for(int i=1;i<=n;i++) belong[i]=i/block; for(int i=1;i<=n;i++) a[i]=read(); Block.init();int m;cin>>m; for(int i=1;i<=m;i++) b[i]={read(),read(),i}; sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,Block.add(a[l]); while(r<b[i].r) r++,Block.add(a[r]); while(l<b[i].l) Block.del(a[l]),l++; while(r>b[i].r) Block.del(a[r]),r--; ans[b[i].id]=Block.ask(); } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}//有本事强制在线```:::
### [[Ynoi1999] XM66F](https://www.luogu.com.cn/problem/P10149)
考虑一下答案是怎么统计出来的。
75 行未改动}```:::
### [[HNOI2016] 序列](https://www.luogu.com.cn/problem/P3246)
我觉得我讲的比高赞题解好,因为我认为我的思维链更像人类一点。
首先这个题目长的比较像莫队,因为子序列拆到移动指针上就可以转化为“以某个点开头或结尾”的序列。
扩展 $[l,r]$ 的右端点时,我们需要刻画出的就是右端点为 $r+1$,左端点为 $[l,r+1]$ 的区间的最小值之和。
所以我们大概率是要把这个部分预处理出来了。
但是预处理所有可能的 $[l,r+1]$ 是不可行的,我们必须想办法压缩其存储。
直接的思路肯定是差分,也就是说,我们设 $f_i$ 代表左端点为 $[1,i]$,右端点为 $i$ 的区间信息之和。
但是 $[l,r+1]$ 与 $f_{r+1}-f_{l-1}$ 并不是等价的!因为我们原本是以 $r+1$ 为右端点的,而 $f_{l-1}$ 就将其变成了以 $l-1$ 为右端点!
---
也就是说,我们需要使用一个新的点,使得其满足差分性。
也就是说,我们需要找到一个点,使得以其为右端点和以 $r+1$ 为右端点是等价的。
即我们需要找到 $[l,r+1]$ 的最小值 $p$,然后我们就把问题拆成了两部分:
- 左端点为 $[l,p]$ 的部分,这一部分最小值都是 $a_p$,贡献为 $a_p \times (p-l+1)$。
- 左端点为 $[p+1,r+1]$ 的部分。
显然现在是可以差分的了!因为以 $p$ 为右端点就隐式的除去了 $[p+1,r+1]$ 这一部分对最小值的影响。
所以我们直接求 $f_{r+1}-f_p$ 即可。
求 $p$ 可以使用 RMQ 求区间最值。
我们先使用单调栈处理出 $pre_x$ 代表第一个小于 $x$ 的数的位置,则 $f$ 数组可以写出一个式子:$f_{x}=f_{pre_x}+(x-pre_x) \times a_{x}$。
至于左指针移动,对称的去做就好了。
:::success[代码]```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;
const int N=1e5+5;struct edge{int l,r,id;}b[N];int a[N],st[N],belong[N],top,n,m,l,r;int pre[N],suf[N];ll ans[N],fl[N],fr[N],now;struct item1{//ST 表 int st[N][25];
inline int cmp(int x,int y){//两个位置,返回值更小的 if(a[x]<a[y]) return x; return y; }
inline void init(){ for(int i=n;i>=1;i--){ st[i][0]=i; for(int j=1;i+(1<<j)-1<=n;j++) st[i][j]=cmp(st[i][j-1],st[i+(1<<(j-1))][j-1]); } }
inline int ask(int x,int y){ int now=log2(y-x+1); return cmp(st[x][now],st[y-(1<<now)+1][now]); }}ST;/*pre 和 suf 记录向左 / 向右找,第一个小于其的数fl[x] 代表左端点为 [1,x],右端点为 x 的最小值之和fr[x] 代表右端点为 [x,n],左端点为 x 的最小值之和*/
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (edge x,edge y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; return ((belong[x.l]&1)?(x.r<y.r):(x.r>y.r));}
inline void add_l(int x){ int p=ST.ask(l,r); ll ans1=1ll*a[p]*(r-p+1);//右端点为 [p,r] 的部分 ll ans2=fr[l]-fr[p];//右端点为 [l,p-1] 的部分 now+=(ans1+ans2);}
inline void add_r(int x){ int p=ST.ask(l,r); ll ans1=1ll*a[p]*(p-l+1);//左端点为 [l,p] 的部分 ll ans2=fl[r]-fl[p];//左端点在 [p+1,r] 的部分 now+=(ans1+ans2);}
inline void del_l(int x){ int p=ST.ask(l,r); ll ans1=1ll*a[p]*(r-p+1);//右端点为 [p,r] 的部分 ll ans2=fr[l]-fr[p];//右端点为 [l,p-1] 的部分 now-=(ans1+ans2);}
inline void del_r(int x){ int p=ST.ask(l,r); ll ans1=1ll*a[p]*(p-l+1);//左端点为 [l,p] 的部分 ll ans2=fl[r]-fl[p];//左端点在 [p+1,r] 的部分 now-=(ans1+ans2);}
signed main(){ cin>>n>>m; for(int i=1;i<=n;i++) a[i]=read(); //预处理 ST 表,使用单调栈求出 pre 和 suf ST.init(); for(int i=1;i<=n;i++){ while(top && a[st[top]]>a[i]) suf[st[top--]]=i; pre[i]=st[top],st[++top]=i; } while(top) pre[st[top]]=st[top-1],suf[st[top--]]=n+1; //通过 pre 数组和 suf 数组预处理 fl 和 fr for(int i=1;i<=n;i++) fl[i]=fl[pre[i]]+1ll*a[i]*(i-pre[i]); for(int i=n;i>=1;i--) fr[i]=fr[suf[i]]+1ll*a[i]*(suf[i]-i); //将原序列分块,将询问排序,莫队,启动! int block=(int)sqrt(n); for(int i=1;i<=n;i++) belong[i]=i/block+1; for(int i=1;i<=m;i++) b[i]={read(),read(),i}; sort(b+1,b+m+1);l=1,r=0; for(int i=1;i<=m;i++){ while(l>b[i].l) l--,add_l(l); while(r<b[i].r) r++,add_r(r); while(l<b[i].l) del_l(l),l++; while(r>b[i].r) del_r(r),r--; ans[b[i].id]=now; } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0; }```:::
# 带修莫队
## 引入
我们在原本莫队的基础上加入单点的修改。
这个问题该怎么做呢?难道我们每一部分都单独做一次莫队?
这是不行的,因为莫队需要初始化 $l=1$,$r=0$,如果初始操作区间是 $[n,n]$,你就需要移动 $\mathcal{O}(n)$ 次指针。这是不行的,因为莫队需要初始化 $l=1$,$r=0$,如果每次修改后的询问都是 $[n,n]$,你就需要移动 $\mathcal{O}(n)$ 次指针。
那么时间复杂度就会来到 $\mathcal{O}(nq)$,而这是无法接受的。
## 算法思想
尝试加入一个时间维度。
在每一次操作前,更新当前的时间维度,更改对应信息。
为了让复杂度更优,在排序的时候加一个判断:如果 $l,r$ 都相同,按照时间排序。为了让复杂度更优,在排序的时候加一个判断:如果 $\text{belong}_l,\text{belong}_r$ 都相同,按照时间排序。
假设有 $n$ 个元素,$m$ 次查询,$t$ 次更改。
时间复杂度吉祥物不会证,需要很多求导状物。至于分析时间复杂度,我不会啊。
总之,结论是,当块长取 $\frac{\sqrt[3]{n^{2}} \sqrt[3]{t}}{\sqrt[3]{m}}$ 时有最优时间复杂度 $\mathcal{O}(\sqrt[3]{n^{2}} \sqrt[3]{m^{2}} \sqrt[3]{t})$。总之结论是当块长取 $\frac{\sqrt[3]{n^{2}} \sqrt[3]{t}}{\sqrt[3]{m}}$ 时有最优时间复杂度 $\mathcal{O}(\sqrt[3]{n^{2}} \sqrt[3]{m^{2}} \sqrt[3]{t})$。
假设 $n,m,t$ 同阶,那么时间复杂度可以当作 $\mathcal{O}(n^{\frac{5}{3}})$,块长可以取 $n^{\frac{2}{3}}$。
## 例题217 行未改动全部展开}```:::
### [【模板】树套树](https://www.luogu.com.cn/problem/P3380)
您说的对,但是不强制在线,我们考虑用莫队。
求前驱和后继是简单的,因为这个可以转化为求排名和查询对应排名的值的结合体。
于是写一个莫队套值域分块,可以做到 $O(n^{\frac 5 3}+m \sqrt K)$,其中 $K$ 是值域大小,离散化后可以做到 $n+m$ 级别。
算出来时间复杂度只有 $9 \times 10^8$ 多一点。而莫队是小常数,随便跑。
:::success[代码]```cpp//belong=莫队(3bjqi300)#include<bits/stdc++.h>using namespace std;
const int N=2e5+5,K=4e5+10;struct item{int op,l,r,k;}now[N];struct node{int id,last,now;}b[N];struct edge{int op,l,r,k,tim,id;}c[N];int a[N],A[K],AA[N],belong[N],ans[N],tim,cnt,l=1,r=0,t=0;struct Val_Block{//值域分块 static const int P=640; int a[K],belong[K],sum[P],st[P],ed[P],block,num; /* block 记录块长,num 记录块数 st 和 ed 记录每一块的起点 & 终点 */
inline void init(int n){ block=(int)sqrt(n);num=n/block; for(int i=1;i<=n;i++){ belong[i]=i/block;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } }
inline void add(int x){a[x]++;sum[belong[x]]++;} inline void del(int x){a[x]--;sum[belong[x]]--;}
inline int ask1(int x){//查询 x 的排名 int ans=0; for(int i=0;i<belong[x];i++) ans+=sum[i];//前面的整块 for(int i=st[belong[x]];i<=x;i++) ans+=a[i];//当前块 return ans; }
inline int ask2(int k){//查询第 k 名 int id=-1; for(int i=0;i<=num;i++){ if(k>sum[i]) k-=sum[i]; else{id=i;break;} } if(id==-1) return -1; for(int i=st[id];i<=ed[id];i++){ if(k>a[i]) k-=a[i]; else return i; } }
inline int ask3(int x){//查询 x 的前驱 int tmp=ask1(x-1);//查询 x-1 的排名,随后我们可以认为 x 的排名为 tmp+1 if(tmp==0) return -2147483647;//不存在前驱 return ask2(tmp);//否则询问对应位置 }
inline int ask4(int x){//查询 x 的后继 int tmp=ask1(x)+1,sumx=0;//查询 x 的排名+1,随后我们可以认为 x 的排名为 tmp-1 for(int i=0;i<=num;i++) sumx+=sum[i];//得到当前总共有多少数 if(sumx<tmp) return 2147483647;//不存在后继 return ask2(tmp);//否则询问对应位置 }}Block;/*A 用于离散化,AA 用来处理修改tim 记录修改的数量,cnt 记录查询的数量b 是修改,c 是查询,now 离线处理信息,保证离散化*/
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (edge x,edge y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; if(belong[x.r]!=belong[y.r]) return belong[x.r]<belong[y.r]; return ((belong[x.r])?(x.tim<y.tim):(x.tim>y.tim)); }
inline void query(int tim){//进行第 tim 次修改 if(l<=b[tim].id && b[tim].id<=r) Block.del(b[tim].last);//在当前区间内,清理掉旧的 a[b[tim].id]=b[tim].now; if(l<=b[tim].id && b[tim].id<=r) Block.add(b[tim].now);//在当前区间内,放入新的}
inline void clear(int tim){//撤销第 tim 次修改 if(l<=b[tim].id && b[tim].id<=r) Block.del(b[tim].now);//在当前区间内,清理掉旧的 a[b[tim].id]=b[tim].last; if(l<=b[tim].id && b[tim].id<=r) Block.add(b[tim].last);//在当前区间内,放入新的}
int main(){ int n,m;cin>>n>>m;int block=3500,tmp=n;//tmp 用来离散化 for(int i=1;i<=n;i++) a[i]=read(),A[i]=a[i]; for(int i=1;i<=n;i++) belong[i]=i/block; //离散化,初始化值域分块 for(int i=1;i<=m;i++){ now[i].op=read(); if(now[i].op==1){ now[i].l=read();now[i].r=read(); now[i].k=read();A[++tmp]=now[i].k; }else if(now[i].op==2){ now[i].l=read();now[i].r=read(); now[i].k=read(); }else if(now[i].op==3){ now[i].l=read(),now[i].k=read(); A[++tmp]=now[i].k; }else if(now[i].op==4){ now[i].l=read();now[i].r=read(); now[i].k=read();A[++tmp]=now[i].k; }else{ now[i].l=read();now[i].r=read(); now[i].k=read();A[++tmp]=now[i].k; } } sort(A+1,A+tmp+1);int len=unique(A+1,A+tmp+1)-A-1;Block.init(len+5);//加 5 防止查询后继中访问空节点 for(int i=1;i<=n;i++) a[i]=AA[i]=lower_bound(A+1,A+len+1,a[i])-A;//离散化 for(int i=1;i<=m;i++){ if(now[i].op==1){//查询 1 now[i].k=lower_bound(A+1,A+len+1,now[i].k)-A; c[++cnt]={1,now[i].l,now[i].r,now[i].k,tim,cnt}; }else if(now[i].op==2){//查询 2 c[++cnt]={2,now[i].l,now[i].r,now[i].k,tim,cnt}; }else if(now[i].op==3){//修改 now[i].k=lower_bound(A+1,A+len+1,now[i].k)-A; b[++tim]={now[i].l,AA[now[i].l],now[i].k};AA[now[i].l]=now[i].k; }else if(now[i].op==4){//查询 3 now[i].k=lower_bound(A+1,A+len+1,now[i].k)-A; c[++cnt]={3,now[i].l,now[i].r,now[i].k,tim,cnt}; }else{//查询 4 now[i].k=lower_bound(A+1,A+len+1,now[i].k)-A; c[++cnt]={4,now[i].l,now[i].r,now[i].k,tim,cnt}; } } //进行带修莫队,注意查询的 op 为 1/2/3/4,而不是 1/2/4/5 m=cnt;sort(c+1,c+m+1); for(int i=1;i<=m;i++){ while(t<c[i].tim) t++,query(t); while(t>c[i].tim) clear(t),t--; while(l>c[i].l) l--,Block.add(a[l]); while(r<c[i].r) r++,Block.add(a[r]); while(l<c[i].l) Block.del(a[l]),l++; while(r>c[i].r) Block.del(a[r]),r--; //查询对应答案,注意查询的 op 为 2/3/4 时要还原信息 if(c[i].op==1) ans[c[i].id]=Block.ask1(c[i].k-1)+1;//注意此处定义为严格比它小的元素个数加一 if(c[i].op==2) ans[c[i].id]=A[Block.ask2(c[i].k)]; if(c[i].op==3) ans[c[i].id]=((Block.ask3(c[i].k)==-2147483647)?(-2147483647):(A[Block.ask3(c[i].k)]));//需要特判是否还原 if(c[i].op==4) ans[c[i].id]=((Block.ask4(c[i].k)==2147483647)?(2147483647):(A[Block.ask4(c[i].k)]));//需要特判是否还原 } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}```:::
### [Machine Learning](https://www.luogu.com.cn/problem/CF940F)
真让我找到带修莫队的题了( • ̀ω•́ )
首先你需要发现这是出现次数的 $\text{mex}$ 值,所以答案最多为 $\sqrt n$。
所以你甚至可以直接暴力找 $\text{mex}$。
但是这题需要离散化,因为 unordered_map 被 CF Hacker 卡了。
:::success[代码]```cpp#include<bits/stdc++.h>using namespace std;
const int N=2e5+5;struct item{int op,x,y;}d[N];struct node{int l,r,tim,id;}c[N];struct edge{int last,now,p;}b[N];int a[N],A[N],AA[N],belong[N],block;int mp[N],cntx[N],ans[N],tot,l=1,r,t;/*d 辅助离散化mp 记录出现次数b 是更改,c 是查询cnt 记录出现次数的出现次数A 是构建 b 的临时数组,AA 用于离散化*/
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (node x,node y){ if(belong[x.l]!=belong[y.l]) return belong[x.l]<belong[y.l]; if(belong[x.r]!=belong[y.r]) return belong[x.r]<belong[y.r]; return ((belong[x.r]&1)?(x.tim<y.tim):(x.tim>y.tim));}
inline void add(int x){ if(mp[x]!=0) cntx[mp[x]]--; mp[x]++,cntx[mp[x]]++;}
inline void del(int x){ cntx[mp[x]]--,mp[x]--; if(mp[x]!=0) cntx[mp[x]]++;}
inline void query(int tim){//处理 tim 时刻的操作 int p=b[tim].p,x=b[tim].now; if(l<=p && p<=r) del(a[p]);//在区间里,删掉 a[p]=x; if(l<=p && p<=r) add(a[p]);//在区间里,加入}
inline void clear(int tim){//撤销 tim 时刻的操作 int p=b[tim].p,x=b[tim].last; if(l<=p && p<=r) del(a[p]);//在区间里,删掉 a[p]=x; if(l<=p && p<=r) add(a[p]);//在区间里,加入}
signed main(){ int n,m;cin>>n>>m;block=pow(n,1.0*2/3); for(int i=1;i<=n;i++) belong[i]=i/block; for(int i=1;i<=n;i++) a[i]=read(),AA[++tot]=a[i]; for(int i=1;i<=m;i++){ int op=read(); if(op==1){ int l=read(),r=read(); d[i]={op,l,r}; }else{ int pl=read(),x=read(); AA[++tot]=x;d[i]={op,pl,x}; } } sort(AA+1,AA+tot+1);int len=unique(AA+1,AA+tot+1)-AA-1; for(int i=1;i<=n;i++) a[i]=lower_bound(AA+1,AA+len+1,a[i])-AA,A[i]=a[i]; int cnt=0,tim=0;//cnt 是查询,tim 是修改 for(int i=1;i<=m;i++){ int op=d[i].op; if(op==1){ int l=d[i].x,r=d[i].y; c[++cnt]={l,r,tim,cnt}; }else{ int pl=d[i].x,x=lower_bound(AA+1,AA+len+1,d[i].y)-AA; b[++tim]={A[pl],x,pl};A[pl]=x; } } m=cnt;sort(c+1,c+m+1); for(int i=1;i<=m;i++){ while(l>c[i].l) l--,add(a[l]); while(r<c[i].r) r++,add(a[r]); while(l<c[i].l) del(a[l]),l++; while(r>c[i].r) del(a[r]),r--; while(t<c[i].tim) t++,query(t); while(t>c[i].tim) clear(t),t--; for(int j=1;j<=500;j++) if(cntx[j]==0){ans[c[i].id]=j;break;} } for(int i=1;i<=cnt;i++) cout<<ans[i]<<"\n"; return 0;}```:::
# 回滚莫队
## 引入
362 行未改动全部展开### [数列分块入门 9](https://www.luogu.com.cn/problem/P13984)
处理众数,不要求强制在线。
不难想到回滚莫队,插入是容易的,删除是难得,写一个不删除莫队就好。不难想到回滚莫队,插入是容易的,删除是难的,写一个不删除莫队就好。
:::success[代码]```cpp#include<bits/stdc++.h>60 行未改动本质其实是去掉一种操作,然后通过记录更改实现撤销操作。
也就是说,当我们的莫队时间复杂度过大,可以考虑**只有插入**或者**只有删除**的情况,如果有一种是好做的,我们就可以尝试回滚莫队。
## 能力提升训练
### [[Cnoi2019] 数字游戏](https://www.luogu.com.cn/problem/P5386)
$2 \times 10^5$ 的量级,不带修,可以离线,理所当然的想到莫队。
要处理两个问题,那么无非就只有两种选择:
- 对 $[l,r]$ 做莫队,用值域分块做值域 $[x,y]$ 的限制。
- 对 $[x,y]$ 做莫队,用序列分块做位置 $[l,r]$ 的限制。
这里使用分块的原因是暴力数据结构肯定比别的数据结构的功能更多。
而序列分块往往更有前途,于是我们考虑对 $[x,y]$ 做莫队。
而原题可以转化为:定义 $b_i=[x \le a_i \le y]$,求在区间 $[l,r]$ 上的每一段 $1$ 的贡献之和。
长度为 $k$ 的连续 $1$ 的贡献为 $\frac{k \times (k+1)}{2}$。
因为这是一个排列,所以我们改变 $x$ 或改变 $y$ 时,有且仅有一个点的 $0/1$ 值会变化。
我们需要做到 $O(1)$ 更改和 $O(\sqrt n)$ 的查询,$0 \to 1$ 是容易的,而 $1 \to 0$ 需要找到两端的对应全 $1$ 串。
所以,$0 \to 1$ 是 $O(1)$ 的,$1 \to 0$ 是 $O(\sqrt n)$,这是我们不想看到的。
考虑使用回滚莫队应对这个问题。
回滚部分是简单的,考虑如何实现分块。
插入时,讨论两侧是否存在可合并的信息。
维护一个 $p$ 数组,记录**当前块**内,当前点的另一个端点。
左端点能合并,需要其是 $1$ 且当前点不是当前块最左侧,因为 $p$ 不能跨越一块。
右端点能合并,需要其是 $1$ 且当前点不是当前块最右侧,因为 $p$ 不能跨越一块。
接下来对答案的讨论,认为 $last-p_{last}=len_1$,$p_{ne}-ne=len_2$
- 左端点和右端点都能合并
改变了左端点和右端点的 $p$ 数组和答案。
- 原本的答案是 $\frac{len_1 \times (len_1+1)+len_2 \times (len_2+1)}{2}$。
- 新的答案是 $\frac{(len_1+len_2+1) \times (len_1+len_2+2)}{2}$。
展开有:
- 原本的答案是 $\frac{(len_1^2 + len_1 + len_2^2 + len_2)}{2}$。
- 新的答案是 $\frac{len_1^2 + len_2^2 + 2\times len_1 \times len_2 + 3 \times len_1 + 3 \times len_2 + 2}{2}$。
相减算出答案之差为 $(len_1 \times len_2 + len_1 + len_2 + 1)$。
因式分解有 $(len_1+1) \times (len_2+1)$。
而 $len_1 + 1=last - p_{last} + 2$,$len_2 + 1=p_{ne} - ne + 2$。
所以原式为 $(last - p_{last} + 2) \times (p_{ne} - ne + 2)$。
- 左端点能合并,右端点不能合并
改变了当前点和左端点的 $p$ 数组和答案。
那么这里长度增加了 $1$。
即为 $\frac{(len_1+1) \times (len_1+2) - len_1 \times (len_1+1)}{2}$,化简为 $len_1+1$,即为 $last-p_{last}+2$。
- 左端点不能合并,右端点能合并
改变了当前点和右端点的 $p$ 数组和答案。
同理,为 $p_{ne}-ne+2$。
- 左端点和右端点都不能合并
当前点只有 $1$ 的贡献。
:::success[代码]```cpp//belong=莫队(3bjqi300)#include<bits/stdc++.h>#define ll long longusing namespace std;
const int N=2e5+5,Q=4e5+5,K=505;struct edge{int l,r,x,y,id;}b[N];int pos[N],a[N],belong[N],st[K],ed[K];ll ans[N];struct Line_Block{//序列分块+撤销 #define calc(x) (1ll*(x)*((x)+1)/2) struct item{int id[4],op,idx;ll delta;}s[Q];int top; int belong[N],p[N],st[K],ed[K],block;bool tag[N];ll sum[K]; /* p[i] 记录当前块内,i 的另一个端点,tag 记录当前点是否为 1 sum 记录当前块的信息之和,s 记录需要撤销的对应的更改,分别记录两对端点和 ans 的改变 */
inline void init(int n){ block=(int)sqrt(n); for(int i=1;i<=n;i++){ belong[i]=(i-1)/block+1;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } }
inline void add(int x){//将 x 放入 item tmp;tmp.op=0;memset(tmp.id,0,sizeof tmp.id);//记录当前修改,初始全空 int now=pos[x],last=now-1,ne=now+1;//当前点位置与其前后点 p[now]=now,tag[now]=1;tmp.idx=now;//默认当前点指向自己,标记当前点为 1 bool tag1=(tag[last] && now!=st[belong[now]]);//左侧是否存在左端点 bool tag2=(tag[ne] && now!=ed[belong[now]]);//右侧是否存在右端点 if(tag1 && tag2){//左 & 右 int lx=p[last],rx=p[ne]; tmp.id[0]=last,tmp.id[1]=lx;//第一队:左侧 && 左侧的左端点 tmp.id[2]=ne,tmp.id[3]=rx;//第二队:右侧 && 右侧的右端点 tmp.delta=1ll*(last-lx+2)*(rx-ne+2);//答案之差,更新答案 p[lx]=rx;p[rx]=lx;//更新信息 }else if(tag1 && !tag2){//左 int lx=p[last]; tmp.id[0]=last,tmp.id[1]=lx;//左侧 && 左侧的左端点 tmp.id[2]=tmp.id[3]=now;//当前点 tmp.delta=last-lx+2;//答案之差,更新答案 p[lx]=now,p[now]=lx;//更新信息 }else if(!tag1 && tag2){//右 int rx=p[ne]; tmp.id[0]=ne,tmp.id[1]=rx;//右侧 && 右侧的右端点 tmp.id[2]=tmp.id[3]=now;//当前点 tmp.delta=rx-ne+2;//答案之差,更新答案 p[rx]=now,p[now]=rx;//更新信息 }else{//全都没有,独立一块 tmp.op=-1; tmp.delta=1; } s[++top]=tmp;//记录修改 sum[belong[now]]+=tmp.delta;//更新对应块的总信息 }
inline ll ask(int l,int r){//查询 [l,r] 的总收益 if(belong[l]==belong[r]){//同一块,直接暴力 int cnt=0;ll ans=0; for(int i=l;i<=r;i++){ if(tag[i]) cnt++; else ans+=calc(cnt),cnt=0; } return ans+calc(cnt);//这里要加上最后剩下的信息 } //先暴力处理两侧,得到两侧剩下的 int cnt1=0,cnt2=0;ll ans=0; for(int i=l;i<=ed[belong[l]];i++){ if(tag[i]) cnt1++; else ans+=calc(cnt1),cnt1=0; } for(int i=r;i>=st[belong[r]];i--){ if(tag[i]) cnt2++; else ans+=calc(cnt2),cnt2=0; } //逐个块处理,一旦遇到非 0 就记录对应答案 int tot=cnt1;//初始化为左侧剩余长度 for(int i=belong[l]+1;i<=belong[r]-1;i++){ if(p[st[i]]==ed[i]) tot+=(ed[i]-st[i]+1);//当前就是一整块都符合,直接记录 else{ if(tag[st[i]]) tot+=(p[st[i]]-st[i]+1),ans-=calc(p[st[i]]-st[i]+1);//前面还有一段,拿上,减去重复部分 ans+=calc(tot);tot=0;ans+=sum[i];//处理当前信息,将 tot 归零,并加上中间的部分 if(tag[ed[i]]) tot+=(ed[i]-p[ed[i]]+1),ans-=calc(ed[i]-p[ed[i]]+1);//前面还有一段,拿上,减去重复部分 } } return ans+calc(tot+cnt2);//注意这里要加上最后剩下的部分 }
inline void clear(int x){//清理到 x while(top!=x){ item tmp=s[top];top--;tag[tmp.idx]=0;//取出当前的信息,将其变回 0 sum[belong[tmp.idx]]-=tmp.delta;//减去原本的信息 if(tmp.op==-1) continue;//当前点是独立点,不需要更新 p 数组 p[tmp.id[1]]=tmp.id[0];//重新指向原本对应的值 p[tmp.id[3]]=tmp.id[2];//重新指向原本对应的值 } }}Block;
inline int read(){ int s=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); return s*f;}
inline bool operator < (edge x,edge y){ if(belong[x.x]!=belong[y.x]) return belong[x.x]<belong[y.x]; return x.y<y.y;}
int main(){ int n,m;cin>>n>>m;int block=(int)sqrt(n);Block.init(n); for(int i=1;i<=n;i++) a[i]=read(),pos[a[i]]=i; for(int i=1;i<=n;i++){ belong[i]=(i-1)/block+1;ed[belong[i]]=i; if(st[belong[i]]==0) st[belong[i]]=i; } for(int i=1;i<=m;i++) b[i]={read(),read(),read(),read(),i}; sort(b+1,b+m+1);int l=1,r=0; for(int i=1;i<=m;i++){ if(belong[b[i].x]!=belong[b[i-1].x]){ Block.clear(0);//清空信息 l=ed[belong[b[i].x]]+1,r=ed[belong[b[i].x]]; } if(belong[b[i].x]==belong[b[i].y]){//在同一块,直接暴力处理 for(int j=b[i].x;j<=b[i].y;j++) Block.add(j); ans[b[i].id]=Block.ask(b[i].l,b[i].r);Block.clear(0); }else{//不在同一块,回滚莫队处理 while(r<b[i].y) r++,Block.add(r); int topx=Block.top,lx=l; while(l>b[i].x) l--,Block.add(l); ans[b[i].id]=Block.ask(b[i].l,b[i].r); Block.clear(topx);l=lx; } } for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; return 0;}```:::
# 树上莫队
注:这里默认的是查询路径,因为查询子树直接拍平就好了。
## 引入
就像名字一样,把莫队上树。
290 行未改动全部展开### [[CTSC2008] 网络管理](https://www.luogu.com.cn/problem/P4175)
看了一圈没看到吉祥物喜欢的做法。
啊,根号,你崛起罢!啊,莫队,你崛起罢!
众所周知,我们可以使用值域分块查询第 $k$ 大,这里要求将这个第 $k$ 大上树,且带修。
考虑现将所有操作读下来,处理所有权值,将其离散化,随后使用树上带修莫队套个值域分块就好。159 行未改动全部展开## 引入
我们先前做的题目基本都是求关于一个指定的数的对应信息。
但是,假如吉祥物把它变成:求两个数的异或值与 $x$ 恰好有 $k$ 位不同的话,我们好像只能够去枚举哪些位数不同,那么时间复杂度就会多乘上一个 $C_{\textrm{len}}^k$,其中 $\textrm{len}$ 是其在二进制下的长度。但是,假如我们把它变成:求两个数的异或值与 $x$ 恰好有 $k$ 位不同的话,我们好像只能够去枚举哪些位数不同,那么时间复杂度就会多乘上一个 $C_{\textrm{len}}^k$,其中 $\textrm{len}$ 是其在二进制下的长度。
这是我们无法承受的。
## 算法思想615 行未改动全部展开