arrow_king @ 2022-12-25 21:41:31
rt.不知道为啥除了第一个点A以外全T了
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define ll long long
#define il inline
#define N 100005
#define lc(x) x<<1
#define rc(x) (x<<1)|1
il ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {if(c=='-') {f=-1;} c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
struct Edge {
int next,to,w;
};
struct Edg {
int from,to,w;
Edg() {
}
Edg(int a,int b,int c) {
from=a,to=b,w=c;
}
};
struct sgt {
int l,r;
int maxn;
};
Edge edge[N<<2];
int head[N],num_edge;
int n;
int x,y,z;
int dep[N],fa[N],a[N],son[N],siz[N];
int seg[N],rev[N],top[N],tot;
sgt tr[N<<2];
vector<Edg> e;
char s[10];
int max(int x,int y) {
return x>y?x:y;
}
void add_edge(int from,int to,int w) {
edge[++num_edge].next=head[from];
edge[num_edge].to=to;
edge[num_edge].w=w;
head[from]=num_edge;
}
void dfs1(int u,int f) {
dep[u]=dep[f]+1,fa[u]=f,siz[u]=1;
for(int i=head[u];i;i=edge[i].next) {
int v=edge[i].to;
if(v==f) continue;
a[v]=edge[i].w;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v]) son[u]=v;
}
}
void dfs2(int u,int f) {
if(son[u]) {
seg[son[u]]=++tot;
rev[tot]=son[u];
top[son[u]]=top[u];
dfs2(son[u],u);
}
for(int i=head[u];i;i=edge[i].next) {
int v=edge[i].to;
if(v!=f) {
seg[v]=++tot;
rev[tot]=v;
top[v]=v;
dfs2(v,u);
}
}
}
il void push_up(int now) {
tr[now].maxn=max(tr[lc(now)].maxn,tr[rc(now)].maxn);
}
il void build(int now,int l,int r) {
tr[now].l=l,tr[now].r=r;
if(l==r) {
tr[now].maxn=a[rev[l]];
return;
}
int mid=(l+r)>>1;
build(lc(now),l,mid);
build(rc(now),mid+1,r);
push_up(now);
}
il void update(int x,int l,int r,int now,int v) {
if(l==r) {
tr[now].maxn=v;
return;
}
int mid=(l+r)>>1;
if(x<=mid) update(x,l,mid,lc(now),v);
else update(x,mid+1,r,rc(now),v);
push_up(now);
}
il int query(int qx,int qy,int l,int r,int now) {
if(qx<=l&&r<=qy) return tr[now].maxn;
int mid=(l+r)>>1,ans=-1;
if(qx<=mid) ans=max(ans,query(qx,qy,l,mid,lc(now)));
if(mid<qy) ans=max(ans,query(qx,qy,mid+1,r,rc(now)));
return ans;
}
il int ask(int x,int y) {
int fx=top[x],fy=top[y],ans=-1;
while(fx!=fy) {
if(dep[fx]<dep[fy]) {
swap(x,y);
swap(fx,fy);
}
ans=max(ans,query(seg[fx],seg[x],1,tot,1));
x=fa[fx];
fx=top[x];
}
if(x==y) return ans;
if(dep[x]>dep[y]) swap(x,y);
ans=max(ans,query(seg[x]+1,seg[y],1,tot,1));
return ans;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
add_edge(y,x,z);
e.push_back(Edg(x,y,z));
}
seg[1]=1,rev[1]=1,top[1]=1,tot=1;
dfs1(1,0);
dfs2(1,0);
build(1,1,tot);
while(1) {
scanf("%s",s);
if(s[0]=='D') break;
scanf("%d%d",&x,&z);
if(s[0]=='C') {
y=e[x-1].to;
x=e[x-1].from;
if(dep[x]<dep[y]) swap(x,y);
update(seg[x],1,tot,1,z);
}
else {
if(x==z) printf("0\n");
else printf("%d\n",ask(x,z));
}
}
return 0;
}