Refined_heart @ 2019-04-10 17:37:41
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#define MAXN 400000
#define inf 2147483647
using namespace std;
struct node{
int ls,rs,l,r,maxn;
}tr[MAXN];
struct Node{
int next,to;
}e[MAXN<<1];
int siz[MAXN],f[MAXN],son[MAXN],head[MAXN],top[MAXN],tot,dfstime,cnt,n;
int rk[MAXN],id[MAXN],dep[MAXN],a[MAXN],val[MAXN],ans,rt;
char c[10];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}while(ch>='0'&&ch<='9'){
s=(s<<1)+(s<<3)+(ch^48);
ch=getchar();
}return s*w;
}
inline void add(int x,int y,int w){
e[++tot].to=y;
e[tot].next=head[x];
a[tot]=w;
head[x]=tot;
}
void dfs1(int u){
siz[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==f[u])continue;
f[v]=u;
val[v]=a[i];
dep[v]=dep[u]+1;
dfs1(v);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;
rk[id[u]=++dfstime]=u;
if(!son[u])return;
dfs2(son[u],t);
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v!=f[u]&&v!=son[u])dfs2(v,v);
}
}
#define lc tr[x].ls
#define rc tr[x].rs
inline int max_(int a,int b){return a>b?a:b;}
inline void pushup(int x){tr[x].maxn=max_(tr[lc].maxn,tr[rc].maxn);}
void build(int li,int ri,int &x){
x=++cnt;
tr[x].l=li;
tr[x].r=ri;
if(li==ri){
tr[x].maxn=val[rk[li]];
return;
}int mid=(li+ri)>>1;
build(li,mid,lc);
build(mid+1,ri,rc);
pushup(x);
}
void get(int li,int ri,int x){
if(tr[x].l>=li&&tr[x].r<=ri){
ans=max_(ans,tr[x].maxn);
return;
}int mid=(tr[x].l+tr[x].r)>>1;
if(li<=mid)get(li,ri,lc);
if(mid<ri)get(li,ri,rc);
}
void work(int x,int y){
int fx=top[x],fy=top[y],ans=-inf;
while(fx!=fy){
if(dep[fx]>=dep[fy]){
get(id[fx],id[x],rt);
x=f[fx],fx=top[x];
}else{
get(id[fy],id[y],rt);
y=f[fy],fy=top[y];
}
}if(x!=y){
if(dep[x]<=dep[y])get(id[x]+1,id[y],rt);
else get(id[y]+1,id[x],rt);
}
printf("%d\n",ans);
}
void change(int x,int li,int ri,int val){
if(tr[x].l>=li&&tr[x].r<=ri){
tr[x].maxn=val;
return;
}int mid=(tr[x].l+tr[x].r)>>1;
if(li<=mid)change(lc,li,ri,val);
if(mid<ri)change(rc,li,ri,val);
pushup(x);
}
int main(){
n=read();
for(int i=1;i<n;i++){
int x=read(),y=read(),z=read();
add(x,y,z);
add(y,x,z);
}dep[1]=1;
dfs1(1);
dfs2(1,1);
build(1,n,rt);
while(cin>>c&&c[0]!='D'){
if(c[0]=='C'){
int x=read(),y=read();
change(1,id[x],id[x],y);
}else{
int x=read(),y=read();
if(x==y)printf("0\n");
else work(x,y);
}
}
return 0;
}
蒟蒻不知道为什么一直
by 星梦童趣Doxitce @ 2019-04-10 17:56:18
还是先不要刷紫题的好
by NaCly_Fish @ 2019-04-10 17:56:52
@Refined_heart 您这个树剖写法真神奇。。
by NaCly_Fish @ 2019-04-10 17:57:21
不如用LCT (逃
by Refined_heart @ 2019-04-10 17:58:18
@NaCly_Fish 蒟蒻不会LCT啊……
by Refined_heart @ 2019-04-10 17:59:03
@星梦童趣Doxitce 嗯,我复习一下最近刚刚背的树剖模板,然后就补基础去……
by Refined_heart @ 2019-04-10 18:00:37
@星梦童趣Doxitce 话说您退出团队了?
by 星梦童趣Doxitce @ 2019-04-10 18:20:15
@Refined_heart 嗯嗯不过我还是会继续学信息学的
by 星梦童趣Doxitce @ 2019-04-10 18:22:47
我还有一道题要改呢就不帮你看了再说我也不会
by Refined_heart @ 2019-04-10 19:29:50
@星梦童趣Doxitce 嗯,没事,谢谢