LG P4074 [WC2013] 糖果公园(带修莫队,树上莫队)

LG P4074 [WC2013] 糖果公园

Solution

树上带修莫队,主要还是复习带修莫队和树上莫队。

带修莫队:

  • 带修莫队要先对 l l l分块的序号作为第一关键字,对 r r r分块的序号作为第二关键字,时间 t t t作为第三关键字排序,然后正常莫队。
  • 块大小约取 O ( n 2 3 ) O(n^{\frac{2}{3}}) O(n32)最优,时间复杂度为 O ( n 5 3 ) O(n^{\frac{5}{3}}) O(n35)

树上莫队:

  • 树上莫队需要按路径端点 d f s dfs dfs序为区间端点做莫队,注意这里的 d f s dfs dfs序要记录 d f n dfn dfn f n s fns fns,也就是形成一个长度为 2 n 2n 2n的访问序列。
  • 然后对于询问 ( x , y ) (x,y) (x,y),假设此时 d f n x < d f n y dfn_x<dfn_y dfnx<dfny,若 L C A ( x , y ) = x LCA(x,y)=x LCA(x,y)=x,则莫队区间为 [ d f n x , d f n y ] [dfn_x,dfn_y] [dfnx,dfny],否则莫队区间为 [ f n s x , d f n y ] [fns_x,dfn_y] [fnsx,dfny]。注意当 L C A ( x , y ) ≠ x LCA(x,y)\not =x LCA(x,y)=x的这种情况 l c a lca lca不会被统计到答案中,需要另外把 l c a lca lca加进去计算。
  • 时间复杂度 O ( n n ) O(n\sqrt n) O(nn )

这题大概把上面两个莫队套一起就行了。

Code

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ll ans=0,Ans[MAXN];
vector<int> e[MAXN];
int id[MAXN],dfn[MAXN],fns[MAXN],dep[MAXN],fa[MAXN][20],DFN=0;
int cnt[MAXN],c[MAXN],V[MAXN],w[MAXN],Log[MAXN],bel[MAXN],flag[MAXN];
struct Qnode{ int l,r,lca,id,t; } Q[MAXN];
struct Cnode{ int x,frm,to; } C[MAXN];
void dfs(int x,int father)
{
	id[dfn[x]=++DFN]=x,dep[x]=dep[father]+1,fa[x][0]=father;
	for (int i=1;i<=Log[dep[x]];i++) fa[x][i]=fa[fa[x][i-1]][i-1];
	for (auto v:e[x]) if (v!=father) dfs(v,x);
	id[fns[x]=++DFN]=x;
}
int getlca(int x,int y)
{
	if (dep[x]<dep[y]) swap(x,y);
	for (int i=Log[dep[x]];i>=0;i--)
		if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
	if (x==y) return x;
	for (int i=Log[dep[x]];i>=0;i--)
		if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

void add(int x) 
{ 
	flag[x]^=1;
	if (flag[x]) cnt[c[x]]++,ans+=1ll*w[cnt[c[x]]]*V[c[x]]; 
	else ans-=1ll*w[cnt[c[x]]]*V[c[x]],cnt[c[x]]--;
}
signed main()
{
	int n=read(),m=read(),q=read(),sz=pow(n,0.6),Qnum=0,Cnum=0;
	for (int i=1;i<=n*2;i++) bel[i]=(i-1)/sz+1;  
	for (int i=1;i<=m;i++) V[i]=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1,u,v;i<n;i++) u=read(),v=read(),e[u].PB(v),e[v].PB(u);
	for (int i=1;i<=n;i++) c[i]=read();
	
	dep[0]=-1,Log[1]=0;
	for (int i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
	dfs(1,0);
		
	for (int i=1;i<=q;i++) 
	{
		int opt=read(),x=read(),y=read();
		if (opt==1)
		{
			int lca=getlca(x,y);
			if (dfn[x]>dfn[y]) swap(x,y);
			if (lca==x) Q[++Qnum]=(Qnode){dfn[x],dfn[y],0,Qnum,Cnum};
			else Q[++Qnum]=(Qnode){fns[x],dfn[y],lca,Qnum,Cnum};
		}
		else C[++Cnum]=(Cnode){x,c[x],y},c[x]=y;
	}
	sort(Q+1,Q+Qnum+1,[&](Qnode x,Qnode y){ return (bel[x.l]<bel[y.l])||(bel[x.l]==bel[y.l]&&bel[x.r]<bel[y.r])||(bel[x.l]==bel[y.l]&&bel[x.r]==bel[y.r]&&x.t<y.t); });
	for (int i=1,t=Cnum,l=1,r=0;i<=Qnum;i++)
	{
		while (t<Q[i].t) 
		{ 
			++t; 
			if (flag[C[t].x]) add(C[t].x),c[C[t].x]=C[t].to,add(C[t].x);
			else c[C[t].x]=C[t].to;
		}
		while (t>Q[i].t)
		{
			if (flag[C[t].x]) add(C[t].x),c[C[t].x]=C[t].frm,add(C[t].x);
			else c[C[t].x]=C[t].frm;
			--t;
		}
		while (l<Q[i].l) add(id[l++]);
		while (l>Q[i].l) add(id[--l]);
		while (r<Q[i].r) add(id[++r]);
		while (r>Q[i].r) add(id[r--]);
		if (Q[i].lca) add(Q[i].lca);
		Ans[Q[i].id]=ans;
		if (Q[i].lca) add(Q[i].lca);
	}
	for (int i=1;i<=Qnum;i++) printf("%lld\n",Ans[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值