更新了板子,原来的常数太大了
这里取反运算是对 64 位全部取反,相当于 *(-1) + 2e64-1 = *( 2e64-1 )+2e64-1
还有一个问题就是先乘后加可以拆分成 乘 和 加,这样可以降低代码长度
坑点:
unsigned long long 读入和输出都是是%llu
#include <bits/stdc++.h>
#define lc l,mid,x<<1
#define rc mid+1,r,x<<1|1
using namespace std;
typedef unsigned long long LL;
typedef int lint;
const lint maxn = 100005;
const lint maxm = 200005;
const unsigned long long mod = -1;
lint n;
lint tot,ver[maxm],ne[maxm],he[maxn];
void add( lint x,lint y ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
}
lint d[maxn],f[maxn];
lint sz[maxn],son[maxn];
void dfs1(lint x ){
lint mm = 0;
sz[x] = 1;son[x] = 0;
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y = ver[cure];
if( y == f[x] ) continue;
f[y] = x;
d[y] = d[x]+1;
dfs1(y);
if( sz[y] > mm ){
mm = sz[y];
son[x] = y;
}
sz[x]+=sz[y];
}
}
lint top[maxn],h[maxn],num,id[maxn];
void dfs2(lint x){
h[x]=++num;
id[num] = x;
if( son[ f[x] ] == x ){
top[x] = top[f[x] ];
}else{
top[x] = x;
}
if(son[x])dfs2(son[x]);
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y =ver[cure];
if( y ==f[x]||y == son[x] ) continue;
dfs2(y);
}
}
LL laz1[5*maxn],laz2[5*maxn],tree[5*maxn];
void push_up( lint x ){
tree[x] = tree[x<<1] + tree[x<<1|1];
}
void push_down( lint x,lint l,lint r ){
if( laz2[x] != 1 ){
laz2[x<<1] *= laz2[x];laz2[x<<1|1] *= laz2[x];
laz1[x<<1] *= laz2[x];laz1[x<<1|1] *= laz2[x];
tree[x<<1] *= laz2[x];tree[x<<1|1] *= laz2[x];
laz2[x] = 1;
}
lint mid = l + r >> 1;
if( laz1[x] ){
laz1[x<<1] += laz1[x];laz1[x<<1|1] += laz1[x];
tree[x<<1] += laz1[x] *(mid-l+1);tree[x<<1|1] += laz1[x]*(r-mid);
laz1[x] = 0;
}
}
void change1( lint ll,lint rr,LL v,lint l,lint r,lint x ){
if( ll <= l && rr >= r ){
laz1[x] *= v;
laz2[x] *= v;
tree[x] *= v;
return;
}
push_down( x,l,r );
lint mid = l + r >> 1;
if( ll <= mid ){
change1( ll,rr,v,lc );
}
if(rr > mid ){
change1( ll,rr,v,rc );
}
push_up(x);
}
void change2( lint ll,lint rr,LL v,lint l,lint r,lint x ){
if( ll <= l && rr >= r ){
laz1[x] += v;
tree[x] += (r-l+1)*v;
return;
}
push_down( x,l,r );
lint mid = l + r>> 1;
if( ll <= mid ){
change2( ll,rr,v,lc );
}
if( rr > mid ){
change2( ll,rr,v,rc );
}
push_up(x);
}
LL ask( lint ll,lint rr,lint l,lint r,lint x ){
if( ll <= l && rr >= r ){
return tree[x];
}
LL res = 0;
lint mid = l + r>>1;
push_down(x,l,r);
if( ll <= mid ){
res += ask( ll,rr,lc );
}
if( rr > mid ){
res += ask( ll,rr,rc );
}
return res;
}
void solvechange1( lint x,lint y,LL v ){
while( top[x] != top[ y ] ){
if( d[ top[x] ] < d[ top[y] ] ) swap( x,y );
change1( h[ top[x] ],h[x],v,1,num,1 );
x = f[ top[x] ];
}
if( d[x] < d[y] ){
swap( x,y );
}
change1( h[ y ],h[x],v,1,num,1 );
}
void solvechange2( lint x,lint y,LL v ){
while( top[x] != top[ y ] ){
if( d[ top[x] ] < d[ top[y] ] ) swap( x,y );
change2( h[ top[x] ],h[x],v,1,num,1 );
x = f[ top[x] ];
}
if( d[x] < d[y] ){
swap( x,y );
}
change2( h[ y ],h[x],v,1,num,1 );
}
void solvechange3( lint x,lint y ){
solvechange1( x,y,mod );
solvechange2( x,y,mod );
}
LL solveask( lint x,lint y ){
LL res = 0;
while( top[x] != top[y] ){
if( d[ top[x] ] < d[ top[y] ] ) swap( x,y );
res += ask( h[top[x] ],h[x],1,num,1 );
x = f[ top[x] ];
}
if( d[x] < d[y] ) swap(x,y);
res += ask( h[y],h[x],1,num,1 );
return res;
}
void build_tree( lint l,lint r,lint x ){
laz1[x] = 0;laz2[x] = 1;tree[x] = 0;
if( l == r ){
return;
}
lint mid = l + r>> 1;
build_tree( lc );
build_tree(rc);
}
void init( lint n ){
tot = 1;
num = 0;
d[1] = 0;
for( lint i = 0;i <= n;i++ ){
he[i] = 0;
}
}
int main(){
lint m;
while( 1 == scanf("%d",&n) ) {
init(n);
for( lint x,i = 1;i <= n-1;i++ ){
scanf("%d",&x);
add( x,i+1 );add( i+1,x );
}
dfs1(1);dfs2(1);
build_tree(1,num,1);
scanf("%d",&m);
lint x,y;
LL v;
for( lint op,i = 1;i <= m;i++ ){
scanf("%d",&op);
if( op == 1 ){
scanf("%d%d%llu",&x,&y,&v);
solvechange1(x,y,v);
}else if( op == 2 ){
scanf("%d%d%llu",&x,&y,&v);
solvechange2(x,y,v);
}else if( op == 3 ) {
scanf("%d%d",&x,&y);
solvechange3(x,y);
}else{
scanf("%d%d",&x,&y);
LL ans = solveask( x,y );
printf("%llu\n",ans);
}
}
}
return 0;
}