hybz2243 染色

来源:互联网 发布:图片走马灯效果代码js 编辑:程序博客网 时间:2024/04/20 10:16
#include#include#includeusing namespace std;const int Max=100860;int val[Max];struct SegmentTree{int lc[Max<<2],rc[Max<<2],cnt[Max<<2],flag[Max<<2];inline void PushUp(int idx){int L=idx<<1 r="L+1;" lc="" idx="" lc="" l="" rc="" idx="" rc="" r="" cnt="" idx="" cnt="" l="" cnt="" r="" -="" rc="" l="" lc="" r="" void="" build="" int="" idx="" int="" left="" int="" right="" flag="" idx="" -1="" if="" left="=right)" lc="" idx="" rc="" idx="" val="" left="" cnt="" idx="" 1="" return="" int="" mid="(left+right)">>1;Build(idx<<1,left,mid);Build(idx<<1|1,mid+1,right);PushUp(idx);}inline void PushDown(int idx){if(flag[idx]!=-1){int L=idx<<1,R=L+1;flag[L]=flag[R]=flag[idx];lc[L]=rc[L]=lc[R]=rc[R]=flag[idx];cnt[L]=cnt[R]=1;flag[idx]=-1;}}void Update(int idx,int left,int right,int L,int R,int color){if(L<=left && right<=R){lc[idx]=rc[idx]=flag[idx]=color;cnt[idx]=1;return ;}PushDown(idx);int mid=(left+right)>>1;if(L<=mid) Update(idx<<1,left,mid,L,R,color);if(mid<R) Update(idx<<1|1,mid+1,right,L,R,color);PushUp(idx);}int QueryCnt(int idx,int left,int right,int L,int R){if(L<=left && right<=R)return cnt[idx];PushDown(idx);int mid=(left+right)>>1,ans=0;if(L<=mid) ans+=QueryCnt(idx<<1,left,mid,L,R);if(mid<R) ans+=QueryCnt(idx<<1|1,mid+1,right,L,R);if(L<=mid && mid<R) ans-=rc[idx<<1]==lc[idx<<1 1="" return="" ans="" int="" querycolor="" int="" idx="" int="" left="" int="" right="" int="" pos="" if="" left="=right" flag="" idx="" -1="" return="" lc="" idx="" pushdown="" idx="" int="" mid="(left+right)">>1;if(pos<=mid) return QueryColor(idx<<1,left,mid,pos);else return QueryColor(idx<<1|1,mid+1,right,pos);}}tree;struct Edge{int v,next;}edge[Max<<1 int="" f="" max="" fa="" max="" tot="" max="" top="" max="" depth="" max="" son="" max="" pos="" max="" size="" weight="" max="" void="" add="" int="" u="" int="" v="" int="" cnt="" edge="" cnt="" v="v;" edge="" cnt="" next="f[u];" f="" u="" cnt="" edge="" cnt="" v="u;" edge="" cnt="" next="f[v];" f="" v="" cnt="" void="" init="" memset="" f="" -1="" sizeof="" f="" depth="" 1="" size="0;" fa="" 1="" top="" 1="" 1="" void="" dfs1="" int="" cur="" tot="" cur="" 1="" son="" cur="" 0="" int="" v="" maxv="-1;" for="" int="" i="f[cur];i!=-1;i=edge[i].next)" v="edge[i].v;" if="" v="" fa="" cur="" fa="" v="" cur="" depth="" v="" depth="" cur="" 1="" dfs1="" v="" tot="" cur="" tot="" v="" if="" tot="" v="">maxv) maxv=tot[v],son[cur]=v;}}}void Dfs2(int cur){int v=son[cur];pos[cur]=++size;val[size]=weight[cur];if(v) top[v]=top[cur],Dfs2(v);for(int i=f[cur];i!=-1;i=edge[i].next){v=edge[i].v;if(v!=fa[cur] && v!=son[cur]){top[v]=v;Dfs2(v);}}}void Update(int u,int v,int data){int topu=top[u];int topv=top[v];while(topu!=topv){if(depth[topu]depth[v]) swap(u,v);tree.Update(1,1,size,pos[u],pos[v],data);}int Query(int u,int v) {int topu=top[u],topv=top[v],ans=0;int uh=-1,vh=-1; //上一次的头while(topu!=topv){if(depth[topu]depth[v]) swap(u,v),swap(uh,vh);ans+=tree.QueryCnt(1,1,size,pos[u],pos[v]);if(uh==tree.QueryColor(1,1,size,pos[u])) ans--;if(vh==tree.QueryColor(1,1,size,pos[v])) ans--;return ans;}int main(){//freopen("test.txt","r",stdin);int N,M,i,u,v,cnt;char ch;while(~scanf("%d%d",&N,&M)){Init();for(i=1;i<=N;++i)scanf("%d",weight+i);for(i=1,cnt=0;i<N;++i){scanf("%d%d",&u,&v);Add(u,v,cnt);}Dfs1(1),Dfs2(1),tree.Build(1,1,size);while(M--){scanf(" %c",&ch);if(ch=='C'){scanf("%d %d %d",&u,&v,&i);Update(u,v,i);}else{scanf("%d %d",&u,&v);printf("%d\n",Query(u,v));}}}return 0;}
0 0
原创粉丝点击