P8496 [NOI2022] 众数

· · 题解

维护 n 个序列,初始给定,进行 q 次操作:单个序列末位加、删元素,合并序列,查询若干序列合并后的绝对众数。

人类智慧。 首先考虑维护 $n$ 个链表以及动态开点线段树,线段树维护当前序列的众数以及众数在该序列的出现次数(如有多个则任意都可)。很容易处理末位加、删元素。 合并序列时,把链表和线段树分别都合并,也不困难。主要考虑怎么查询。 注意到,如果一个集合存在绝对众数,则随机选择一个数,选到它的概率 $>\frac{1}{2}$,所以我们选 $x$ 次,判定失误概率为指数级分之一。 于是我们先尝试“众数的出现次数最多”的前 $15$ 个序列的众数,看看这些众数是否在查询的所有序列中出现的次数和超过一半。 然后再随机选择 $15$ 个序列的众数,看看这些众数是否在询问的所有序列中出现次数和超过一半。 需要快速的读写。 时间复杂度 $\mathcal{O}(N\log N\times 30)$(当作 $N,M$ 同阶来计算),最慢的点 $700\textrm{ms}$。 卡不掉你(我构造了半天都没卡掉自己)。 **展示[代码](https://www.luogu.com.cn/record/271335303):** ```cpp int n,q,root[N]; struct node{ int lc,rc,sum,mx,id; }t[N<<4]; int tot; void add(int&p,int l,int r,int x,int c){ if(!p) p=++tot; if(l==r){ t[p].sum+=c; t[p].mx=t[p].sum; t[p].id=l; return; } int mid=l+r>>1; if(x<=mid) add(t[p].lc,l,mid,x,c); else add(t[p].rc,mid+1,r,x,c); t[p].sum=t[t[p].lc].sum+t[t[p].rc].sum; t[p].mx=max(t[t[p].lc].mx,t[t[p].rc].mx); t[p].id=(t[t[p].lc].mx==t[p].mx)?t[t[p].lc].id:t[t[p].rc].id; } int merge(int p1,int p2,int l,int r){ if(!p1 or !p2)return p1+p2; if(l==r){ t[p1].sum+=t[p2].sum; t[p1].mx=t[p1].sum; return p1; } int mid=(l+r)>>1; t[p1].lc=merge(t[p1].lc,t[p2].lc,l,mid); t[p1].rc=merge(t[p1].rc,t[p2].rc,mid+1,r); t[p1].sum=t[t[p1].lc].sum+t[t[p1].rc].sum; t[p1].mx=max(t[t[p1].lc].mx,t[t[p1].rc].mx); t[p1].id=(t[t[p1].lc].mx==t[p1].mx)?t[t[p1].lc].id:t[t[p1].rc].id; return p1; } int ask(int p,int l,int r,int x){ if(!p)return 0; if(l==r)return t[p].sum; int mid=l+r>>1; if(x<=mid)return ask(t[p].lc,l,mid,x); if(x>mid)return ask(t[p].rc,mid+1,r,x); } int tail[N],from[N],to[N],all,val[N],head[N]; int pb(int x,int y){ to[tail[x]]=++all,from[all]=tail[x], tail[x]=all,val[all]=y; if(!head[x])head[x]=all; return all; } int del(int x){ int ret=val[tail[x]]; tail[x]=from[tail[x]]; to[tail[x]]=0; if(!tail[x])head[x]=0; return ret; } struct popcorn{ int x,y,z; bool operator<(const popcorn&p)const{ return y>p.y; } }a[N]; signed main(){ n=read(),q=read(); for(int i=1;i<=n;++i){ int m=read(); while(m--){ int x=read(); pb(i,x); add(root[i],1,n+q,x,1); } } for(int T=1;T<=q;++T){ int op=read(); if(op==1){ int x=read(),y=read(); pb(x,y),add(root[x],1,n+q,y,1); }else if(op==2){ int x=read(); add(root[x],1,n+q,del(x),-1); }else if(op==3){ int m=read(),use=0,cat=0,ans=-1; for(int i=1;i<=m;++i) a[i].x=read(), a[i].y=t[root[a[i].x]].mx, a[i].z=t[root[a[i].x]].id, cat+=t[root[a[i].x]].sum; sort(a+1,a+m+1); for(int i=1;i<=min(m,15) and ans==-1;++i){ int cot=0,g=a[i].z; for(int j=1;j<=m;++j) cot+=ask(root[a[j].x],1,n+q,g); if(cot*2>cat) ans=g; } for(int gg=1;gg<=15 and ans==-1;++gg){ int cot=0,g=a[1ll*rand()*rand()%m+1].z; for(int j=1;j<=m;++j) cot+=ask(root[a[j].x],1,n+q,g); if(cot*2>cat) ans=g; } write(ans),putchar(10); }else if(op==4){ int x=read(),y=read(),z=read(); root[z]=merge(root[x],root[y],1,n+q); if(!head[x]) head[z]=head[y], tail[z]=tail[y]; else if(!head[y]) head[z]=head[x], tail[z]=tail[x]; else head[z]=head[x], tail[z]=tail[y], from[head[y]]=tail[x], to[tail[x]]=head[y]; } } return 0; } ```