#include<bits/stdc++.h>
#define Yukinoshita namespace
#define Yukino std
using Yukinoshita Yukino;
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int mxn=1e5+5;
using ll=long long;
int f[mxn],e[mxn];
inline int aske(int x,int y)
{
if(x>y) swap(x,y);
//cout<<x<<' '<<y<<endl;
assert(f[y]==x);
return e[y];
}
struct LCT
{
struct del_que
{
multiset<int> st;
void push(int x)
{
st.insert(x);
}
void del(int x)
{
auto i=st.find(x);
assert(i!=st.end());
st.erase(i);
}
int top()
{
return st.size()?*--st.end():0;
}
void pop()
{
st.erase(--st.end());
}
int top2()
{
if(st.empty()) return 0;
if(st.size()==1) return *st.begin();
return *--st.end()+*----st.end();
}
// priority_queue<int> q1,q2;
// void push(int x)
// {
// q1.push(x);
// }
// void del(int x)
// {
// q2.push(x);
// }
// int top()
// {
// while(q2.size()&&q1.top()==q2.top()) q1.pop(),q2.pop();
// return q1.size()?q1.top():0;
// }
// void pop()
// {
// q2.push(q1.top());
// }
// int top2()
// {
// if(q1.size()==q2.size()) return 0;
// if(q1.size()-q2.size()==1) return top();
// int x=top();
// pop();
// int y=top();
// push(x);
// return x+y;
// }
}xans[mxn][2],cq[mxn][2];
struct node0
{
int ans1,ans2,l1,l2,c1,c2,ac[2],as1[2],as2[2],x,y,dis;
bool fl;
friend node0 operator +(const node0 &x,const node0 &y)
{
// cout<<x.y<<'#'<<y.x<<endl;
int v=aske(x.y,y.x),i;
node0 res;
res.x=x.x,res.y=y.y,res.c1=x.c1,res.c2=y.c2;
res.dis=x.dis+y.dis+v;
res.ans1=x.ans1,res.ans2=y.ans2;
res.l1=x.l1,res.l2=y.l2;
res.fl=(x.c2==y.c1)&x.fl&y.fl;
if(x.c2==y.c1)
{
if(x.fl)
res.ans1=max({res.ans1,x.l2+y.l1+v,y.ans1}),res.l1=max(res.l1,x.dis+v+y.l1);
if(y.fl)
res.ans2=max({res.ans2,x.l2+y.l1+v,x.ans2}),res.l2=max(res.l2,y.dis+v+x.l2);
}
for(i=0;i<2;i++)
{
res.ac[i]=max({x.ac[i],y.ac[i],x.as2[i]+y.as1[i]+v});
res.as1[i]=max(x.as1[i],x.dis+y.as1[i]+v);
res.as2[i]=max(y.as2[i],y.dis+x.as2[i]+v);
}
return res;
}
void init(int id)
{
c1=c2=fl=1,x=y=id;
}
void ad(int v)
{
ans1=ans2=ac[v],l1=as1[v],l2=as2[v],c1=c2=v,fl=1;
}
void swp()
{
swap(ans1,ans2),swap(l1,l2),swap(c1,c2),swap(as1,as2),swap(x,y);
}
};
struct node
{
int f,s[2];
bool tag;
int ctag,c;
node0 sum;
}t[mxn];
#define ls t[w].s[0]
#define rs t[w].s[1]
inline bool nroot(int w)
{
return t[t[w].f].s[0]==w||t[t[w].f].s[1]==w;
}
inline bool fc(int w)
{
return t[t[w].f].s[1]==w;
}
/*
int ans1,ans2,l1,l2,c1,c2,ac[2],as1[2],as2[2],x,y,dis;
bool fl;
*/
inline void pushup(int w)
{
node0 res;
res.ans1=res.ans2=max(xans[w][t[w].c].top(),cq[w][t[w].c].top2());
res.x=res.y=w;
res.l1=res.l2=cq[w][t[w].c].top();
res.c1=res.c2=t[w].c;
res.ac[0]=max(xans[w][0].top(),cq[w][0].top2());
res.ac[1]=max(xans[w][1].top(),cq[w][1].top2());
res.as1[0]=res.as2[0]=cq[w][0].top();
res.as1[1]=res.as2[1]=cq[w][1].top();
res.fl=1,res.dis=0;
if(ls) res=t[ls].sum+res;
if(rs) res=res+t[rs].sum;
t[w].sum=res;
}
inline void reverse(int w)
{
if(!w) return;
swap(ls,rs),t[w].sum.swp();
t[w].tag^=1;
}
inline void ad(int w,int c)
{
if(!w) return;
t[w].c=t[w].ctag=c,t[w].sum.ad(c);
}
inline void pushdown(int w)
{
if(t[w].tag)
reverse(ls),reverse(rs),t[w].tag=0;
if(~t[w].ctag)
ad(ls,t[w].ctag),ad(rs,t[w].ctag),t[w].ctag=-1;
}
inline void rotate(int w)
{
int y=t[w].f,z=t[y].f,k=t[y].s[1]==w,x=t[w].s[!k];
if(nroot(y)) t[z].s[t[z].s[1]==y]=w;
t[w].s[!k]=y,t[y].s[k]=x;
if(x) t[x].f=y;
t[y].f=w,t[w].f=z;
pushup(y);
}
inline void pushdownall(int w)
{
if(nroot(w)) pushdownall(t[w].f);
pushdown(w);
}
inline void splay(int w)
{
pushdownall(w);
for(int f;f=t[w].f,nroot(w);rotate(w))
if(nroot(f))
rotate(fc(f)==fc(w)?f:w);
pushup(w);
}
void access(int w)
{
for(int y=0;w;w=t[y=w].f)
{
splay(w);
if(y)
cq[w][t[y].sum.c1].del(t[y].sum.l1+aske(w,t[y].sum.x)),
xans[w][t[y].sum.c1].del(t[y].sum.ans1);
if(rs)
cq[w][t[rs].sum.c1].push(t[rs].sum.l1+aske(w,t[rs].sum.x)),
xans[w][t[rs].sum.c1].push(t[rs].sum.ans1);
rs=y,pushup(w);
}
}
void makeroot(int w)
{
access(w),splay(w),reverse(w);
}
void split(int x,int y)
{
makeroot(x),access(y),splay(y);
}
void link(int x,int y)
{
makeroot(x),makeroot(y);
t[y].f=x;
cq[x][t[y].c].push(t[y].sum.l1+aske(x,y));
xans[x][t[y].c].push(t[y].sum.ans1);
pushdown(x),pushup(x);
}
void add(int x,int y,int c)
{
split(x,y);
ad(y,c);
}
int ask(int x)
{
makeroot(x);
return t[x].sum.ans1;
}
}t;
int main()
{
int n=read(),i;
for(i=2;i<=n;i++)
f[i]=read();
for(i=2;i<=n;i++)
e[i]=read();
for(i=1;i<=n;i++)
t.t[i].sum.init(i),t.t[i].c=1,t.t[i].ctag=-1;
for(i=2;i<=n;i++)
t.link(i,f[i]);
int m=read();
while(m--)
{
int op=read(),x=read();
if(op==1)
{
int res=t.ask(x);
if(res) printf("%d\n",res);
else puts("QwQ");
}
else
{
int y=read();
t.add(x,y,read()&1);
}
}
}
/*
5
1 1 1 1
1 2 3 4
10000
2 1 1 2
1 1
1 2
*/剪贴板
剪贴板 l1e9avgr
剪贴板信息
剪贴板信息
- 收录
- 更新