传送门
写在前面:模拟赛中CA爷卡我内存,暴力100变60……不过还是亲学长(毕竟在我初中的时候就是学长),每次做CA爷的题都是高分……
思路:
(迄今做的最爽的树链剖分)
被char哥带着做这道题,结果耗了好久时间调好,这应该是第一次写边链剖了?与点链剖不同的是要处理下边(深度大的变成入点),然后建树时,这个入点的权值就是边的权值(根的权值为0)。查询时,最后l,r到同一条链上来,要让l赋为l的重儿子,然后再求值(如果l,r是同一个点就直接退出,不用赋值重儿子了,因为相同的点之间没有路径权值)
其他就是简单的加标记求最大最小与总和了,这个题多一步取反操作,直接打标记,最大值最小值交换,然后最大最小与总和全部取相反数就行了
注意:读入判断命令要注意……
代码:
#include<bits/stdc++.h>
#define Inf 0x7fffffff
using namespace std;
int n,m,tot,cnt;
char ch;
int val[20010],L[20010],first[20010],pre[20010],dep[20010],siz[20010],top[20010],fa[20010],son[20010];
struct edge
{
int u,v,w,next;
}e[40010];
struct node
{
int sum,maxn,minn;
bool lazy;
}tree[100010];
void add(int x,int y,int z)
{
e[++tot].u=x;
e[tot].v=y;
e[tot].w=z;
e[tot].next=first[x];
first[x]=tot;
}
void dfs1(int now)
{
siz[now]=1;
for (int i=first[now];i;i=e[i].next)
if (fa[now]!=e[i].v)
{
dep[e[i].v]=dep[now]+1;
fa[e[i].v]=now;
dfs1(e[i].v);
siz[now]+=siz[e[i].v];
if (siz[e[i].v]>siz[son[now]]) son[now]=e[i].v;
}
}
void dfs2(int now,int tp)
{
top[now]=tp;
pre[++cnt]=now;
L[now]=cnt;
if (son[now]) dfs2(son[now],tp);
for (int i=first[now];i;i=e[i].next)
if (e[i].v!=fa[now]&&e[i].v!=son[now])
dfs2(e[i].v,e[i].v);
}
void pushdown(int now)
{
if (!tree[now].lazy) return;
tree[now<<1].lazy^=1;
tree[now<<1|1].lazy^=1;
swap(tree[now<<1].maxn,tree[now<<1].minn);
tree[now<<1].maxn=-tree[now<<1].maxn;
tree[now<<1].minn=-tree[now<<1].minn;
tree[now<<1].sum=-tree[now<<1].sum;
swap(tree[now<<1|1].maxn,tree[now<<1|1].minn);
tree[now<<1|1].maxn=-tree[now<<1|1].maxn;
tree[now<<1|1].minn=-tree[now<<1|1].minn;
tree[now<<1|1].sum=-tree[now<<1|1].sum;
tree[now].lazy=0;
}
void pushup(int now)
{
tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
tree[now].maxn=max(tree[now<<1].maxn,tree[now<<1|1].maxn);
tree[now].minn=min(tree[now<<1].minn,tree[now<<1|1].minn);
}
void build(int now,int begin,int end)
{
if (begin==end) {tree[now].sum=tree[now].minn=tree[now].maxn=val[pre[end]];return;}
int mid=(begin+end)>>1;
build(now<<1,begin,mid);
build(now<<1|1,mid+1,end);
pushup(now);
}
void update(int now,int begin,int end,int pos,int num)
{
if (begin==end)
{
tree[now].lazy=0;
tree[now].sum=tree[now].minn=tree[now].maxn=num;
return;
}
pushdown(now);
int mid=(begin+end)>>1;
if (mid>=pos) update(now<<1,begin,mid,pos,num);
else update(now<<1|1,mid+1,end,pos,num);
pushup(now);
}
void oppo(int now,int begin,int end,int l,int r)
{
if (l<=begin&&end<=r)
{
tree[now].lazy^=1;
tree[now].sum=-tree[now].sum;
swap(tree[now].minn,tree[now].maxn);
tree[now].minn=-tree[now].minn;
tree[now].maxn=-tree[now].maxn;
return;
}
pushdown(now);
int mid=(begin+end)>>1;
if (mid>=l) oppo(now<<1,begin,mid,l,r);
if (mid<r) oppo(now<<1|1,mid+1,end,l,r);
pushup(now);
}
int get_max(int now,int begin,int end,int l,int r)
{
if (l<=begin&&end<=r) return tree[now].maxn;
pushdown(now);
int ans=-Inf,mid=(begin+end)>>1;
if (mid>=l) ans=max(get_max(now<<1,begin,mid,l,r),ans);
if (mid<r) ans=max(get_max(now<<1|1,mid+1,end,l,r),ans);
return ans;
}
int get_min(int now,int begin,int end,int l,int r)
{
if (l<=begin&&end<=r) return tree[now].minn;
pushdown(now);
int ans=Inf,mid=(begin+end)>>1;
if (mid>=l) ans=min(get_min(now<<1,begin,mid,l,r),ans);
if (mid<r) ans=min(get_min(now<<1|1,mid+1,end,l,r),ans);
return ans;
}
int get_sum(int now,int begin,int end,int l,int r)
{
if (l<=begin&&end<=r) return tree[now].sum;
pushdown(now);
int ans=0,mid=(begin+end)>>1;
if (mid>=l) ans+=get_sum(now<<1,begin,mid,l,r);
if (mid<r) ans+=get_sum(now<<1|1,mid+1,end,l,r);
return ans;
}
void solve(int l,int r)
{
int minn=Inf,maxn=-Inf,ans=0,f1=top[l],f2=top[r];
while (f1!=f2)
{
if (dep[f1]<dep[f2]) swap(f1,f2),swap(l,r);
if (ch=='N') oppo(1,1,cnt,L[f1],L[l]);
else if (ch=='S') ans+=get_sum(1,1,cnt,L[f1],L[l]);
else if (ch=='I') minn=min(minn,get_min(1,1,cnt,L[f1],L[l]));
else maxn=max(maxn,get_max(1,1,cnt,L[f1],L[l]));
l=fa[f1];f1=top[l];
}
if (dep[l]>dep[r]) swap(l,r);
if (l==r)
{
if (ch=='S') {printf("%d\n",ans);return;}
if (ch=='I') {printf("%d\n",minn);return;}
if (ch=='A') {printf("%d\n",maxn);return;}
}
l=son[l];
if (ch=='N') oppo(1,1,cnt,L[l],L[r]);
else if (ch=='S') ans+=get_sum(1,1,cnt,L[l],L[r]),printf("%d\n",ans);
else if (ch=='I') minn=min(minn,get_min(1,1,cnt,L[l],L[r])),printf("%d\n",minn);
else if (ch=='A')maxn=max(maxn,get_max(1,1,cnt,L[l],L[r])),printf("%d\n",maxn);
}
main()
{
scanf("%d",&n);
int x,y,z;
for (int i=1;i<n;i++)
scanf("%d%d%d",&x,&y,&z),
x++,y++,
add(x,y,z),add(y,x,z);
dfs1(1);
dfs2(1,1);
for (int i=1;i<=tot;i++)
{
if (dep[e[i].u]>dep[e[i].v]) swap(e[i].u,e[i].v);
val[e[i].v]=e[i].w;
}
build(1,1,cnt);
scanf("%d",&m);
while (m--)
{
ch=getchar();
while (ch!='S'&&ch!='C'&&ch!='M'&&ch!='N') ch=getchar();
if (ch=='C') {scanf("%d%d",&x,&y);update(1,1,cnt,L[e[x<<1].v],y);continue;}
else if (ch=='S') getchar(),getchar();
else if (ch=='M') ch=getchar(),getchar();
scanf("%d%d",&x,&y);
solve(x+1,y+1);
}
}