Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)

时间:2023-03-08 16:22:53

Jiu Yuan Wants to Eat

https://nanti.jisuanke.com/t/31714

You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she will be queen of Tusikur, so she wanted to travel the world while she was still young. In a country, she found a small pub called Whitehouse. Just as she was about to go in for a drink, the boss Carola appeared. And ask her to solve this problem or she will not be allowed to enter the pub. The problem description is as follows:

There is a tree with nn nodes, each node ii contains weight a[i]a[i], the initial value of a[i]a[i] is 00. The root number of the tree is 11. Now you need to do the following operations:

1)1) Multiply all weight on the path from uu to vv by xx

2)2) For all weight on the path from uu to vv, increasing xx to them

3)3) For all weight on the path from uu to vv, change them to the bitwise NOT of them

4)4) Ask the sum of the weight on the path from uu to vv

The answer modulo 2^{64}264.

Jiu Yuan is a clever girl, but she was not good at algorithm, so she hopes that you can help her solve this problem. Ding\backsim\backsim\backsim∽∽∽

The bitwise NOT is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Bits that are 00 become 11, and those that are 11 become 00. For example:

NOT 0111 (decimal 7) = 1000 (decimal 8)

NOT 10101011 = 01010100

Input

The input contains multiple groups of data.

For each group of data, the first line contains a number of nn, and the number of nodes.

The second line contains (n - 1)(n−1) integers b_ibi​, which means that the father node of node (i +1)(i+1) is b_ibi​.

The third line contains one integer mm, which means the number of operations,

The next mm lines contain the following four operations:

At first, we input one integer opt

1)1) If opt is 11, then input 33 integers, u, v, xu,v,x, which means multiply all weight on the path from uu to vv by xx

2)2) If opt is 22, then input 33 integers, u, v, xu,v,x, which means for all weight on the path from uu to vv, increasing xx to them

3)3) If opt is 33, then input 22 integers, u, vu,v, which means for all weight on the path from uu to vv, change them to the bitwise NOT of them

4)4) If opt is 44, then input 22 integers, u, vu,v, and ask the sum of the weights on the path from uu to vv

1 \le n,m,u,v \le 10^51≤n,m,u,v≤105

1 \le x < 2^{64}1≤x<264

Output

For each operation 44, output the answer.

样例输入

7
1 1 1 2 2 4
5
2 5 6 1
1 1 6 2
4 5 6
3 5 2
4 2 2
2
1
4
3 1 2
4 1 2
3 1 1
4 1 1

样例输出

5
18446744073709551613
18446744073709551614
0

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 100005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef unsigned long long ull;
using namespace std; ull tree[maxn<<],lazya[maxn<<],lazym[maxn<<];
int n;
ull v[maxn],val[maxn];
int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
vector<ull>ve[maxn]; void pushup(int rt){
tree[rt]=tree[rt<<]+tree[rt<<|];
} void pushdown(int len,int rt){
if(lazya[rt]||lazym[rt]!=){
lazym[rt<<]*=lazym[rt];
lazym[rt<<|]*=lazym[rt];
lazya[rt<<]*=lazym[rt];
lazya[rt<<|]*=lazym[rt];
lazya[rt<<]+=lazya[rt];
lazya[rt<<|]+=lazya[rt];
tree[rt<<]*=lazym[rt];
tree[rt<<|]*=lazym[rt];
tree[rt<<]+=(len-len/)*lazya[rt];
tree[rt<<|]+=len/*lazya[rt];
lazya[rt]=;
lazym[rt]=;
}
} void build(int l,int r,int rt){
lazya[rt]=;
lazym[rt]=;
if(l==r){
tree[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void mul(int L,int R,ull k,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]*=k;
lazym[rt]*=k;
lazya[rt]*=k;
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) mul(L,R,k,lson);
if(R>mid) mul(L,R,k,rson);
pushup(rt);
} void add(int L,int R,ull k,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]+=(r-l+)*k;
lazya[rt]+=k;
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) add(L,R,k,lson);
if(R>mid) add(L,R,k,rson);
pushup(rt);
} void Not(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]+=(r-l+);
tree[rt]=-tree[rt];
lazym[rt]=-lazym[rt];
lazya[rt]++;
lazya[rt]=-lazya[rt];
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) Not(L,R,lson);
if(R>mid) Not(L,R,rson);
pushup(rt);
} ull query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt];
}
int mid=(l+r)/;
pushdown(r-l+,rt);
ull ans=;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
pushup(rt);
return ans;
} void dfs1(int now,int f,int deep){
dep[now]=deep;
siz[now]=;
fa[now]=f;
int maxson=-;
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==f) continue;
dfs1(ve[now][i],now,deep+);
siz[now]+=siz[ve[now][i]];
if(siz[ve[now][i]]>maxson){
maxson=siz[ve[now][i]];
son[now]=ve[now][i];
}
}
} void dfs2(int now,int topp){
id[now]=++cnt;
val[cnt]=v[now];
top[now]=topp;
if(!son[now]) return;
dfs2(son[now],topp);
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==son[now]||ve[now][i]==fa[now]) continue;
dfs2(ve[now][i],ve[now][i]);
}
} ull qRange(int x,int y){
ull ans=;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=query(id[top[x]],id[x],,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=query(id[x],id[y],,n,);
return ans;
} void addRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(id[x],id[y],k,,n,);
} void mulRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
mul(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
mul(id[x],id[y],k,,n,);
} void notRange(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Not(id[top[x]],id[x],,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Not(id[x],id[y],,n,);
} int main(){
int m,r;
while(~scanf("%d",&n)){
memset(v,,sizeof(v));
memset(val,,sizeof(val));
memset(dep,,sizeof(dep));
memset(fa,,sizeof(fa));
memset(siz,,sizeof(siz));
memset(son,,sizeof(son));
memset(id,,sizeof(id));
memset(top,,sizeof(top));
int pos,z,x,y;
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=n;i++){
scanf("%d",&x);
ve[x].push_back(i);
ve[i].push_back(x);
}
cnt=;
dfs1(,,);
dfs2(,);
build(,n,);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d %d %d",&pos,&x,&y);
if(pos==){
scanf("%d",&z);
mulRange(x,y,z);
}
else if(pos==){
scanf("%d",&z);
addRange(x,y,z);
}
else if(pos==){
notRange(x,y);
}
else{
printf("%llu\n",qRange(x,y));
}
}
}
}