zoukankan      html  css  js  c++  java
  • [Luogu4074][WC2013]糖果公园

    BZOJ权限题!提供洛谷链接

    sol

    树上带修改莫队
    很显然吧。对吧。
    所以说树上莫队要怎么写呢?
    我们知道莫队=给区间排序+依次暴力处理,所以对于树上莫队而言也是一样的。
    序列莫队基于序列分块(也就是直接(sqrt{n})一块),而树上莫队则基于树分块。
    所以说树分块是什么?
    这里提供一份代码至于为什么是这样写请自己YY

    void dfs(int u,int f)
    {
    	int ttp=tp;
    	for (int e=head[u];e;e=a[e].next)
    	{
    		int v=a[e].to;if (v==f) continue;
    		dfs(v,u);
    		if (tp-ttp>=block) {++ccnt;while (tp>ttp) bl[s[tp--]]=ccnt;}
    	}
    	s[++tp]=u;
    }
    

    其中(block)是分的块的大小。


    我们现在来看树上莫队
    对于一组询问((u,v)),我们希望把这条路经上的信息累计到答案中。设(S(u,v))表示(u)(v)路径上的所有点。

    有一个这样的东西
    (S(u,v)=S(root,u)) xor (S(root,v)) xor (lca(u,v))
    发现(lca(u,v))多出来了不好搞,就把它搞掉,重新定义
    (T(u,v)=S(root,u)) xor (S(root,v))
    所以(S(u,v)=T(u,v)) xor (lca(u,v))
    考虑(T(u_1,v_1))如何转移到(T(u_2,v_2))
    (T(u_1,v_1)) xor (T(u_2,v_2)=S(root,u_1)) xor (S(root,v_1)) xor (S(root,u_2)) xor (S(root,v_2))
    (=T(u_1,u_2)) xor (T(v_1,v_2))
    所以如果(u_1,u_2)很近,(v_1,v_2)很近,那么移动耗费的复杂度就很小了。对于每个询问这样移动,然后(lca(u,v))就单独拿出来处理就行了。
    所以我们树分块以后,把所有询问按照u所在的块第一关键字v所在的块第二关键字排序。若带修改就把修改版本作为第三关键字。
    至于剩下的部分,就怎么暴力怎么来吧。

    code

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define ll long long
    const int N = 100005;
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    struct edge{int to,next;}a[N<<1];
    int n,block,m,q,v[N],w[N],col[N],head[N],cnt;
    int fa[N],dep[N],sz[N],son[N],top[N],dfn[N];//树链剖分
    int ccnt,bl[N],s[N],tp;//树分块
    int cnt1,cnt2,pre[N],vis[N],tot[N];//莫队
    struct query{
    	int u,v,t,id;
    	bool operator < (const query &b) const
    		{
    			if (bl[u]==bl[b.u]&&bl[v]==bl[b.v]) return t<b.t;
    			if (bl[u]==bl[b.u]) return bl[v]<bl[b.v];
    			return bl[u]<bl[b.u];
    		}
    }q1[N];
    struct xiugai{int pos,val,pre;}q2[N];
    ll Ans,ans[N];
    void dfs1(int u,int f)
    {
    	fa[u]=f;dep[u]=dep[f]+1;sz[u]=1;
    	for (int e=head[u];e;e=a[e].next)
    	{
    		int v=a[e].to;if (v==f) continue;
    		dfs1(v,u);
    		sz[u]+=sz[v];if (sz[v]>sz[son[u]]) son[u]=v;
    	}
    }
    void dfs2(int u,int up)
    {
    	top[u]=up;dfn[u]=++cnt;int ttp=tp;
    	if (son[u]) dfs2(son[u],up);
    	if (tp-ttp>=block) {ccnt++;while (tp>ttp) bl[s[tp--]]=ccnt;}
    	for (int e=head[u];e;e=a[e].next)
    	{
    		int v=a[e].to;if (v==fa[u]||v==son[u]) continue;
    		dfs2(v,v);
    		if (tp-ttp>=block) {ccnt++;while (tp>ttp) bl[s[tp--]]=ccnt;}
    	}
    	s[++tp]=u;
    }
    int lca(int u,int v)
    {
    	while (top[u]^top[v])
    	{
    		if (dep[top[u]]<dep[top[v]]) swap(u,v);
    		u=fa[top[u]];
    	}
    	return dep[u]<dep[v]?u:v;
    }
    void update(int x)//莫队单点修改
    {
    	if (!vis[x]) vis[x]=1,Ans+=(ll)v[col[x]]*w[++tot[col[x]]];
    	else vis[x]=0,Ans-=(ll)v[col[x]]*w[tot[col[x]]--];
    }
    void modify(int x,int v)//带修改莫队的版本修改
    {
    	if (!vis[x]) col[x]=v;
    	else update(x),col[x]=v,update(x);
    }
    void change(int u,int v)//路径修改
    {
    	while (u^v)
    		if (dep[u]>dep[v]) update(u),u=fa[u];
    		else update(v),v=fa[v];
    }
    int main()
    {
    	n=gi();block=pow(n,0.6);m=gi();q=gi();
    	for (int i=1;i<=m;i++) v[i]=gi();
    	for (int i=1;i<=n;i++) w[i]=gi();
    	for (int i=1,u,v;i<n;i++)
    	{
    		u=gi(),v=gi();
    		a[++cnt]=(edge){v,head[u]};head[u]=cnt;
    		a[++cnt]=(edge){u,head[v]};head[v]=cnt;	
    	}
    	for (int i=1;i<=n;i++) pre[i]=col[i]=gi();
    	dfs1(1,0);cnt=0;dfs2(1,1);
    	while (tp) bl[s[tp--]]=ccnt;
    	for (int i=1,type,x,y;i<=q;i++)
    	{
    		type=gi();x=gi();y=gi();
    		if (type==0)
    			q2[++cnt2]=(xiugai){x,y,pre[x]},pre[x]=y;
    		else
    		{
    			if (dfn[x]>dfn[y]) swap(x,y);
    			q1[++cnt1]=(query){x,y,cnt2,cnt1};
    		}
    	}
    	sort(q1+1,q1+cnt1+1);cnt2=q1[1].t;
    	for (int i=1;i<=cnt2;i++) modify(q2[i].pos,q2[i].val);
    	change(q1[1].u,q1[1].v);
    	int gg=lca(q1[1].u,q1[1].v);
    	update(gg);ans[q1[1].id]=Ans;update(gg);
    	for (int i=2;i<=cnt1;i++)
    	{
    		while (cnt2<q1[i].t) cnt2++,modify(q2[cnt2].pos,q2[cnt2].val);
    		while (cnt2>q1[i].t) modify(q2[cnt2].pos,q2[cnt2].pre),cnt2--;
    		change(q1[i].u,q1[i-1].u);change(q1[i].v,q1[i-1].v);
    		gg=lca(q1[i].u,q1[i].v);
    		update(gg);ans[q1[i].id]=Ans;update(gg);
    	}
    	for (int i=1;i<=cnt1;i++) printf("%lld
    ",ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    IDL读取TXT文件并写入二维数组中【转】
    远程连接ArcSDE
    Silverlight项目启动出现System.NullReferenceException未将对象引用到对象实例
    ENVI扩展工具:HDF5 Browser
    READF: End of file encountered. Unit: 100
    ENVI4.8下从两幅分类结果的栅格图中计算土地利用类型转换矩阵
    IDL中去掉数组中相同的元素方法
    利用IDL程序自动添加ENVI菜单【转】
    WIN7远程桌面连接知识
    对COM组件的调用返回了错误"HRESULT E_FAIL”的错误分析(c#与IDL混合编程)转
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8316286.html
Copyright © 2011-2022 走看看