题解 M弟娃

传送门

发现每个幻境可能贡献答案的总祭祀台的位置范围很好找
于是线段树维护即可,要求区间加和区间查max
卡常颇为严重,可以树剖lca+全局加1用偏移量实现卡过

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 300010
#define ll long long
#define fir first
#define sec second
#define make make_pair
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (f=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m;
int head[N], size;
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {e[++size].to=t; e[size].next=head[s]; head[s]=size;}

namespace force{
	int ans[N], fa[N][25], lg[N], dep[N];
	void dfs(int u, int pa) {
		for (int i=1; i<20; ++i)
			if (dep[u]>(1<<i)) fa[u][i]=fa[fa[u][i-1]][i-1];
			else break;
		for (int i=head[u],v; ~i; i=e[i].next) {
			v = e[i].to;
			if (v==pa) continue;
			dep[v]=dep[u]+1; fa[v][0]=u;
			dfs(v, u);
		}
	}
	int lca(int a, int b) {
		if (dep[a]<dep[b]) swap(a, b);
		while (dep[a]>dep[b]) a=fa[a][lg[dep[a]-dep[b]]-1];
		if (a==b) return a;
		for (int i=lg[dep[a]]-1; ~i; --i)
			if (fa[a][i]!=fa[b][i])
				a=fa[a][i], b=fa[b][i];
		return fa[a][0];
	}
	inline int dis(int a, int b) {return dep[a]+dep[b]-2*dep[lca(a, b)];}
	void calc(int rot, int a, int b) {
		int g=lca(a, b);
		// cout<<"lca: "<<a<<' '<<b<<' '<<g<<endl;
		int lim=min(dis(a, rot), dis(b, rot));
		int now=a;
		while (now!=g) {
			now=fa[now][0];
			if (now!=a&&now!=b) {
				if (dis(now, rot)<=lim) return ;
			}
		}
		now=b;
		while (now!=g) {
			now=fa[now][0];
			if (now!=a&&now!=b) {
				if (dis(now, rot)<=lim) return ;
			}
		}
		// cout<<"++ans: "<<rot<<endl;
		++ans[rot];
	}
	void solve() {
		dep[1]=1; dfs(1, 0);
		for (int i=1; i<=n; ++i) lg[i]=lg[i-1]+(1<<lg[i-1]==i);
		for (int i=1,a,b; i<=m; ++i) {
			a=read(); b=read();
			for (int j=1; j<=n; ++j) {
				calc(j, a, b);
			}
			int maxn=0;
			for (int j=1; j<=n; ++j) maxn=max(maxn, ans[j]);
			printf("%d\n", maxn);
		}
		exit(0);
	}
}

namespace task1{
	int fa[25][N], lg[N], id[N], siz[N], tot, dep[N], top[N], msiz[N], mson[N];
	int tl[N<<2], tr[N<<2], maxn[N<<2], tag[N<<2];
	#define tl(p) tl[p]
	#define tr(p) tr[p]
	#define maxn(p) maxn[p]
	#define tag(p) tag[p]
	#define pushup(p) maxn(p)=max(maxn(p<<1), maxn(p<<1|1))
	void spread(int p) {
		if (!tag(p)) return ;
		maxn(p<<1)+=tag(p); tag(p<<1)+=tag(p);
		maxn(p<<1|1)+=tag(p); tag(p<<1|1)+=tag(p);
		tag(p)=0;
	}
	void build(int p, int l, int r) {
		tl(p)=l; tr(p)=r;
		if (l==r) return ;
		int mid=(l+r)>>1;
		build(p<<1, l, mid);
		build(p<<1|1, mid+1, r);
	}
	void upd(int p, int l, int r, int val) {
		if (l<=tl(p)&&r>=tr(p)) {maxn(p)+=val; tag(p)+=val; return ;}
		spread(p);
		int mid=(tl(p)+tr(p))>>1;
		if (l<=mid) upd(p<<1, l, r, val);
		if (r>mid) upd(p<<1|1, l, r, val);
		pushup(p);
	}
	void dfs1(int u, int pa) {
		id[u]=++tot; siz[u]=1;
		for (int i=1; i<23; ++i)
			if (dep[u]>(1<<i)) fa[i][u]=fa[i-1][fa[i-1][u]];
			else break;
		for (int i=head[u],v; ~i; i=e[i].next) {
			v = e[i].to;
			if (v==pa) continue;
			dep[v]=dep[u]+1; fa[0][v]=u;
			dfs1(v, u); siz[u]+=siz[v];
			if (siz[v]>msiz[u]) msiz[u]=siz[v], mson[u]=v;
		}
	}
	void dfs2(int u, int f, int t) {
		top[u]=t;
		if (!mson[u]) return ;
		dfs2(mson[u], u, t);
		for (int i=head[u],v; ~i; i=e[i].next) {
			v = e[i].to;
			if (v==f || v==mson[u]) continue;
			dfs2(v, u, v);
		}
	}
	#ifndef TLE_OJ
	int lca(int a, int b) {
		while (top[a]!=top[b]) {
			if (dep[top[a]]<dep[top[b]]) swap(a, b);
			a=fa[0][top[a]];
		}
		if (dep[a]>dep[b]) swap(a, b);
		return a;
	}
	#else
	int lca(int a, int b) {
		if (dep[a]<dep[b]) swap(a, b);
		while (dep[a]>dep[b]) a=fa[lg[dep[a]-dep[b]]-1][a];
		if (a==b) return a;
		for (int i=lg[dep[a]]-1; ~i; --i)
			if (fa[i][a]!=fa[i][b])
				a=fa[i][a], b=fa[i][b];
		return fa[0][a];
	}
	#endif
	int lcs(int a, int b) {
		int dlt=dep[b]-dep[a]-1;
		for (int i=lg[dlt]; ~i; --i)
			if (dlt>=(1<<i)) b=fa[i][b], dlt-=1<<i;
		return b;
	}
	void solve() {
		dep[1]=1; dfs1(1, 0); dfs2(1, 0, 1);
		build(1, 1, tot);
		int dlt=0;
		for (int i=1; i<=n; ++i) lg[i]=lg[i-1]+(1<<lg[i-1]==i);
		for (int i=1,a,b; i<=m; ++i) {
			a=read(); b=read();
			int g=lca(a, b);
			if (a==b) ++dlt;
			else if (g==a||g==b) {
				if (g!=a) swap(a, b);
				upd(1, id[b], id[b]+siz[b]-1, 1);
				++dlt;
				int s=lcs(a, b);
				// cout<<"lcs: "<<a<<' '<<b<<' '<<s<<endl;
				upd(1, id[s], id[s]+siz[s]-1, -1);
			}
			else {
				upd(1, id[a], id[a]+siz[a]-1, 1);
				upd(1, id[b], id[b]+siz[b]-1, 1);
			}
			printf("%d\n", maxn(1)+dlt);
		}
		exit(0);
	}
}

signed main()
{
	freopen("magic.in", "r", stdin);
	freopen("magic.out", "w", stdout);

	memset(head, -1, sizeof(head));
	n=read(); m=read();
	for (int i=1,u,v; i<n; ++i) {
		u=read(); v=read();
		add(u, v); add(v, u);
	}
	// force::solve();
	task1::solve();

	return 0;
}
上一篇:LG 题解 P1357 花园


下一篇:对数函数