虽然洛谷数据水,然而咱最终还是没有卡过uoj上的毒瘤数据……
神tm全uoj就3个人过了这题……
首先,每个数最多被开根\(6\)次,开到\(1\)之后就别管它了,把它用并查集连到它父亲上
它每次要走\(k\)步,我们可以对\(k\)分类讨论,如果\(k\)比较大就直接暴力跳,否则建\(k\)棵树,每次树剖+线段树维护
然而我只会暴力跳了……
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#pragma GCC optimize(3)
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
inline void swap(R int &x,R int &y){x^=y^=x^=y;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
ll read(){
R ll res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R ll x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5,S=655;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
ll a[N],ans;int ga[N],fa[N],fat[N][25],jp[N][S],dep[N],son[N],sz[N],top[N];
int n,s,m,u,v,op,k;
int find(R int x){return ga[x]==x?x:ga[x]=find(ga[x]);}
void dfs1(int u,int f){
sz[u]=1,dep[u]=dep[f]+1,fa[u]=fat[u][0]=jp[u][1]=f;
jp[u][0]=u;
fp(i,1,18)fat[u][i]=fat[fat[u][i-1]][i-1];
fp(i,2,s)jp[u][i]=jp[f][i-1];
go(u)if(v!=f){
dfs1(v,u),sz[u]+=sz[v];
if(sz[v]>sz[son[u]])son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;if(!son[u])return;
dfs2(son[u],t);
go(u)if(!top[v])dfs2(v,v);
}
int LCA(R int u,R 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;
}
struct query{
int md[N],len[N],top[N],son[N],hi[N];
vector<int>U[N],D[N];
void dfs1(int u,int fa){
md[u]=dep[u];
go(u)if(v!=fa){
dfs1(v,u);
if(md[v]>md[son[u]])son[u]=v,md[u]=md[v];
}
}
void dfs2(int u,int t){
top[u]=t,len[u]=md[u]-dep[t]+1;
if(!son[u])return;
dfs2(son[u],t);
go(u)if(!top[v])dfs2(v,v);
}
void init(){
dfs1(1,0),dfs2(1,1);
int now=0;
fp(i,1,n){
if(!(i>>now&1))++now;
hi[i]=now;
}
fp(i,1,n)if(i==top[i]){
for(R int j=1,u=i;j<=len[i]&&u;++j)u=fa[u],U[i].push_back(u);
for(R int j=1,u=i;j<=len[i]&&u;++j)u=son[u],D[i].push_back(u);
}
}
int Fa(int u,int k){
if(k>dep[u])return 0;if(!k)return u;
u=fat[u][hi[k]],k^=(1<<hi[k]);
if(!k)return u;
if(dep[u]-dep[top[u]]==k)return top[u];
if(k>dep[u]-dep[top[u]])return U[top[u]][k-dep[u]+dep[top[u]]-1];
return D[top[u]][dep[u]-dep[top[u]]-k-1];
}
}qwq;
void upd(R int u){
if(a[u]==1)return;
a[u]=sqrt(a[u]);
if(a[u]==1)ga[u]=find(fa[u]);
}
int pos(int u,int v,int lca,int k){
if(dep[v]-dep[lca]>=k)return qwq.Fa(v,k);
k-=dep[v]-dep[lca];
return qwq.Fa(u,dep[u]-dep[lca]-k);
}
int jump(int u,int k){
if(k>s)return qwq.Fa(u,k);
int v=find(fa[u]),d=(dep[u]-dep[v])%k;
if(d)d=k-d;
return jp[v][d];
}
void update(int u,int v,int k){
if(u==v)return upd(u);
int lca=LCA(u,v),dis=dep[u]+dep[v]-(dep[lca]<<1);
if(dis<=k)return upd(u),upd(v);
if(dis%k)upd(v),v=pos(u,v,lca,dis%k),lca=LCA(u,v);
while(dep[u]>=dep[lca])upd(u),u=jump(u,k);
while(dep[v]>dep[lca])upd(v),v=jump(v,k);
}
void query(int u,int v,int k){
if(u==v)return ans=a[u],void();
int lca=LCA(u,v),dis=dep[u]+dep[v]-(dep[lca]<<1);
if(dis<=k)return ans=a[u]+a[v],void();
if(dis%k)ans+=a[v],v=pos(u,v,lca,dis%k),lca=LCA(u,v);
ans+=(dep[u]+dep[v]-(dep[lca]<<1))/k+1;
while(dep[u]>=dep[lca])ans+=a[u]-1,u=jump(u,k);
while(dep[v]>dep[lca])ans+=a[v]-1,v=jump(v,k);
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();
fp(i,1,n)a[i]=read(),ga[i]=i;
fp(i,1,n-1)u=read(),v=read(),add(u,v),add(v,u);
m=read(),s=500;
dfs1(1,0),dfs2(1,1),qwq.init();
fp(i,1,n)if(a[i]==1)ga[i]=find(fa[i]);
while(m--){
op=read(),u=read(),v=read(),k=read();
if(op==1){
ans=0,query(u,v,k),print(ans);
}else update(u,v,k);
}
return Ot(),0;
}