题目描述
S国有N个城市,编号从1到N。城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市。每个城市信仰不同的宗教,如飞天面条神教、隐形独角兽教、绝地教都是常见的信仰。
为了方便,我们用不同的正整数代表各种宗教, S国的居民常常旅行。旅行时他们总会走最短路,并且为了避免麻烦,只在信仰和他们相同的城市留宿。当然旅程的终点也是信仰与他相同的城市。S国政府为每个城市标定了不同的旅行评级,旅行者们常会记下途中(包括起点和终点)留宿过的城市的评级总和或最大值。
在S国的历史上常会发生以下几种事件:
“CC x c“:城市x的居民全体改信了c教;
“CW x w“:城市x的评级调整为w;
“QS x y“:一位旅行者从城市x出发,到城市y,并记下了途中留宿过的城市的评级总和;
“QM x y“:一位旅行者从城市x出发,到城市y,并记下了途中留宿过的城市的评级最大值。
由于年代久远,旅行者记下的数字已经遗失了,但记录开始之前每座城市的信仰与评级,还有事件记录本身是完好的。请根据这些信息,还原旅行者记下的数字。 为了方便,我们认为事件之间的间隔足够长,以致在任意一次旅行中,所有城市的评级和信仰保持不变。
输入输出格式
输入格式:
输入的第一行包含整数N,Q依次表示城市数和事件数。 接下来N行,第i+l行两个整数Wi,Ci依次表示记录开始之前,城市i的评级和信仰。 接下来N-1行每行两个整数x,y表示一条双向道路。 接下来Q行,每行一个操作,格式如上所述。
输出格式:
对每个QS和QM事件,输出一行,表示旅行者记下的数字。
输入输出样例
5 6 3 1 2 3 1 2 3 3 5 1 1 2 1 3 3 4 3 5 QS 1 5 CC 3 1 QS 1 5 CW 3 3 QS 1 5 QM 2 4
8 9 11 3
说明
N,Q < =10^5 , C < =10^5
数据保证对所有QS和QM事件,起点和终点城市的信仰相同;在任意时
刻,城市的评级总是不大于10^4的正整数,且宗教值不大于C。
思路:
树剖+多棵线段树
来,上代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define maxn 100005 using namespace std; struct EdgeType { LL to,next; }; struct EdgeType edge[maxn<<1]; struct TreeNodeType { LL l,r,dis,ma; }; struct TreeNodeType tree[maxn*20]; LL if_z,n,dis[maxn],god[maxn],cnt,q; LL head[maxn],deep[maxn],f[maxn],size[maxn]; LL belong[maxn],flag[maxn],root[maxn]; char Cget; inline void read_int(LL &now) { now=0,if_z=1,Cget=getchar(); while(Cget>'9'||Cget<'0') { if(Cget=='-') if_z=-1; Cget=getchar(); } while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } now*=if_z; } inline void edge_add(LL from,LL to) { cnt++; edge[cnt].to=to; edge[cnt].next=head[from]; head[from]=cnt; } void search_1(LL now,LL fa) { LL pos=cnt++; deep[now]=deep[fa]+1,f[now]=fa; for(LL i=head[now];i;i=edge[i].next) { if(edge[i].to==fa) continue; search_1(edge[i].to,now); } size[now]=cnt-pos; } void search_2(LL now,LL chain) { belong[now]=chain,flag[now]=++cnt; LL pos=0; for(LL i=head[now];i;i=edge[i].next) { if(edge[i].to==f[now]) continue; if(size[edge[i].to]>size[pos]) pos=edge[i].to; } if(pos==0) return ; search_2(pos,chain); for(LL i=head[now];i;i=edge[i].next) { if(edge[i].to==f[now]||edge[i].to==pos) continue; search_2(edge[i].to,edge[i].to); } } inline void tree_up(LL now) { tree[now].dis=tree[tree[now].l].dis+tree[tree[now].r].dis; tree[now].ma=max(tree[tree[now].l].ma,tree[tree[now].r].ma); } void tree_do(LL &now,LL to,LL x,LL l,LL r) { if(now==0) now=++cnt; if(l==r) { tree[now].ma=x; tree[now].dis=x; return ; } LL mid=(l+r)>>1; if(to<=mid) tree_do(tree[now].l,to,x,l,mid); else tree_do(tree[now].r,to,x,mid+1,r); tree_up(now); } LL tree_query(LL now,LL tol,LL tor,LL l,LL r,LL type) { if(tol==l&&tor==r) { if(type==1) return tree[now].dis; else return tree[now].ma; } LL mid=(l+r)>>1; if(tol>mid) return tree_query(tree[now].r,tol,tor,mid+1,r,type); else if(tor<=mid) return tree_query(tree[now].l,tol,tor,l,mid,type); else { if(type==1) return tree_query(tree[now].l,tol,mid,l,mid,type)+tree_query(tree[now].r,mid+1,tor,mid+1,r,type); else return max(tree_query(tree[now].l,tol,mid,l,mid,type),tree_query(tree[now].r,mid+1,tor,mid+1,r,type)); } } inline LL solve_query(LL x,LL y,LL g,LL type) { LL pos=0; while(belong[x]!=belong[y]) { if(deep[belong[x]]<deep[belong[y]]) swap(x,y); if(type==1) pos+=tree_query(root[g],flag[belong[x]],flag[x],1,n,1); else pos=max(pos,tree_query(root[g],flag[belong[x]],flag[x],1,n,2)); x=f[belong[x]]; } if(deep[x]>deep[y]) swap(x,y); if(type==1) pos+=tree_query(root[g],flag[x],flag[y],1,n,1); else pos=max(pos,tree_query(root[g],flag[x],flag[y],1,n,2)); return pos; } int main() { read_int(n),read_int(q); for(LL i=1;i<=n;i++) read_int(dis[i]),read_int(god[i]); LL from,to; for(LL i=1;i<n;i++) { read_int(from),read_int(to); edge_add(from,to); edge_add(to,from); } cnt=0,search_1(1,0); cnt=0,search_2(1,1); cnt=0; for(LL i=1;i<=n;i++) { tree_do(root[god[i]],flag[i],dis[i],1,n); } char ch[10]; for(LL i=1;i<=q;i++) { cin>>ch;read_int(from),read_int(to); if(ch[0]=='Q') { if(ch[1]=='S') { cout<<solve_query(from,to,god[from],1); putchar('\n'); } else { cout<<solve_query(from,to,god[from],2); putchar('\n'); } } else { if(ch[1]=='C') { tree_do(root[god[from]],flag[from],0,1,n); god[from]=to; tree_do(root[god[from]],flag[from],dis[from],1,n); } else { tree_do(root[god[from]],flag[from],to,1,n); dis[from]=to; } } } return 0; }