对每种信仰建一个线段树
显然这是MLE的
动态开点就好了!
代码如下:
#include<algorithm>
#include<ctype.h>
#include<cstdio>
#define N 100020
using namespace std;
inline int read(){
int x=0,f=1;char c;
do c=getchar(),f=c=='-'?-1:f; while(!isdigit(c));
do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c));
return x*f;
}
int n,m,x,y,Top,Timer;
int fir[N],dep[N],fa[N],top[N],pre[N],tree[N],son[N],siz[N];
char c[5];
struct City{
int t,k;
}a[N];
struct Edge{
int to,nex;
Edge(int _=0,int __=0):to(_),nex(__){};
}nex[N<<1];
struct Node{
int sum,maxx;
Node *ls,*rs;
inline void maintain(){
maxx=sum=0;
if(ls) maxx=max(maxx,ls->maxx),sum+=ls->sum;
if(rs) maxx=max(maxx,rs->maxx),sum+=rs->sum;
return;
}
Node():sum(0),maxx(0),ls(NULL),rs(NULL){};
}*root[N];
inline void add(int x,int y){
nex[++Top]=Edge(y,fir[x]);
fir[x]=Top;
}
void dfs1(int x,int Dep,int Fa){
dep[x]=Dep;fa[x]=Fa;
siz[x]=1;
for(int i=fir[x];i;i=nex[i].nex){
if(nex[i].to==Fa) continue;
dfs1(nex[i].to,Dep+1,x);
siz[x]+=siz[nex[i].to];
if(siz[son[x]]<siz[nex[i].to]) son[x]=nex[i].to;
}
}
void dfs2(int x,int Top){
top[x]=Top;
tree[x]=++Timer;pre[Timer]=x;
if(!son[x]) return;
dfs2(son[x],Top);
for(int i=fir[x];i;i=nex[i].nex){
if(nex[i].to==fa[x] || nex[i].to==son[x]) continue;
dfs2(nex[i].to,nex[i].to);
}
}
void Change(int L,int R,int x,int v,Node *&k){
if(!k) k=new Node;
if(L==R){
k->sum=k->maxx=v;
return;
}
int mid=(L+R)>>1;
if(x<=mid) Change(L,mid,x,v,k->ls);
else Change(mid+1,R,x,v,k->rs);
k->maintain();
return;
}
int Query_Sum(int L,int R,int x,int y,Node *k){
if(!k) return 0;
if(L>=x && R<=y) return k->sum;
int mid=(L+R)>>1;
if(mid>=y) return Query_Sum(L,mid,x,y,k->ls);
else if(mid<x) return Query_Sum(mid+1,R,x,y,k->rs);
else return Query_Sum(L,mid,x,y,k->ls)+Query_Sum(mid+1,R,x,y,k->rs);
}
int Query_Max(int L,int R,int x,int y,Node *k){
if(!k) return -1;
if(L>=x && R<=y) return k->maxx;
int mid=(L+R)>>1;
if(mid>=y) return Query_Max(L,mid,x,y,k->ls);
else if(mid<x) return Query_Max(mid+1,R,x,y,k->rs);
else return max(Query_Max(L,mid,x,y,k->ls),Query_Max(mid+1,R,x,y,k->rs));
}
inline int Sum_Tree(int x,int y){
int tmp=0,type=a[x].t;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
tmp=tmp+Query_Sum(1,n,tree[top[x]],tree[x],root[type]);
x=fa[top[x]];
}
if(tree[x]>tree[y]) swap(x,y);
tmp=tmp+Query_Sum(1,n,tree[x],tree[y],root[type]);
return tmp;
}
inline int Max_Tree(int x,int y){
int tmp=-1,type=a[x].t;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
tmp=max(tmp,Query_Max(1,n,tree[top[x]],tree[x],root[type]));
x=fa[top[x]];
}
if(tree[x]>tree[y]) swap(x,y);
tmp=max(tmp,Query_Max(1,n,tree[x],tree[y],root[type]));
return tmp;
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++)
a[i].k=read(),a[i].t=read();
for(int i=1;i<n;i++){
x=read();y=read();
add(x,y);add(y,x);
}
dfs1(1,1,0);dfs2(1,1);
for(int i=1;i<=n;i++)
Change(1,n,tree[i],a[i].k,root[a[i].t]);
for(int i=1;i<=m;i++){
scanf("%s",c+1);
x=read();y=read();
if(c[1]=='C'){
if(c[2]=='C'){
Change(1,n,tree[x],0,root[a[x].t]);
Change(1,n,tree[x],a[x].k,root[a[x].t=y]);
}
else Change(1,n,tree[x],a[x].k=y,root[a[x].t]);
}
else{
if(c[2]=='S') printf("%d\n",Sum_Tree(x,y));
else printf("%d\n",Max_Tree(x,y));
}
}
return 0;
}