树剖裸题,边权转点权,直接赋给深度较深的点.

查询的时候由于会出多修改一个点的问题,所以要\(+1\)。

这里把编号从\(0\)到\(n-1\)变成了\(1\)到\(n\).

貌似这样会少很多锅?单点修改也要下放标记。怪不得一直Wa

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#define int long long
#define ls o<<1
#define rs o<<1|1
#define N 1000080
#define R register
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,head[N],tot,depth[N],size[N],f[N],son[N],val[N];
struct cod{int u,v,w,fr;}edge[N<<1];
int tr[N<<2],mn[N<<2],mx[N<<2],m,tg[N<<2];
int dfn[N],fdfn[N],idx,top[N];
char s[8];
inline void add(int x,int y,int z)
{
edge[++tot].u=head[x];
edge[tot].fr=x;
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
}
inline void up(int o)
{
tr[o]=tr[ls]+tr[rs];
mx[o]=max(mx[ls],mx[rs]);
mn[o]=min(mn[ls],mn[rs]);
}
inline void down(int o,int l,int r)
{
if(tg[o])
{
tg[ls]^=1;tg[rs]^=1;
tr[ls]*=-1;tr[rs]*=-1;
swap(mx[ls],mn[ls]);swap(mx[rs],mn[rs]);
mx[ls]*=-1;mx[rs]*=-1;mn[ls]*=-1;mn[rs]*=-1;
tg[o]^=1;
}
}
void build(int o,int l,int r)
{
if(l==r)
{
tr[o]=val[fdfn[l]];
mn[o]=mx[o]=tr[o];
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
up(o);
}
void change(int o,int l,int r,int pos,int w)
{
if(l==r){tr[o]=mx[o]=mn[o]=w;return;}
down(o,l,r);
int mid=(l+r)>>1;
if(pos<=mid)change(ls,l,mid,pos,w);
else change(rs,mid+1,r,pos,w);
up(o);
}
void qufan(int o,int l,int r,int x,int y)
{
if(x<=l and y>=r)
{
tr[o]*=-1;tg[o]^=1;
swap(mx[o],mn[o]);
mx[o]*=-1;mn[o]*=-1;
return;
}
down(o,l,r);
int mid=(l+r)>>1;
if(x<=mid)qufan(ls,l,mid,x,y);
if(y>mid)qufan(rs,mid+1,r,x,y);
up(o);
}
int query_sum(int o,int l,int r,int x,int y)
{
if(x<=l and y>=r)return tr[o];
down(o,l,r);
int mid=(l+r)>>1,res=0;
if(x<=mid)res+=query_sum(ls,l,mid,x,y);
if(y>mid) res+=query_sum(rs,mid+1,r,x,y);
return res;
}
int query_max(int o,int l,int r,int x,int y)
{
if(x<=l and y>=r)return mx[o];
down(o,l,r);
int mid=(l+r)>>1,res=-214700483647LL;
if(x<=mid)res=max(res,query_max(ls,l,mid,x,y));
if(y>mid)res=max(res,query_max(rs,mid+1,r,x,y));
return res;
}
int query_min(int o,int l,int r,int x,int y)
{
if(x<=l and y>=r)return mn[o];
down(o,l,r);
int mid=(l+r)>>1,res=214700483647LL;
if(x<=mid)res=min(res,query_min(ls,l,mid,x,y));
if(y>mid)res=min(res,query_min(rs,mid+1,r,x,y));
return res;
}
void dfs1(int u,int fa,int dis)
{
depth[u]=depth[fa]+1;size[u]=1;f[u]=fa;val[u]=dis;
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs1(edge[i].v,u,edge[i].w);
size[u]+=size[edge[i].v];
if(son[u]==-1 or size[son[u]]<size[edge[i].v])
son[u]=edge[i].v;
}
}
void dfs2(int u,int t)
{
dfn[u]=++idx;fdfn[idx]=u;top[u]=t;
if(son[u]==-1)return;
dfs2(son[u],t);
for(R int i=head[u];i;i=edge[i].u)
{
if(dfn[edge[i].v])continue;
dfs2(edge[i].v,edge[i].v);
}
}
inline void tchange(int x,int y)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(depth[fx]>depth[fy])
{
qufan(1,1,idx,dfn[fx],dfn[x]);
x=f[fx];
}
else
{
qufan(1,1,idx,dfn[fy],dfn[y]);
y=f[fy];
}
fx=top[x],fy=top[y];
}
if(x == y) return ;
if(dfn[x]>dfn[y])swap(x,y);
qufan(1,1,idx,dfn[x]+1,dfn[y]);
}
inline int tquery(int x,int y)
{
int fx=top[x],fy=top[y],res=0;
while(fx!=fy)
{
if(depth[fx]>depth[fy])
{
res+=query_sum(1,1,idx,dfn[fx],dfn[x]);
x=f[fx];
}
else
{
res+=query_sum(1,1,idx,dfn[fy],dfn[y]);
y=f[fy];
}
fx=top[x],fy=top[y];
}
if(x == y) return res ;
if(dfn[x]>dfn[y])swap(x,y);
res+=query_sum(1,1,idx,dfn[x]+1,dfn[y]);
return res;
}
inline int tquery_min(int x,int y)
{
int fx=top[x],fy=top[y],res=214700483647LL;
while(fx!=fy)
{
if(depth[fx]>depth[fy])
{
res=min(res,query_min(1,1,idx,dfn[fx],dfn[x]));
x=f[fx];
}
else
{
res=min(res,query_min(1,1,idx,dfn[fy],dfn[y]));
y=f[fy];
}
fx=top[x],fy=top[y];
}
if(x == y) return res ;
if(dfn[x]>dfn[y])swap(x,y);
res=min(res,query_min(1,1,idx,dfn[x]+1,dfn[y]));
return res;
}
inline int tquery_max(int x,int y)
{
int fx=top[x],fy=top[y],res=-214700483647LL;
while(fx!=fy)
{
if(depth[fx]>depth[fy])
{
res=max(res,query_max(1,1,idx,dfn[fx],dfn[x]));
x=f[fx];
}
else
{
res=max(res,query_max(1,1,idx,dfn[fy],dfn[y]));
y=f[fy];
}
fx=top[x],fy=top[y];
}
if(x == y) return res ;
if(dfn[x]>dfn[y])swap(x,y);
res=max(res,query_max(1,1,idx,dfn[x]+1,dfn[y]));
return res;
}
signed main()
{
in(n);memset(son,-1,sizeof son);
for(R int i=1,x,y,z;i<n;i++)
{
in(x),in(y),in(z);
x++;y++;
add(x,y,z);add(y,x,z);
}
dfs1(1,0,0);dfs2(1,1);
build(1,1,n);
in(m);
for(R int x,y;m;m--)
{
scanf("%s",s);in(x),in(y);
if(s[0]=='C')
{
x*=2;
if(depth[edge[x].fr]>depth[edge[x].v])
x=edge[x].fr;
else x=edge[x].v;
change(1,1,idx,dfn[x],y);
}
else if(s[0]=='N'){x++,y++,tchange(x,y);}
else if(s[0]=='S'){x++,y++,printf("%lld\n",tquery(x,y));}
else if(s[1]=='A'){x++,y++,printf("%lld\n",tquery_max(x,y));}
else if(s[1]=='I'){x++,y++,printf("%lld\n",tquery_min(x,y));}
}
}
05-12 07:14