BZOJ 4129

http://www.lydsy.com/JudgeOnline/problem.php?id=4129

还是树上带修莫队,

只不过要离散化一下,

对于离散化后的值域还要分块一下

每次询问可以进行分块查询

复杂度O(n√n)

#include<cstdio>
#include<algorithm>
#include<cmath>
#define VIS(now) for(register int e=las[now];e;e=nxt[e])
#define FOR(i,s,t) for(register int i=s;i<=t;++i)
#define up(l,r) for(register int j=l;j!=r;j=fa[j])
using std::swap;
using std::lower_bound;
using std::sort;
using std::unique;
const int N=200011;
int n,m,blo,dfn_num,t,l,r,x,y,tot,times,now,lca;
int las[N],nxt[N],to[N],a[N],b[N<<1];
int cnt[N<<1],g[N],bo[N],ans[N]; 
int xu[N],top[N],sz[N],dep[N],fa[N];
struct qs{
	int l,r,t,id;
	inline bool operator<(qs A)const{
		if(xu[l]/blo!=xu[A.l]/blo)return xu[l]<xu[A.l];
		if(xu[r]/blo!=xu[A.r]/blo)return xu[r]<xu[A.r];
		if(t/blo!=A.t/blo)return t<A.t;
		return id<A.id;
	}
}q[N];
struct cg{int pos,num;}c[N];
inline void add(int x,int y){
	nxt[++tot]=las[x];las[x]=tot;to[tot]=y;
}
inline void dfs1(int now){
	sz[now]=1;
	VIS(now)
		if(!dep[to[e]]){
			dep[to[e]]=dep[now]+1;
			fa[to[e]]=now;
			dfs1(to[e]);
			sz[now]+=sz[to[e]];
		}
}
inline void dfs2(int now,int chain){
	xu[now]=++dfn_num;
	top[now]=chain;
	register int i=0;
	VIS(now)if(sz[to[e]]>sz[i]&&to[e]!=fa[now])i=to[e];
	if(!i)return;
	dfs2(i,chain);
	VIS(now)if(to[e]!=i&&to[e]!=fa[now])dfs2(to[e],to[e]);
}
inline int LCA(int x,int y){
	for(;top[x]!=top[y];dep[top[x]]>dep[top[y]]?x=fa[top[x]]:y=fa[top[y]]);
	return dep[x]<dep[y]?x:y;
}
inline void disc_init(){
	sort(b+1,b+b[0]+1);
	b[0]=unique(b+1,b+b[0]+1)-b-1; 
	FOR(i,1,n)a[i]=lower_bound(b+1,b+b[0]+1,a[i])-b;
	FOR(i,1,times)c[i].num=lower_bound(b+1,b+b[0]+1,c[i].num)-b;
}
inline void add(int now){
	int col;
	bo[now]^=1;
	col=a[now];
	if(bo[now]){if(!cnt[col])++g[(col-1)/blo+1];++cnt[col];}
	else{--cnt[col];if(!cnt[col])--g[(col-1)/blo+1];}
} 
inline void change(int now){
	int pos=c[now].pos;
	int col;
	if(bo[pos]){
		col=a[pos];--cnt[col];if(!cnt[col])--g[(col-1)/blo+1];
		col=c[now].num;if(!cnt[col])++g[(col-1)/blo+1];++cnt[col];
	}
	swap(a[pos],c[now].num);
}
inline int query(){
	register int i;
	for(i=1;g[i]==blo;++i);
	for(register int j=(i-1)*blo+1;;++j)
		if(!cnt[j])return j;
}
int main(){
	scanf("%d%d",&n,&m);
	blo=pow(n,2.00/3.00);
	b[++b[0]]=0;
	FOR(i,1,n){
		scanf("%d",a+i);
		b[++b[0]]=a[i];b[++b[0]]=a[i]+1;
	}
	FOR(i,2,n){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
	dep[1]=1;dfs1(1);dfs2(1,1);
	tot=0;
	FOR(i,1,m){
		scanf("%d%d%d",&t,&l,&r);
		if(t==1){
			if(xu[l]>xu[r])swap(l,r);
			q[++tot]=(qs){l,r,times,tot};		
		}
		else{
			c[++times]=(cg){l,r};
			b[++b[0]]=r;
			b[++b[0]]=r+1;
		}
	}
	disc_init();
	sort(q+1,q+tot+1);
	l=1;r=1;now=0;
	FOR(i,1,tot){
		lca=LCA(l,q[i].l);up(l,lca)add(j);up(q[i].l,lca)add(j);
		lca=LCA(r,q[i].r);up(r,lca)add(j);up(q[i].r,lca)add(j);
		lca=LCA(q[i].l,q[i].r);add(lca);
		while(now<q[i].t)change(++now);while(q[i].t<now)change(now--);
		ans[q[i].id]=query();add(lca);
		l=q[i].l;r=q[i].r;
	}
	FOR(i,1,tot)printf("%d\n",b[ans[i]]);
	return 0;
}

  

转载于:https://www.cnblogs.com/Stump/p/7940281.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值