https://nanti.jisuanke.com/t/31714
前两种是基本操作 第三种取反操作乍一看觉得要单独维护一个标记 于是考虑lazy标记的次序 后来发现一步转换
a'=(2^64-1)-a 就相当于两步操作 先区间乘-1 再对区间+(2^64-1) 然后就是个模板题
比赛时没时间写 最后四十分钟看到这个题 最后不到半小时有思路 明知道自己可以做出来但已经没有时间 明知道写不完还要咬着牙去写
真的是我不够努力吗
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const ll mod=18446744073709551615;
struct node1
{
int v;
int next;
};
struct node2
{
int l;
int r;
ll val;
ll laz1;
ll laz2;
};
node1 edge[200010];
node2 tree[400010];
int first[100010],fa[100010],deep[100010],sum[100010],son[100010],top[100010],mp1[100010],mp2[100010];
int n,q,num;
void addedge(int u,int v)
{
edge[num].v=v;
edge[num].next=first[u];
first[u]=num++;
return;
}
void dfsI(int cur)
{
int i,v;
sum[cur]=1,son[cur]=-1;
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur])
{
fa[v]=cur,deep[v]=deep[cur]+1;
dfsI(v);
sum[cur]+=sum[v];
if(son[cur]==-1||sum[son[cur]]<sum[v]) son[cur]=v;
}
}
return;
}
void dfsII(int cur,int tp)
{
int i,v;
num++;
top[cur]=tp,mp1[cur]=num,mp2[num]=cur;
if(son[cur]==-1) return;
dfsII(son[cur],tp);
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur]&&v!=son[cur]) dfsII(v,v);
}
return;
}
void changeI(int cur,ll val)
{
ll len;
len=tree[cur].r-tree[cur].l+1;
tree[cur].val=tree[cur].val+val*len;
return;
}
void changeII(int cur,ll val)
{
tree[cur].val=tree[cur].val*val;
return;
}
void pushup(int cur)
{
tree[cur].val=tree[2*cur].val+tree[2*cur+1].val;
return;
}
void pushdown(int cur)
{
if(tree[cur].laz1!=0||tree[cur].laz2!=1)
{
changeII(2*cur,tree[cur].laz2);
changeI(2*cur,tree[cur].laz1);
tree[2*cur].laz2=tree[2*cur].laz2*tree[cur].laz2;
tree[2*cur].laz1=tree[2*cur].laz1*tree[cur].laz2+tree[cur].laz1;
changeII(2*cur+1,tree[cur].laz2);
changeI(2*cur+1,tree[cur].laz1);
tree[2*cur+1].laz2=tree[2*cur+1].laz2*tree[cur].laz2;
tree[2*cur+1].laz1=tree[2*cur+1].laz1*tree[cur].laz2+tree[cur].laz1;
tree[cur].laz1=0;
tree[cur].laz2=1;
}
return;
}
void build(int l,int r,int cur)
{
int m;
tree[cur].l=l;
tree[cur].r=r;
tree[cur].val=0;
tree[cur].laz1=0;
tree[cur].laz2=1;
if(l==r) return;
m=(l+r)/2;
build(l,m,2*cur);
build(m+1,r,2*cur+1);
pushup(cur);
}
void updateII(int op,int pl,int pr,ll val,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
if(op==1)
{
changeI(cur,val);
tree[cur].laz1+=val;
}
else
{
changeII(cur,val);
tree[cur].laz2=tree[cur].laz2*val;
tree[cur].laz1=tree[cur].laz1*val;
}
return;
}
pushdown(cur);
if(pl<=tree[2*cur].r) updateII(op,pl,pr,val,2*cur);
if(pr>=tree[2*cur+1].l) updateII(op,pl,pr,val,2*cur+1);
pushup(cur);
}
void updateI(int op,int u,int v,ll val)
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
updateII(op,mp1[top[u]],mp1[u],val,1);
u=fa[top[u]];
}
if(deep[u]<deep[v]) swap(u,v);
updateII(op,mp1[v],mp1[u],val,1);
return;
}
ll queryII(int pl,int pr,int cur)
{
ll res;
if(pl<=tree[cur].l&&tree[cur].r<=pr) return tree[cur].val;
pushdown(cur);
res=0;
if(pl<=tree[2*cur].r) res+=queryII(pl,pr,2*cur);
if(pr>=tree[2*cur+1].l) res+=queryII(pl,pr,2*cur+1);
return res;
}
ll queryI(int u,int v)
{
ll res;
res=0;
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
res+=queryII(mp1[top[u]],mp1[u],1);
u=fa[top[u]];
}
if(deep[u]<deep[v]) swap(u,v);
res+=queryII(mp1[v],mp1[u],1);
return res;
}
int main()
{
ll w;
int i,op,u,v;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
num=0;
for(i=2;i<=n;i++)
{
scanf("%d",&u);
addedge(u,i);
addedge(i,u);
}
fa[1]=0,deep[1]=1;
dfsI(1);
num=0;
dfsII(1,1);
build(1,n,1);
scanf("%d",&q);
while(q--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%llu",&u,&v,&w);
updateI(2,u,v,w);
}
else if(op==2)
{
scanf("%d%d%llu",&u,&v,&w);
updateI(1,u,v,w);
}
else if(op==3)
{
scanf("%d%d",&u,&v);
updateI(2,u,v,mod);
updateI(1,u,v,mod);
}
else
{
scanf("%d%d",&u,&v);
printf("%llu\n",queryI(u,v));
}
}
}
return 0;
}