WA50求助
查看原帖
WA50求助
596437
Raisen_zx楼主2024/7/20 11:27

修改撤回挂了,有没有大佬来解释一下为什么swap 会挂

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cctype>
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
using namespace std;
char buf[1<<20],*p1=buf,*p2=buf;
template<typename type>
inline void read(type &x)
{
    x=0;bool flag=0;char ch=gc();
    while(!isdigit(ch)) flag=ch=='-',ch=gc();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
    flag?x=-x:0;
}
const int MAXN=1e5+10;
typedef long long ll;
int n,m,q,B,blo[MAXN],cntm,cntq,cnt[MAXN],cntb,top[MAXN],fa[MAXN],siz[MAXN],dep[MAXN],son[MAXN],stk[MAXN],tail,head[MAXN],cnte,c[MAXN];
ll v[MAXN],w[MAXN],ans[MAXN],res;
bool vis[MAXN];
struct Edge
{
	int to,nxt;
}e[MAXN<<1];
struct QUERY
{
	int u,v,id,t;
	inline bool operator<(const QUERY &x)const
	{
		if(blo[u]!=blo[x.u]) return blo[u]<blo[x.u];
		if(blo[v]!=blo[x.v]) return blo[v]<blo[x.v];
		return t<x.t;
	}
}qry[MAXN];
struct MODIFY
{
	int pos,val;
}md[MAXN];
inline void link(int u,int v)
{
	e[++cnte]={v,head[u]};
	head[u]=cnte;
}
void dfs1(int u,int f)
{
	int tmp=tail;
	fa[u]=f,dep[u]=dep[f]+1,siz[u]=1;
	for(int i=head[u],v;i;i=e[i].nxt)
	{
		v=e[i].to;
		if(v==f) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
		if(tail-tmp>B)
		{
			cntb++;
			while(tail!=tmp) blo[stk[tail--]]=cntb;
		}
	}
	stk[++tail]=u;
}
void dfs2(int u,int t)
{
	top[u]=t;
	if(son[u]) dfs2(son[u],t);
	for(int i=head[u],v;i;i=e[i].nxt)
	{
		v=e[i].to;
		if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
	}
}
inline int LCA(int u,int v)
{
	while(top[u]!=top[v])
	{
		if(dep[top[u]]>dep[top[v]]) u=fa[top[u]];
		else v=fa[top[v]];
	}
	return dep[u]<dep[v]?u:v;
}
inline void add(int x)
{
	cnt[x]++;
	res+=v[x]*w[cnt[x]];
}
inline void del(int x)
{
	res-=v[x]*w[cnt[x]];
	cnt[x]--;
}
inline void update(int x)
{
	if(vis[x]) del(c[x]),vis[x]=0;
	else add(c[x]),vis[x]=1;
}
inline void mdf(MODIFY t)
{
	if(vis[t.pos])
	{
		del(c[t.pos]);
		swap(c[t.pos],t.val);
		add(c[t.pos]);
	}
	else swap(c[t.pos],t.val);
}
inline void move(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	while(dep[x]>dep[y])
	{
		update(x);
		x=fa[x];
	}
	while(x!=y)
	{
		update(x),update(y);
		x=fa[x],y=fa[y];
	}
}
int main()
{
	read(n),read(m),read(q);
	B=pow(n,2.0/3.0),res=0;
	for(int i=1;i<=m;++i) read(v[i]);
	for(int i=1;i<=n;++i) read(w[i]);
	for(int i=1,a,b;i<n;++i)
	{
		read(a),read(b);
		link(a,b),link(b,a);
	}
	for(int i=1;i<=n;++i)
	{
		read(c[i]);
	}
	for(int i=1,op;i<=q;++i)
	{
		read(op);
		if(op==1)
		{
			read(qry[++cntq].u),read(qry[cntq].v);
			qry[cntq].id=cntq,qry[cntq].t=cntm;
		}
		else
		{
			read(md[++cntm].pos),read(md[cntm].val);
		}
	}
	dfs1(1,0);
	while(tail>0) blo[stk[tail--]]=cntb;
	dfs2(1,1);
	// for(int i=1;i<=n;++i) printf("%d\n",blo[i]);
	sort(qry+1,qry+cntq+1);
	int u=1,v=1,t=0;
	update(1);
	for(int i=1;i<=cntq;++i)
	{
		update(LCA(u,v));
		if(u!=qry[i].u) move(u,qry[i].u),u=qry[i].u;
		if(v!=qry[i].v) move(v,qry[i].v),v=qry[i].v;
		while(t<qry[i].t) t++,mdf(md[t]);
		while(t>qry[i].t) mdf(md[t]),t--;
		update(LCA(u,v));
		ans[qry[i].id]=res;
	}
	for(int i=1;i<=cntq;++i) printf("%lld\n",ans[i]);
	return 0;
}
2024/7/20 11:27
加载中...