跳到正文

个人记录

模板合集

平衡树森林约 42 分钟
原文
文章信息与版本

文章信息

发布
2020年6月19日
收录
2025年12月12日
更新
2026年8月14日
篇幅
约 42 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;

//0.0 前置------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

const LL maxn=100000+10;
const LL maxm=100000+10;
const LL INF=1e12; 

//0.1 图/树------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

struct edge{
	LL v,w,next;
}E[maxm*2];
LL head[maxn],Ecnt;

void addedge(LL u,LL v,LL w)
{
	E[++Ecnt].v=v;
	E[Ecnt].w=w;
	E[Ecnt].next=head[u];
	head[u]=Ecnt;
}

//1.最短路径:dijkstra------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LL d[maxn];
void dijkstra(LL S)
{
	fill(d,d+maxn,INF);
	priority_queue<pair<LL,LL>,vector<pair<LL,LL> >,greater<pair<LL,LL> > > q;
	d[S]=0;
	q.push(pair<LL,LL>(0,S));
	while (!q.empty())
	{
		pair<LL,LL> p=q.top(); q.pop();
		LL u=p.second;
		if (p.first>d[u]) continue;
		for (LL i=head[u];~i;i=E[i].next)
		{
			LL v=E[i].v,w=E[i].w;
			if (d[u]+w<d[v])
			{
				d[v]=d[u]+w;
				q.push(pair<LL,LL>(0,S));
			}
		}
	}
}

//2.强连通分量:tarjan------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL dfn[maxn],low[maxn],vis[maxn],cnt,col[maxn],colnum;
stack<LL> s;
void tarjan(LL u)
{
	cnt++;
	dfn[u]=low[u]=cnt;
	vis[u]=true;
	s.push(u);
	for (LL i=head[u];~i;i=E[i].next)
	{
		LL v=E[i].v;
		if (!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if (vis[v]) low[u]=min(low[u],dfn[v]);
	}
	if (dfn[u]==low[u])
	{
		colnum++;
		while (s.top()!=u)
		{
			LL v=s.top();s.pop();
			col[v]=colnum;
			vis[v]=0;
		}
		s.pop();
		col[u]=colnum;
		vis[u]=0;
	}
} 

//3.并查集------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL p[maxn];
void init(LL n){for (LL i=1;i<=n;i++) p[i]=i;}
LL find(LL x){return p[x]==x?x:p[x]=find(p[x]);}
void unite(LL x,LL y)
{
	LL a=find(x),b=find(y);
	if (a==b) return;
	p[a]=b;
}
LL same(LL x,LL y){return find(x)==find(y);}

//4.数论>.<------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
inv[1]=1;
inv[i]=(p-p/i)*inv[p%i]%p;

LL qmod(LL a,LL b,LL mod)
{
	LL ans=1;
	while (b)
	{
		if (b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}

LL exgcd(LL a,LL b,LL &x,LL &y)
{
	if (b==0)
	{
		x=1; y=0;
		return a;
	}
	LL r=exgcd(a,b,x,y);
	LL t=x; x=y; y=t-a/b*y;
	return r;
}

LL mark[maxn],primes[maxn],cnt;
void make()
{
	mark[1]=1;
	for (LL i=2;i<maxn;i++)
	{
		if (!mark[i]) primes[++cnt]=i;
		for (LL j=1;j<=cnt && i*primes[j]<maxn;j++)
		{
			mark[i*primes[j]]=1;
			if (i%primes[j]==0) break;
		}
	}
}
//5.高斯消元------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
double a[maxn][maxn],x[maxn];
LL n;

LL gauss()
{
	LL res=0;
	for (LL i=1;i<n;i++)
	{
		if (abs(a[i][i])<0.0000001) 
		{
			for (LL j=i+1;j<=n+1;j++)
			{
				if (abs(a[j][i])<0.0000001) continue;
				for (LL k=1;k<=n+1;k++) swap(a[i][k],a[j][k]);
				break;
			}
			if (abs(a[i][i])<0.0000001) continue;
		}
		for (LL j=i+1;j<=n+1;j++) a[i][j]/=a[i][i];
		a[i][i]=1;
		for (LL j=i+1;j<=n;j++)
		{
			double r=a[j][i];
			for (LL k=i;k<=n+1;k++) a[j][k]-=a[i][k]*r;
		}
	}
	for (LL i=n;i>=1;i--)
	{
		if (a[i][i]==0 && a[i][n+1]==0) res++;
		else if (a[i][i]==0 && a[i][n+1]!=0) return -1;
		else x[i]=a[i][n+1]/a[i][i];
		for (LL j=1;j<i;j++) a[j][n+1]-=x[i]*a[j][i],a[j][i]=0;
	}
	return res;
}

//6. 线段树------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL sp[maxn*4],add[maxn*4];

void up(LL id) 
{
	sp[id]=sp[id*2]+sp[id*2+1];
}

void down(LL id,LL l,LL r)
{
	if (add[id])
	{
		add[id*2]+=add[id];
		add[id*2+1]+=add[id];
		LL mid=(l+r)/2;
		sp[id*2]+=(mid-l+1)*add[id];
		sp[id*2+1]+=(r-mid)*add[id];
		add[id]=0;
	}
}

void updata(LL id,LL l,LL r,LL x,LL y,LL v)
{
	if (x<=l && r<=y)
	{
		sp[id]+=(r-l+1)*v;
		add[id]+=v;
	}
	down(id,l,r);
	LL mid=(l+r)/2;
	if (x<=mid) updata(id*2,l,mid,x,y,v);
	if (y>mid) updata(id*2+1,mid+1,r,x,y,v);
	up(id);
}

LL query(LL id,LL l,LL r,LL ql,LL qr)
{
	if (ql<=l && r<=qr)	return sp[id];
	down(id,l,r);
	LL mid=(l+r)/2,ans=0;
	if (ql<=mid) ans+=query(id*2,l,mid,ql,qr);
	if (qr>mid) ans+=query(id*2+1,mid+1,r,ql,qr);
	return ans;
}

//7.倍增LCA------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL p[maxn][20];
LL dep[maxn];

void dfs(LL u,LL fa)
{
	p[u][1]=fa;
	for (LL i=1;i<20;i++) 
	{
		if (p[u][i]>0) p[u][i+1]=p[p[u][i]][i];
		else p[u][i+1]=-1;
	}
	for (LL i=head[u];~i;i=E[i].next)
	{
		LL v=E[i].v;
		if (v==fa) continue;
		dep[v]=dep[u]+1;
		dfs(v,u);
	}
} 

LL lca(LL u,LL v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (LL i=20;i>0;i--)
	{
		if (p[u][i]>0 && dep[p[u][i]]>=dep[v]) u=p[u][i];
		if (u==v) return u;
	}
	for (LL i=20;i>0;i--)
	{
		if (p[u][i]!=p[v][i]) u=p[u][i],v=p[v][i];
	}
	return p[u][1];
}

//8.ST表------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL a[maxn];
LL st[20][maxn];
LL n;
LL log[maxn];

void init()
{
	for (LL i=1;i<=n;i++) st[0][i]=a[i];
	for (LL i=2;i<=n;i++) log[i]=log[i/2]+1;
	for (LL i=1;1<<i<=n;i++)
	{
		for (LL j=1;j+(1<<i)-1<=n;j++)
		{
			st[i][j]=max(st[i-1][j],st[i-1][j+(1<<(i-1))]);
		}
	}
}

LL rmq(LL l,LL r)
{
	LL Log=log[r-l+1];
	LL a,b;
	a=st[Log][l];
	b=st[Log][r-(1<<Log)+1];
	return max(a,b);
}

//9.AC自动机------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct ACT{
	LL ch[maxn][26],f[maxn],last[maxn];
	LL val[maxn],siz;
	ACT(){memset(ch[0],0,sizeof(ch[0]));val[0]=0;siz=1;}
	LL id(char c){return c-'a';}
	void insert(string s,LL v)
	{
		LL u=0;
		for (LL i=0;i<s.size();i++)
		{
			LL c=id(s[i]);
			if (!ch[u][c])
			{
				memset(ch[siz],0,sizeof(ch[siz]));
				val[siz]=0;
				ch[u][c]=siz;
				siz++;
			}
			u=ch[u][c];
		}
		val[u]=v;
	}
	void init()
	{
		queue<LL> q;
		f[0]=0;
		for (LL i=0;i<26;i++)
		{
			LL u=ch[0][i];
			if (!u) continue;
			f[u]=0; q.push(u); last[u]=0;
		}
		while (!q.empty())
		{
			LL u=q.front(); q.pop();
			for (LL i=0;i<26;i++)
			{
				LL v=ch[u][i];
				if (!v)
				{
					ch[u][i]=ch[f[u]][i];
					continue;
				}
				q.push(v);
				f[v]=ch[f[u]][i];
				last[v]=val[v]?f[v]:last[f[v]];
			}
		}
	}
	void print(LL i,LL j)
	{
		if (j)
		{
			//cout<<i<<" "<<val[j]
			print(i,last[j]);
		}
	}
	void find(string s)
	{
		LL j=0;
		for (LL i=0;i<s.size();i++)
		{
			LL c=id(s[i]);
			j=ch[j][c];
			if (val[j]) print(i,j);
			else if (last[j]) print(i,last[j]);
		}
	}
};

//10.最大流dinic------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct edge{
	LL from,to,cap,flow;
};
struct dinic{
	vector<LL> G[maxn];
	vector<edge> E[maxn*2];
	LL cur[maxn];
	LL d[maxn];
	void addedge(LL u,LL v,LL c)
	{
		E.push_back((edge){u,v,c,0});
		E.push_back((edge){v,u,0,0});
		LL cnt=E.size();
		G[u].push_back(cnt-2);
		G[v].push_back(cnt-1);
	}
	bool bfs(LL s,LL t)
	{
		memset(d,0,sizeof(d));
		d[s]=1;
		queue<LL> q;
		q.push(s);
		while (!q.empty())
		{
			LL u=q.front(); q.pop();
			for (LL i=0;i<G[u].size();i++)
			{
				edge e=E[G[u][i]];
				if (!d[e.to] && e.cap>e.flow)
				{
					d[e.to]=d[u]+1;
					q.push(e.to);
				}
			}
		}
		return d[t];
	}
	LL dfs(LL x,LL a,LL t)
	{
		if (x==t || a==0) return a;
		LL flow=0,f=0;
		for (LL &i=cur[x];i<G[x].size();i++)
		{
			edge &e=E[G[x][i]];
			if (d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow),t))>0)
			{
				e.flow+=f;
				E[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if (a==0) break;
			}
		}
		return flow;
	}
	LL maxflow(LL s,LL t)
	{
		LL ans=0;
		while (bfs(s,t))
		{
			memset(cur,0,sizeof(cur));
			flow+=dfs(s,INF,t);
		}
		return flow;
	}
}; 

//11.费用流>.<------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
struct edge{
	LL from,to,cap,flow,cost;
};

struct MCMF{
	LL n,m;
	vector<edge> E;
	vector<LL> G[maxn];
	LL inq[maxn];
	LL d[maxn],p[maxn],a[maxn];
	void addedge(LL u,LL v,LL c,LL cost)
	{
		E.push_back((edge){u,v,c,0,cost});
		E.push_back((edge){v,u,0,0,-cost});
		LL m=E.size();
		G[u].push_back(m-2);
		G[v].push_back(m-1);
	}
	LL SPFA(LL s,LL t,LL &flow,LL &cost)
	{
		flow=cost=0;
		for (LL i=0;i<n;i++) d[i]=INF;
		memset(inq,0,sizeof(inq));
		d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;
		queue<LL> q;
		q.push(s);
		while (!q.empty())
		{
			LL u=q.front(); q.pop();
			inq[u]=0;
			for (LL i=0;i<G[u].size();i++)
			{
				edge e=E[G[u][i]];
				if (e.cap>e.flow && d[e.to]>d[u]+e.cost)
				{
					d[e.to]=d[u]+e.cost;
					p[e.to]=G[u][i];
					a[e.to]=min(a[u],e.cap-e.flow);
					if (!inq[e.to]) q.push(e.to),inq[e.to]=1;
				}
			}
		}
		if (d[t]==INF) return false;
		flow+=a[t];
		cost+=d[t]*a[t];
		LL u=t;
		while (u!=s)
		{
			E[p[u]].flow+=a[t];
			E[p[u]^1].flow-=a[t];
			u=E[p[u]].from;
		}
		return true;
	}
	void mincost(LL s,LL t,LL &mflow,LL &mcost)
	{
		mflow=mcost=0;
		LL flow,cost;
		while (SPFA(s,t,flow,cost)) mflow+=flow,mcost+=cost;
	}
}mc;

//12.fhqTreap------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
struct node{
	LL l,r;
	LL siz,val,w;
}ns[maxn];
LL cnt;

inline LL newnode(LL val)
{
	cnt++;
	ns[cnt].val=val;
	ns[cnt].w=rand()*rand()^rand()*rand()%100000;
	ns[cnt].l=ns[cnt].r=0;
	ns[cnt].siz=1;
	return cnt;
}

inline void updata(LL root)
{
	LL l=ns[root].l,r=ns[root].r;
	ns[root].siz=ns[l].siz+ns[r].siz+1;
}

void merge(LL&root,LL l,LL r) 
{
	if (l==0 || r==0)
	{
		root=l+r;
		return;
	}
	if (ns[l].w>ns[r].w) root=l,merge(ns[root].r,ns[root].r,r),updata(root);
	else root=r,merge(ns[root].l,l,ns[root].l),updata(root);
}

void split1(LL root,LL &l,LL &r,LL k)
{
	if (root==0)
	{
		l=r=0;
		return;
	}
	if (k==0)
	{
		l=0,r=root;
		return;
	}
	LL siz=ns[ns[root].l].siz;
	if (siz+1<=k) l=root,split1(ns[root].r,ns[root].r,r,k-siz-1);
	else r=root,split1(ns[root].l,l,ns[root].l,k);
	updata(root);
}

void split2(LL root,LL &l,LL &r,LL k)
{
	if (root==0)
	{
		l=r=0;
		return;
	}
	LL val=ns[root].val;
	if (val<=k) l=root,split2(ns[root].r,ns[root].r,r,k);
	else r=root,split2(ns[root].l,l,ns[root].l,k);
	updata(root);
}

//13.主席树--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
LL a[maxn],b[maxn],T[maxn];
LL sp[maxn<<5],L[maxn<<5],R[maxn<<5],cnt;

inline LL build(LL l,LL r)
{
	LL rt=++cnt;
	sp[rt]=0;
	if (l<r)
	{
		LL mid=(l+r)/2;
		L[rt]=build(l,mid);
		R[rt]=build(mid+1,r);
	}
	return rt;
}

inline LL updata(LL pre,LL l,LL r,LL x)
{
	LL rt=++cnt;
	L[rt]=L[pre],R[rt]=R[pre],sp[rt]=sp[pre]+1;
	if (l<r)
	{
		LL mid=(l+r)/2;
		if (x<=mid) L[rt]=updata(L[pre],l,mid,x);
		else R[rt]=updata(R[pre],mid+1,r,x);
	}
	return rt;
}

inline LL query(LL u,LL v,LL l,LL r,LL k)
{
	if (l>=r) return l;
	LL x=sp[L[v]]-sp[L[u]];
	LL mid=(l+r)/2;
	if (k<=x) return query(L[u],L[v],l,mid,k);
	else return query(R[u],R[v],mid+1,r,k-x);
}

int main()
{
	LL n,m,q;
	cin>>n>>q;
	for (LL i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b+1,b+n+1);
	m=unique(b+1,b+n+1)-b-1;
	T[0]=build(1,m);
	for (LL i=1;i<=n;i++)
	{
		LL t=lower_bound(b+1,b+m+1,a[i])-b;
		T[i]=updata(T[i-1],1,m,t);
	}
	for (LL i=1;i<=q;i++)
	{
		LL l,r,k;
		cin>>l>>r>>k;
		cout<<b[query(T[l-1],T[r],1,m,k)]<<endl;
	}
	return 0;
}

//14.可持久化01Trie --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL s[maxn];
LL T[maxn],ch[maxn<<5][2],sz;

inline LL insert(LL o,LL x)
{
	LL rt=++sz;
	LL u=rt;
	for (LL i=30;i>=0;i--)
	{
		LL d=(x&(1<<i))==0?0:1;
		ch[u][d^1]=ch[o][d^1];
		ch[u][d]=++sz;
		o=ch[o][d];
		u=ch[u][d];
	}
	return rt;
}

inline LL query(LL u,LL v,LL x)
{
	LL ans=0;
	for (LL i=30;i>=0;i--)
	{
		LL d=(x&(1<<i))==0?0:1;
		if (ch[v][d^1]-ch[u][d^1]) u=ch[u][d^1],v=ch[v][d^1],ans=(ans<<1)^1;
		else u=ch[u][d],v=ch[v][d],ans=(ans<<1);
	//	cout<<i<<" "<<d<<" "<<ch[u][d]<<" "<<ch[u][d^1]<<" "<<ch[v][d]<<" "<<ch[v][d^1]<<endl;
	}
	return ans;
}

int main()
{
	LL n,m;
	cin>>n>>m;
	T[0]=1;
	for (LL i=1;i<=n;i++)
	{
		LL x;
		scanf("%d",&x);
		s[i]=s[i-1]^x;
		T[i]=insert(T[i-1],s[i]);
	}
	bool flag=1;
	for (LL i=1;i<=m;i++)
	{
		char opt;
		cin>>opt;
		if (opt=='A') 
		{
			LL x;
			scanf("%d",&x);
			n++;
			s[n]=s[n-1]^x;
			T[n]=insert(T[n-1],s[n]);
		}
		else
		{
			LL l,r,x;
			scanf("%d %d %d",&l,&r,&x);
		//	if (flag && r==1) while(1);
			flag=0;
			if (l-2>=0) cout<<query(T[l-2],T[r-1],x^s[n])<<endl;
			else if (r-1>=1 )cout<<query(0,T[r-1],x^s[n])<<endl;
			else cout<<(s[n]^x)<<endl;
		}
	}
	return 0;
}
//15.线性基 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct XXJ{
	LL d[100],tmp[100];
	LL flag;
	LL bn;
	XXJ(LL n){bn=n;flag=0;}
	void add(LL x)
	{
		for (LL i=bn-1;i>=0;i--)
		{
			if (x&(1ull<<i))
			{
				if (d[i]) x^=d[i];
				else 
				{
					d[i]=x;
					break;
				}
			}
		}
		flag=1;
	}
	LL qmax()
	{
		LL ans=0;
		for (LL i=bn-1;i>=0;i--) ans=max(ans,ans^d[i]);
		return ans;
	}
	LL qmin()
	{
		for (LL i=0;i<bn;i++) if (d[i]) return d[i];
		return 0;
	}
	LL Kth(LL k)
	{
		LL ans=0,cnt=0;
		k-=flag; if (!k) return 0;
		for (LL i=0;i<bn;i++)
		{
			for (LL j=0;j<i;j++)
			{
				if (d[i]&(1ull<<j)) d[i]^=d[j];
			}
			if (d[i]) tmp[++cnt]=d[i];
		}
		if (k>=(1ull<<cnt)) return -1;
		for (LL i=1;i<=cnt;i++)
		{
			if (k&(1ull<<(i-1))) ans^=tmp[i];
		}
		return ans;
	}
};
//16.静态莫队------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL a[maxn];
LL u[maxn],num;
struct ques{
	LL id,l,r;
}q[maxn];
LL ans[maxn];
LL bl[maxn],blo;
inline bool cmp(ques x,ques y)
{
	return (bl[x.l]^bl[y.l])?bl[x.l]<bl[y.l]:((bl[x.l]&1)?x.r<y.r:x.r>y.r);
}

inline void add(LL x)
{
	if (u[a[x]]++==0) num++;
}

inline void del(LL x)
{
	if (--u[a[x]]==0) num--;
}

int main()
{
	LL n,m;
	n=read(),blo=sqrt(n);
	for (LL i=1;i<=n;i++) a[i]=read();
	for (LL i=1;i<=n;i++) bl[i]=(i-1)/blo+1;
	m=read();
	for (LL i=1;i<=m;i++)
	{
		q[i].l=read(),q[i].r=read();
        if (q[i].l>q[i].r) swap(q[i].l,q[i].r);
		q[i].id=i;
	}
	sort(q+1,q+m+1,cmp);
	LL l=0,r=0;
	for (LL i=1;i<=m;i++)
	{
		while (l<q[i].l) del(l++);
		while (l>q[i].l) add(--l);
		while (r<q[i].r) add(++r);
		while (r>q[i].r) del(r--);
		ans[q[i].id]=num;
	}
	for (LL i=1;i<=m;i++)
	{
		print(ans[i]);
		printf("\n");
	}
	return 0;
}

//17.树上带修莫队------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct ques{
	LL l,r,add,id,t,ans;
}q[maxn];

struct chan{
	LL id,pre,suc;
}alt[maxn];

LL n,m,Q,num,blo,bl[maxn],u[maxn],vis[maxn],b[maxn],pot[maxn];
LL p[maxn][30],dep[maxn];
LL s[maxn],t[maxn],cnt;
LL v[maxn],w[maxn],c[maxn];
LL Tcnt;

void dfs(LL u,LL fa)
{
	p[u][0]=fa; 
	s[u]=++cnt; pot[cnt]=u;
	for (LL i=1;i<20;i++)
	{
		LL pa=p[u][i-1];
		if (pa!=-1) p[u][i]=p[pa][i-1];
		else p[u][i]=-1;
	}
	for (LL i=head[u];~i;i=E[i].next)
	{
		LL v=E[i].v;
		if (v==fa) continue;
		dep[v]=dep[u]+1;
		dfs(v,u);
	}
	t[u]=++cnt; pot[cnt]=u;
}

inline LL lca(LL u,LL v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (LL i=19;i>=0;i--)
	{
		if (p[u][i]>=0 && dep[p[u][i]]>=dep[v]) u=p[u][i];
		if (u==v) return u;
	}
	for (LL i=19;i>=0;i--)
	{
		if (p[u][i]!=p[v][i]) u=p[u][i],v=p[v][i];
	}
	return p[u][0];
}

inline bool cmp(ques x,ques y)
{
	if (bl[x.l]!=bl[y.l]) return bl[x.l]<bl[y.l];
	if (bl[x.r]!=bl[y.r]) return bl[x.r]<bl[y.r];
	return x.t<y.t;
}

inline bool rcmp(ques x,ques y)
{
	return x.id<y.id;
}

inline void add(LL x) 
{
	num+=w[++u[c[x]]]*v[c[x]];
}

inline void del(LL x)
{
	num-=w[u[c[x]]--]*v[c[x]];
}

inline void go()
{
	LL id=alt[++Tcnt].id;
	if (vis[id]) del(id);
	c[id]=alt[Tcnt].suc;
	if (vis[id]) add(id);
}

inline void re()
{
	LL id=alt[Tcnt].id;
	if (vis[id]) del(id);
	c[id]=alt[Tcnt--].pre;
	if (vis[id]) add(id);
}

inline void updata(LL x)
{
	if (!vis[x]) add(x); else del(x);
	vis[x]^=1;
}

int main()
{
	memset(head,-1,sizeof(head));
	n=read(); m=read(); Q=read(); blo=pow(n*2,0.666666);
	LL qn=0;
	for (LL i=1;i<=n*2;i++) bl[i]=(i-1)/blo+1;
	for (LL i=1;i<=m;i++) v[i]=read();
	for (LL i=1;i<=n;i++) w[i]=read();
	for (LL i=1;i<n;i++)
	{
		LL u=read(),v=read();
		addedge(u,v);
		addedge(v,u);
	}
	dfs(1,-1);
	for (LL i=1;i<=n;i++) c[i]=read();
	for (LL i=1;i<=Q;i++)
	{
		LL opt=read();
		if (opt==1)
		{
			LL u=read(),v=read();
			if (s[u]>s[v]) swap(u,v);
			LL top=lca(u,v);
			if (u==top) q[++qn]=(ques){s[u],s[v],0,qn,Tcnt,0};
			else q[++qn]=(ques){t[u],s[v],top,qn,Tcnt,0};
		}
		else
		{
			Tcnt++;
			alt[Tcnt].id=read();alt[Tcnt].suc=read();
			alt[Tcnt].pre=c[alt[Tcnt].id];
			c[alt[Tcnt].id]=alt[Tcnt].suc;
		}
	}
	sort(q+1,q+qn+1,cmp);
	LL l=1,r=0;
	for (LL i=1;i<=qn;i++)
	{
		while (l<q[i].l) updata(pot[l++]);
		while (l>q[i].l) updata(pot[--l]);
		while (r<q[i].r) updata(pot[++r]);
		while (r>q[i].r) updata(pot[r--]);
		while (Tcnt<q[i].t) go();
		while (Tcnt>q[i].t) re();
		if(q[i].add) updata(q[i].add);
		q[i].ans=num;
		if(q[i].add) updata(q[i].add);
	}
	sort(q+1,q+qn+1,rcmp);
	for (LL i=1;i<=qn;i++)
	{
		print(q[i].ans);
		printf("\n");
	}
	return 0;
}
//18.矩阵------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct matric{
	LL n,m;
	LL a[maxn][maxn];
	matric()
	{
		n=m=0;
		memset(a,0,sizeof(a));
	}
	matric operator*(const matric&x) const
	{
		matric ans;
		ans.n=n,ans.m=x.m;
		for (LL i=1;i<=n;i++)
		{
			for (LL j=1;j<=x.m;j++)
			{
				for (LL k=1;k<=m;k++)
				{
					ans.a[i][j]+=a[i][k]*x.a[k][j];
					ans.a[i][j]%=MOD;
				}
			}
		}
		return ans;
	}
};
//19.模拟退火------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LL SA()
{
	double T=100,dlt=0.99995,now=get_e();
	while (T>1e-14)
	{
		LL next=get_e();
		LL dE=next-now;
		LL R=Rand(1e8);
		if (dE<0) now=next;
		else if (exp(-dE/T)*(1e8)>R) now=next;
		T*=dlt;
	}
	return now;
}
while ((double)clock()/CLOCKS_PER_SEC<4.8)
{
	SA();
}

//20.快读快输 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

inline void print(LL x)
{
    if(x/10) print(x/10);
    putchar(x%10+'0');
}

inline void print(LL x,char ch) {if (x<0) putchar('-'),x=-x; print(x);putchar(ch);}
//21.对拍----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define LL long long
using namespace std;

int main()
{
	LL kase=0;	
    while(1)
	{
        Sleep(50);
        system("./program_data");
        system("./program_test");
        system("./program");
        printf("%d : ",++kase);
        if (system("diff program.out program.ans"))
    	{
			printf("WA\n");
			return 0;
        }
		else printf("AC\n");
    }
}