Seanq @ 2019-10-04 16:58:01
全部TLE
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
typedef long long ll;
struct line{
int from,to;
ll val;
int conec;
}s[N];
int n;
int hed[N],tal[N<<1],nxt[N<<1],cnt=0;
ll val[N<<1];
int fa[N];
int dep[N];
int size[N]={0};
int son[N]={0};
int dfn[N];
int rk[N];
int indexy=0;
int top[N];
ll v[N];
ll llmax(ll x,ll y){return x>y?x:y;}
struct Sugment_Tree{
ll t[N<<2];
#define mid (l+r)/2
void push_up(int num){
t[num]=llmax(t[num<<1],t[num<<1|1]);
}
void build(int l,int r,int num){
if(l==r){
t[num]=v[l];
return;
}
build(l,mid,num<<1),build(mid+1,r,num<<1|1);
push_up(num);
}
void upd(int l,int r,int num,int pos,ll SUM){
if(l>pos||r<pos) return;
if(l==r&&r==pos){
t[num]=SUM;
return;
}
upd(l,mid,num<<1,pos,SUM);
upd(mid+1,r,num<<1|1,pos,SUM);
push_up(num);
}
ll ask(int l,int r,int num,int L,int R){
if(L>R) return 0;
if(l>R||r<L) return 0;
if(l>=L&&r<=R) return t[num];
ll a=ask(l,mid,num<<1,L,R),b=ask(mid+1,r,num<<1|1,L,R);
return llmax(a,b);
}
}T;
void addege(int x,int y,ll z){
cnt++;
tal[cnt]=y;
val[cnt]=z;
nxt[cnt]=hed[x];
hed[x]=cnt;
}
void dfs1(int u,int father){
fa[u]=father;
size[u]=1;
for(int i=hed[u];i;i=nxt[i]){
int v=tal[i];
if(v==fa[u]) continue;
dfs1(v,u);
dep[v]=dep[u]+1;
size[u]+=size[v];
if(size[v]>size[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp){
indexy++;
dfn[u]=indexy;
rk[dfn[u]]=u;
top[u]=tp;
if(!son[u]) return;
dfs2(son[u],tp);
for(int i=hed[u];i;i=nxt[i]){
int v=tal[i];
if(v==fa[u]||v==son[u]) continue;
dfs2(v,v);
}
}
ll cgn(int x,int y){
ll ans=0;
//LCA不能算进来!!!!!!
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans=llmax(ans,T.ask(1,n,1,dfn[top[x]],dfn[x]));
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
ans=llmax(ans,T.ask(1,n,1,dfn[y]+1,dfn[x]));
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d%lld",&s[i].from,&s[i].to,&s[i].val);
addege(s[i].from,s[i].to,s[i].val);
addege(s[i].to,s[i].from,s[i].val);
}
dep[1]=1;
dfs1(1,1);
dfs2(1,1);
for(int i=1;i<n;i++){
if(fa[s[i].from]==s[i].to) v[dfn[s[i].from]]=s[i].val,s[i].conec=dfn[s[i].from];
else v[dfn[s[i].to]]=s[i].val,s[i].conec=dfn[s[i].to];
}
/*
for(int i=1;i<=n;i++)
cout<<v[i]<<" ";
cout<<endl;
*/
T.build(1,n,1);
char c[10];
while(1){
scanf("%s",c);
if(c[0]=='D') break;
else if(c[0]=='Q'){
int x,y;
scanf("%d%d",&x,&y);
if(x==y){
printf("0\n");
continue;
}
printf("%lld\n",cgn(x,y));
}
else{
int pos;
ll va;
scanf("%d%lld",&pos,&va);
T.upd(1,n,1,s[pos].conec,va);
}
}
return 0;
}
by CreeperLordVader @ 2019-10-04 16:59:28
struct Sugment_Tree
名字瞩目
by Seanq @ 2019-10-04 16:59:44
习惯了......
by Seanq @ 2019-10-04 17:09:37
哦,已AC
by Seanq @ 2019-10-04 17:09:57
dfs1错了