莫队
普通莫队
普通莫队处理静态多次询问的离线算法,基于分块的思想,时间复杂度
问题模型
对于序列上的区间询问问题,如果能从
通常要求是
过程
假设现在我们已经知道怎么
莫队采用了分块的思想,对询问分块后,每个元素都有自己属于的块
这样排序后暴力转移从上个区间转移到下个区间的时间复杂度为
:::success[证明:]{open}
设块长为
先看左指针
分类讨论从
- 若
l_i 和l_i+1 在同一块内,那么移动距离不会超过块长B ,q 次询问,总共O(qB) 。 - 若
l_i 和l_i+1 不在同一块内,这种情况最多发生块的个数\frac{n}{B} 次,每次移动距离最多为n ,总共O(\frac{n^2}{B})
加起来总共
再看右指针
跨块时最多移动
加起来总共
| 所以总时间复杂度上界为 |
|---|
由上面的证明可以看出莫队的块长通常取
常数优化: 奇偶优化。
手动模拟一下莫队的过程可以发现,如果单纯块内按照
奇偶化排序即对于属于奇数块的询问,
可以证明采用了就优化时间复杂度上界并不会变,但常数确实要小得多。
例题
P2709 【模板】莫队 / 小 B 的询问
题意:
有一个长为
有
其中
做法:
先看能否
既然可以离线也能
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,m,k,a[N];
int cnt[N],block,ans;
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(l/block!=p.l/block) return l/block<p.l/block;
return ((l/block)&1)?r<p.r:r>p.r;
}
}q[N];
void add(int p){
ans-=cnt[a[p]]*cnt[a[p]];
cnt[a[p]]++;
ans+=cnt[a[p]]*cnt[a[p]];
}
void del(int p){
ans-=cnt[a[p]]*cnt[a[p]];
cnt[a[p]]--;
ans+=cnt[a[p]]*cnt[a[p]];
}
int Q[N];
signed main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
block=sqrt(n);
sort(q+1,q+m+1);
int l=1,r=0;
for(int i=1;i<=m;i++){
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(l<q[i].l) del(l++);
while(r>q[i].r) del(r--);
Q[q[i].id]=ans;
}
for(int i=1;i<=m;i++) cout<<Q[i]<<'\n';
return 0;
}
:::
注意:
莫队处理中 4 个 while 循环的顺序最好严格按照上面的顺序写,先扩大再收缩,先处理左指针再处理右指针。
P1494 [国家集训队] 小 Z 的袜子
题意:
给定
做法:
显然是可以离线的,考虑怎么向相邻区间转移。假设新加入颜色
删除颜色则相反,然后分母答案为
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,m,k,a[N];
int cnt[N],block,ans;
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(l/block!=p.l/block) return l/block<p.l/block;
return ((l/block)&1)?r<p.r:r>p.r;
}
}q[N];
void add(int p){
ans-=cnt[a[p]]*cnt[a[p]];
cnt[a[p]]++;
ans+=cnt[a[p]]*cnt[a[p]];
}
void del(int p){
ans-=cnt[a[p]]*cnt[a[p]];
cnt[a[p]]--;
ans+=cnt[a[p]]*cnt[a[p]];
}
int Q[N];
signed main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
block=sqrt(n);
sort(q+1,q+m+1);
int l=1,r=0;
for(int i=1;i<=m;i++){
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(l<q[i].l) del(l++);
while(r>q[i].r) del(r--);
Q[q[i].id]=ans;
}
for(int i=1;i<=m;i++) cout<<Q[i]<<'\n';
return 0;
}
:::
P3709 大爷的字符串题
双倍经验:P1997 faebdc 的烦恼
题意:
题面比较一坨,其实就是多次询问区间众数的出现次数。
做法:
首先这道题可以离线,插入和删除都可以用一个桶维护数的出现次数,再用一个桶维护所有出现次数的数的个数,
所以直接上普通莫队板子即可。
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N],b[N],block;
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(l/block!=p.l/block) return l/block<p.l/block;
return ((l/block)&1)?r<p.r:r>p.r;
}
}q[N];
int cnt[N],num[N],ans,Q[N];
void add(int x){
num[cnt[a[x]]]--;
cnt[a[x]]++;
num[cnt[a[x]]]++;
ans=max(ans,cnt[a[x]]);
}
void del(int x){
num[cnt[a[x]]]--;
if(cnt[a[x]]==ans&&!num[cnt[a[x]]]) ans--;
cnt[a[x]]--;
num[cnt[a[x]]]++;
}
int main(){
cin>>n>>m;
block=n/sqrt(m);
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+len+1,a[i])-b;
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+m+1);
int l=1,r=0;
for(int i=1;i<=m;i++){
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(l<q[i].l) del(l++);
while(r>q[i].r) del(r--);
Q[q[i].id]=ans;
}
for(int i=1;i<=m;i++) cout<<-Q[i]<<'\n';
return 0;
}
:::
还有更多普通莫队题,都是考用什么数据结构维护,通常时需要用值域分块平衡复杂度(
如下:
P3604 美好的每一天
P3730 曼哈顿交易
P4396 [AHOI2013] 作业
P4867 Gty的妹子序列
带修莫队
普通的莫队无法解决带单点修改的问题,如果对每两次修改之间间隔跑莫队,时间到达
过程
为了强行让其支持修改,我们多引入时间的维度。
把询问
 将莫队区间的左端点
因为排序,所以
但是
可以发现这个过程中
:::success[图解]{open}
蓝色的为枚举的当前块,黄色箭头为
第一次询问
第二次询问从块的右端点开始
第三次询问与第二次询问同理。
| 由此可以清晰看出回滚莫队是怎么避免删除操作的。 |
|---|
时间复杂度:
:::success[证明]{open}
若
否则按照上面的过程更新。
右端点移动为
左端点移动不超过块长
| 总时间复杂度 |
|---|
例题
P5906 【模板】回滚莫队 & 不删除莫队
题意:
给定一个序列,多次询问一段区间
序列中两个元素的间隔距离指的是两个元素下标差的绝对值。
做法:
多次询问考虑离线用莫队处理,需要维护的信息插入很好做,用桶记录最早的出现位置和最末的出现位置,能够实现
由此想到只是用插入操作,做回滚莫队。然后套上面的做法就行。
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m;
int a[N],b[N],bel[N],block,Q[N];
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(bel[l]!=bel[p.l]) return bel[l]<bel[p.l];
return r<p.r;
}
}q[N];
int st[N],ed[N],st2[N];
int clac(int l,int r){
int mx=0;
for(int i=l;i<=r;i++){
if(!st2[a[i]]) st2[a[i]]=i;
else mx=max(mx,i-st2[a[i]]);
}
for(int i=l;i<=r;i++) st2[a[i]]=0;
return mx;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+len+1,a[i])-b;
cin>>m;
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
block=max(n/sqrt(m),1.0);
for(int i=1;i<=n;i++) bel[i]=(i-1)/block+1;
sort(q+1,q+m+1);
for(int i=1,j=1;i<=bel[n];i++){
int R=min(n,i*block),r=R,l=r+1,ans=0;
memset(st,0,sizeof st);
memset(ed,0,sizeof ed);
for(;bel[q[j].l]==i;j++){
if(bel[q[j].l]==bel[q[j].r]){
Q[q[j].id]=clac(q[j].l,q[j].r);
continue;
}
while(r<q[j].r){
r++;
ed[a[r]]=r;
if(!st[a[r]]) st[a[r]]=r;
else ans=max(ans,r-st[a[r]]);
}
int tmp=ans;
while(l>q[j].l){
l--;
if(!ed[a[l]]) ed[a[l]]=l;
else ans=max(ans,ed[a[l]]-l);
}
Q[q[j].id]=ans;
ans=tmp;
while(l<=R) {
if(ed[a[l]]==l) ed[a[l]]=0;
l++;
}
}
}
for(int i=1;i<=m;i++) cout<<Q[i]<<'\n';
return 0;
}
:::
P13984 数列分块入门 9
ps: 之前我写在线数列分块现在都没调过。
题意:
给出一个长为
做法:
这道题目可以离线,区间众数很明显是易于插入而难删除的信息,用桶维护出现次数就可以实现
所以采用回滚莫队解决,注意需要对左右指针分别开桶避免信息污染。
回滚莫队比在线的数列分块要快的多。
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
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;
}
const int N=3e5+5;
int n,a[N],b[N];
int block,bel[N],Q[N];
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(bel[l]!=bel[p.l]) return bel[l]<bel[p.l];
return r<p.r;
}
}q[N];
int num[N];
int clac(int l,int r){
int mn=INT_MAX,mx=0;
for(int i=l;i<=r;i++) num[a[i]]=0;
for(int i=l;i<=r;i++){
num[a[i]]++;
if(num[a[i]]>mx) mx=num[a[i]],mn=a[i];
else if(mx==num[a[i]]) mn=min(mn,a[i]);
}
return mn;
}
int cntL[N],cntR[N];
int main(){
n=read();
block=sqrt(n);
for(int i=1;i<=n;i++) a[i]=read(),bel[i]=(i-1)/block+1,b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+len+1,a[i])-b;
for(int i=1;i<=n;i++) q[i]={read(),read(),i};
sort(q+1,q+n+1);
for(int i=1,j=1;i<=bel[n];i++){
int R=min(i*block,n),l=R+1,r=R,mx=0,mn=INT_MAX;
memset(cntR,0,sizeof cntR);
for(;bel[q[j].l]==i;j++){
if(bel[q[j].l]==bel[q[j].r]){
Q[q[j].id]=clac(q[j].l,q[j].r);
continue;
}
while(r<q[j].r){
r++;
cntR[a[r]]++;
if(cntR[a[r]]>mx) mx=cntR[a[r]],mn=a[r];
else if(cntR[a[r]]==mx) mn=min(mn,a[r]);
}
int tmp1=mx,tmp2=mn;
while(l>q[j].l){
l--;
cntL[a[l]]++;
if(cntR[a[l]]+cntL[a[l]]>mx) mx=cntR[a[l]]+cntL[a[l]],mn=a[l];
else if(mx==cntR[a[l]]+cntL[a[l]]) mn=min(mn,a[l]);
}
Q[q[j].id]=mn;
mx=tmp1,mn=tmp2;
while(l<=R){
cntL[a[l]]--;
l++;
}
}
}
for(int i=1;i<=n;i++) cout<<b[Q[i]]<<'\n';
return 0;
}
:::
P14420 [JOISC 2014] 历史的研究 / Historical Research
题意:
给你一个长度为
思路:
还是回滚莫队板子题,插入操作可以用桶
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n,m,a[N],b[N],c[N];
int block,bel[N],ans[N];
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(bel[l]!=bel[p.l]) return bel[l]<bel[p.l];
return r<p.r;
}
}q[N];
int cnt[N],num[N];
int clac(int l,int r){
int mx=0;
for(int i=l;i<=r;i++) num[c[i]]=0;
for(int i=l;i<=r;i++) num[c[i]]++,mx=max(mx,num[c[i]]*a[i]);
return mx;
}
signed main(){
cin>>n>>m;
block=n/sqrt(m);
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i],bel[i]=(i-1)/block+1;
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) c[i]=lower_bound(b+1,b+len+1,a[i])-b;
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+m+1);
for(int i=1,j=1;i<=bel[n];i++){
int R=min(n,i*block),l=R+1,r=R,mx=0;
memset(cnt,0,sizeof cnt);
for(;bel[q[j].l]==i;j++){
if(bel[q[j].l]==bel[q[j].r]){
ans[q[j].id]=clac(q[j].l,q[j].r);
continue;
}
while(r<q[j].r){
r++;
cnt[c[r]]++;
mx=max(mx,a[r]*cnt[c[r]]);
}
int tmp=mx;
while(l>q[j].l){
l--;
cnt[c[l]]++;
mx=max(mx,a[l]*cnt[c[l]]);
}
ans[q[j].id]=mx;
mx=tmp;
while(l<=R){
cnt[c[l]]--;
l++;
}
}
}
for(int i=1;i<=m;i++) cout<<ans[i]<<'\n';
return 0;
}
:::
P8078 [WC2022] 秃子酋长
题意:
给一个长为
做法:
问题可以离线,先考虑普通莫队的做法。对于插入和删除操作,都可以用 set 维护,查询其在 set 中的前驱和后继,再将其前驱后继位置之差减去,再加上其分别到前驱和后继的距离差,删除则相反。
但是这样做必须用 set 维护,是
我们可以发现插入是难以
只删除的回滚莫队跟只插入的回滚莫队有什么区别? 其实刚好相反。
- 将询问按照
bel_l 为第一关键字升序,r 为第二关键字降序排序。 - 对每块单独处理,先将
l 设为块的左端点,r 设为n ,开始收缩只需要删除操作。
然后同样是继承
回顾只插入的莫队,需要暴力的原因是
这样做的时间复杂度证明和只插入的回滚莫队相同,就不赘述了。
实现细节: 回滚撤销通法
可以发现链表的撤销回滚并不是很好做,因为涉及了多个数组。而且如果使用更复杂的数据结构维护需要撤销的变量会越来越多,有没有撤销的通法呢?
我们可以用栈储存修改过的变量和其原来的值,最后逆序还原就行。问题是不知道存储的是哪个变量的值,解决方法是把栈内储存的信息变为变量地址和其原来的值,这样就能精准撤销。
struct node{
int* it;//变量的地址
int upt;//变量的原值
}st[N];
st[++top]={&le[ri[now]],le[ri[now]]};//将即将更改的值压入栈中
while(top) *st[top].it=st[top].upt,top--;//撤销还原
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,a[N];
int block,bel[N];
struct node{
int l,r,id;
bool operator < (const node &p)const{
if(bel[l]!=bel[p.l]) return bel[l]<bel[p.l];
return r>p.r;
}
}q[N];
int pos[N],le[N],ri[N],head,tail;
long long ans,Q[N];
void init(int L){
ans=head=tail=0;
for(int i=1;i<=n;i++){
if(pos[i]>=L){
if(tail) ri[tail]=i,le[i]=tail,ans+=abs(pos[i]-pos[tail]);
else head=i;
tail=i;
}
}
ri[tail]=le[head]=0;
}
struct node2{
int* it;
int upt;
}st[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
block=n/sqrt(m);
for(int i=1;i<=n;i++) cin>>a[i],pos[a[i]]=i,bel[i]=(i-1)/block+1;
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+m+1);
for(int i=1,j=1;i<=bel[n];i++){
int L=(i-1)*block,l=L+1,r=n;
init(L+1);
for(;bel[q[j].l]==i;j++){
while(r>q[j].r){
int now=a[r];
if(ri[now]){
le[ri[now]]=le[now];
ans-=abs(pos[now]-pos[ri[now]]);
}
if(le[now]){
ri[le[now]]=ri[now];
ans-=abs(pos[now]-pos[le[now]]);
}
if(ri[now]&&le[now]) ans+=abs(pos[ri[now]]-pos[le[now]]);
r--;
}
long long tmp=ans;
int top=0;
while(l<q[j].l){
int now=a[l];
if(ri[now]){
st[++top]={&le[ri[now]],le[ri[now]]};
le[ri[now]]=le[now];
ans-=abs(pos[now]-pos[ri[now]]);
}
if(le[now]){
st[++top]={&ri[le[now]],ri[le[now]]};
ri[le[now]]=ri[now];
ans-=abs(pos[now]-pos[le[now]]);
}
if(ri[now]&&le[now]) ans+=abs(pos[ri[now]]-pos[le[now]]);
l++;
}
Q[q[j].id]=ans;
ans=tmp;
l=L+1;
while(top) *st[top].it=st[top].upt,top--;
}
}
for(int i=1;i<=m;i++) cout<<Q[i]<<'\n';
return 0;
}
:::
P5386 [Cnoi2019] 数字游戏
题意:
给定一个排列,每次询问
做法:
问题可以离线,考虑莫队做法。但现在有两组区间
假设对
假设对
可以发现把 1 改成 0 不好做,但把 0 改成 1 好做,所以用只插入的回滚莫队。那么该选取什么数据结构? 插入有
若使用线段树维护 0 / 1 序列连续段,插入和询问都是
考虑怎么用值域分块维护,散块暴力处理,整块维护区间所有 1 连续段
回滚莫队 + 值域分块做完了(代码好难写!)。
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N];
struct Blocks{
int bel[N],L[N],R[N],block,w[N],top1,top2;
struct node{
long long sum;
long long Llen,Rlen,len;
node(){sum=Llen=Rlen=len=0;}
node operator + (const node &oth)const{
node a=*this,b=oth;
if(!a.len) return b;
if(!b.len) return a;
node c;
c.len=a.len+b.len;
c.Llen=a.Llen;
if(a.Llen==a.len) c.Llen+=b.Llen;
c.Rlen=b.Rlen;
if(b.Rlen==b.len) c.Rlen+=a.Rlen;
c.sum=a.sum+b.sum-a.Rlen*(a.Rlen+1)/2-b.Llen*(b.Llen+1)/2+(a.Rlen+b.Llen)*(a.Rlen+b.Llen+1)/2;
return c;
}
}t[N];
struct stk{
node* it;
node upd;
}st1[N];
struct stk2{
int* it;
int upd;
}st2[N];
void init(){
top1=top2=0;
block=sqrt(n);
for(int i=1;i<=n;i++) bel[i]=(i-1)/block+1,w[i]=0;
for(int i=1;i<=bel[n];i++){
L[i]=(i-1)*block+1;
R[i]=min(i*block,n);
t[i]=node();
t[i].len=R[i]-L[i]+1;
}
}
void modify(int x){
st2[++top2]={&w[x],w[x]};
w[x]=1;
st1[++top1]={&t[bel[x]],t[bel[x]]};
int l=x,r=x;
while(l>L[bel[x]]&&w[l-1]) l--;
while(r<R[bel[x]]&&w[r+1]) r++;
long long L1=x-l,L2=r-x,Ltot=r-l+1;
t[bel[x]].sum=t[bel[x]].sum-L1*(L1+1)/2-L2*(L2+1)/2+Ltot*(Ltot+1)/2;
if(l==L[bel[x]]) t[bel[x]].Llen=r-l+1;
if(r==R[bel[x]]) t[bel[x]].Rlen=r-l+1;
}
void rollback(){
while(top1) *st1[top1].it=st1[top1].upd,top1--;
while(top2) *st2[top2].it=st2[top2].upd,top2--;
}
long long query(int l,int r){
if(bel[l]==bel[r]){
long long ans=0; int len=0;
for(int i=l;i<=r;i++){
if(w[i]) ans+=++len;
else len=0;
}
return ans;
}
node ans;
for(int i=bel[l]+1;i<=bel[r]-1;i++) ans=ans+t[i];
node lst; int len=0;
lst.len=R[bel[l]]-l+1;
for(int i=l;i<=R[bel[l]];i++){
if(w[i]) lst.sum+=++len;
else len=0;
}
lst.Rlen=len; ans=lst+ans;
node rst; len=0;
rst.len=r-L[bel[r]]+1;
for(int i=r;i>=L[bel[r]];i--){
if(w[i]) rst.sum+=++len;
else len=0;
}
rst.Llen=len; ans=ans+rst;
return ans.sum;
}
}B;
int block,bel[N],pos[N];
long long Q[N];
struct node{
int l,r,x,y,id;
bool operator < (const node &p)const{
if(bel[x]!=bel[p.x]) return bel[x]<bel[p.x];
return y<p.y;
}
}q[N];
int main(){
cin>>n>>m;
block=n/sqrt(m);
for(int i=1;i<=n;i++) cin>>a[i],pos[a[i]]=i,bel[i]=(i-1)/block+1;
for(int i=1;i<=m;i++) cin>>q[i].l>>q[i].r>>q[i].x>>q[i].y,q[i].id=i;
sort(q+1,q+m+1);
for(int i=1,j=1;i<=bel[n];i++){
int R=min(i*block,n),l=R+1,r=R;
B.init();
for(;bel[q[j].x]==i;j++){
if(bel[q[j].x]==bel[q[j].y]){
for(int k=q[j].x;k<=q[j].y;k++) B.modify(pos[k]);
Q[q[j].id]=B.query(q[j].l,q[j].r);
B.rollback();
continue;
}
while(r<q[j].y){
r++;
B.modify(pos[r]);
}
B.top1=B.top2=0;
while(l>q[j].x){
l--;
B.modify(pos[l]);
}
Q[q[j].id]=B.query(q[j].l,q[j].r);
B.rollback();
l=R+1;
}
}
for(int i=1;i<=m;i++) cout<<Q[i]<<'\n';
return 0;
}
:::
P6349 [PA 2011] Kangaroos
题意:
给出长为
每次询问树上两点间一条路径
大约在
:::info[代码]
#include<bits/stdc++.h>
using namespace std;
const int N=4e4+5;
int n,m,a[N],b[N],block;
vector<int> v[N];
int sz[N],son[N],fat[N],dep[N];
int mx[N],id[N],idx;
void dfs1(int x,int fa){
fat[x]=fa;
sz[x]=1;
dep[x]=dep[fa]+1;
mx[x]=dep[x];
for(int y:v[x]){
if(y==fa) continue;
dfs1(y,x);
sz[x]+=sz[y];
mx[x]=max(mx[x],mx[y]);
if(sz[y]>sz[son[x]]) son[x]=y;
}
if(mx[x]-dep[x]>=block) id[x]=++idx,mx[x]=dep[x];
}
int top[N];
void dfs2(int x,int tp){
top[x]=tp;
if(son[x]) dfs2(son[x],tp);
for(int y:v[x]){
if(y==fat[x]||y==son[x]) continue;
dfs2(y,y);
}
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fat[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
bitset<N> c[205][205],ans;
int st[N],tp,f[N];
void dfs3(int x,int fa){
for(int y:v[x]){
if(y==fa) continue;
if(id[y]){
for(int i=y;i!=st[tp];i=fat[i]) c[id[st[tp]]][id[y]].set(a[i]);
f[y]=st[tp];
st[++tp]=y;
}
dfs3(y,x);
if(id[y]) tp--;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
block=1000;
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i];
sort(b+1,b+n+1);
int len=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+len+1,a[i])-b;
for(int i=1;i<n;i++){
int x,y;cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs1(1,0);
dfs2(1,1);
if(!id[1]) id[1]=++idx;
st[++tp]=1;
dfs3(1,0);
int lst=0;
while(m--){
int x,y;cin>>x>>y;
x^=lst;ans.reset();
int p=lca(x,y);
while(x!=p&&!id[x]) ans.set(a[x]),x=fat[x];
while(y!=p&&!id[y]) ans.set(a[y]),y=fat[y];
if(x!=p){
int t=x;
while(dep[f[t]]>=dep[p]) ans|=c[id[f[t]]][id[t]],t=f[t];
while(t!=p) ans.set(a[t]),t=fat[t];
}
if(y!=p){
int t=y;
while(dep[f[t]]>=dep[p]) ans|=c[id[f[t]]][id[t]],t=f[t];
while(t!=p) ans.set(a[t]),t=fat[t];
}
ans.set(a[p]);
lst=ans.count();
cout<<lst<<'\n';
}
return 0;
}
:::
推荐题单
祝你成为下一个分块大神:
Ynoi 分块集合