题解:

树剖,线段树维护区间颜色段数

记录两端点的颜色,做到O(1)合并

问题:

  非递归建树实现

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=; int n,m;
int incolor[maxn]; int cntedge;
int head[maxn];
int to[maxn<<],nex[maxn<<];
int addedge(int x,int y){
nex[++cntedge]=head[x];
to[cntedge]=y;
head[x]=cntedge;
} int father[maxn],depth[maxn],siz[maxn];
int heavyson[maxn],top[maxn],idx[maxn],ref[maxn]; int dfs1(int now,int fa){
father[now]=fa;
depth[now]=depth[fa]+;
siz[now]=;
for(int i=head[now];i;i=nex[i]){
if(to[i]==fa)continue;
dfs1(to[i],now);
siz[now]+=siz[to[i]];
if(siz[to[i]]>siz[heavyson[now]]){
heavyson[now]=to[i];
}
}
}
int temp;
int dfs2(int now,int toppoint){
top[now]=toppoint;
idx[now]=(++temp);
ref[temp]=now;
if(!heavyson[now])return ;
dfs2(heavyson[now],toppoint);
for(int i=head[now];i;i=nex[i]){
if(to[i]==father[now])continue;
if(to[i]==heavyson[now])continue;
dfs2(to[i],to[i]);
}
} struct SegmentTree{
int l,r;
int lcolor,rcolor;
int setcolor,sum;
}tree[maxn<<];
int pushup(int now){
tree[now].lcolor=tree[now<<].lcolor;
tree[now].rcolor=tree[now<<|].rcolor;
if(tree[now<<].rcolor==tree[now<<|].lcolor){
tree[now].sum=tree[now<<].sum+tree[now<<|].sum-;
}else{
tree[now].sum=tree[now<<].sum+tree[now<<|].sum;
}
}
int pushdown(int now){
if(tree[now].setcolor!=-){
tree[now<<].setcolor=tree[now<<].lcolor=tree[now<<].rcolor=tree[now].setcolor;
tree[now<<|].setcolor=tree[now<<|].lcolor=tree[now<<|].rcolor=tree[now].setcolor;
tree[now<<].sum=tree[now<<|].sum=;
tree[now].setcolor=-;
}
} int Build(int now,int l,int r){
tree[now].l=l;tree[now].r=r;
tree[now].setcolor=-;
if(l==r){
tree[now].lcolor=tree[now].rcolor=incolor[ref[l]];
tree[now].sum=;
return ;
}
int mid=(l+r)>>;
Build(now<<,l,mid);
Build(now<<|,mid+,r);
pushup(now);
} int Updata(int now,int ll,int rr,int x){
if(tree[now].l>=ll&&tree[now].r<=rr){
tree[now].setcolor=tree[now].lcolor=tree[now].rcolor=x;
tree[now].sum=;
return ;
}
int mid=(tree[now].l+tree[now].r)>>;
pushdown(now);
if(ll<=mid)Updata(now<<,ll,rr,x);
if(rr>mid)Updata(now<<|,ll,rr,x);
pushup(now);
} int Querysum(int now,int ll,int rr){
if(tree[now].l>=ll&&tree[now].r<=rr){
return tree[now].sum;
}
int mid=(tree[now].l+tree[now].r)>>;
pushdown(now);
if(rr<=mid)return Querysum(now<<,ll,rr);
else if(ll>mid)return Querysum(now<<|,ll,rr);
else if(tree[now<<].rcolor==tree[now<<|].lcolor)return Querysum(now<<,ll,rr)+Querysum(now<<|,ll,rr)-;
else return Querysum(now<<,ll,rr)+Querysum(now<<|,ll,rr);
} int Querycolor(int now,int p){
if(tree[now].l==tree[now].r){
return tree[now].lcolor;
}
int mid=(tree[now].l+tree[now].r)>>;
pushdown(now);
if(p<=mid)return Querycolor(now<<,p);
else return Querycolor(now<<|,p);
} int Change(int u,int v,int x){
int tu=top[u];
int tv=top[v];
while(tu!=tv){
if(depth[tu]<depth[tv]){
swap(tu,tv);swap(u,v);
}
Updata(,idx[tu],idx[u],x);
u=father[tu];tu=top[u];
}
if(depth[u]>depth[v])swap(u,v);
Updata(,idx[u],idx[v],x);
} int Getans(int u,int v){
int ret=;
int tu=top[u];
int tv=top[v];
while(tu!=tv){
if(depth[tu]<depth[tv]){
swap(tu,tv);swap(u,v);
}
ret=ret+Querysum(,idx[tu],idx[u]);
u=father[tu];
if(Querycolor(,idx[u])==Querycolor(,idx[tu]))--ret;
tu=top[u];
}
if(depth[u]>depth[v])swap(u,v);
ret=ret+Querysum(,idx[u],idx[v]);
return ret;
} int minit(){
temp=cntedge=;
memset(heavyson,,sizeof(heavyson));
memset(head,,sizeof(head));
} int main(){
scanf("%d%d",&n,&m);
minit(); for(int i=;i<=n;++i)scanf("%d",&incolor[i]);
for(int i=;i<=n-;++i){
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
dfs1(,);
dfs2(,);
Build(,,n); char opty[];
while(m--){
int x,y,z;
scanf("%s",opty);
if(opty[]=='C'){
scanf("%d%d%d",&x,&y,&z);
Change(x,y,z);
}else{
scanf("%d%d",&x,&y);
printf("%d\n",Getans(x,y));
}
}
return ;
}

BZOJ:2243: [SDOI2011]染色的更多相关文章

  1. BZOJ 2243: [SDOI2011]染色 [树链剖分]

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6651  Solved: 2432[Submit][Status ...

  2. bzoj 2243 [SDOI2011]染色(树链剖分,线段树)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4637  Solved: 1726[Submit][Status ...

  3. Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5020  Solved: 1872[Submit][Status ...

  4. bzoj 2243: [SDOI2011]染色 线段树区间合并+树链剖分

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 7925  Solved: 2975[Submit][Status ...

  5. bzoj 2243: [SDOI2011]染色 (树链剖分+线段树 区间合并)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 9854  Solved: 3725[Submit][Status ...

  6. BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  7. BZOJ 2243: [SDOI2011]染色 树链剖分+线段树区间合并

    2243: [SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数 ...

  8. bzoj 2243 [SDOI2011]染色(树链剖分+线段树合并)

    [bzoj2243][SDOI2011]染色 2017年10月20日 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询 ...

  9. 洛谷 P2486 [SDOI2011]染色/bzoj 2243: [SDOI2011]染色 解题报告

    [SDOI2011]染色 题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同 ...

  10. BZOJ 2243 [SDOI2011]染色 (树链剖分)(线段树区间修改)

    [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6870  Solved: 2546[Submit][Status][Disc ...

随机推荐

  1. BugFix系列---开篇介绍

      这个系列的文章,主要目的在于积累总结实际开发中遇到的错误,记录下来自己的解决思路,用来提升自己. 不出意外,应该会持续不断的记录更新,在整个开发openstack的过程中,抓住机会吸取开源界大牛的 ...

  2. 【Unity】双击物体

    using UnityEngine; using System.Collections; using System; public class Click_yushe : MonoBehaviour ...

  3. k-近邻算法的优缺点及拓展思考

    //2019.08.03晚#k-近邻算法的拓展思考与总结1.k-近邻算法是一种非常典型的分类监督学习算法,它可以解决多分类的问题:另外,它的整体思想简单,效果强大.它也可以用来解决回归问题,使用的库函 ...

  4. is application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem

    最近试着了解 c++,接触到了QT,写了一个测试程序,在开发环境下正常后移到非开发环境,报错 网上找资料说是少了platforms文件夹中的dll,把里面所有的dll复制到执行程序目录,还是提示,继续 ...

  5. mybatis连接数据库出错获取不到SQLsession

    采用mybatis连接数据库时候出现的问题描述: 数据库连接配置正确,mybatis-config数据库等部分配置均正确,连接数据库是OK的 <properties resource=" ...

  6. Git闪退问题

    打开Git 会一闪而过.并出现一个错误的日志文件.自己尝试安装了几个不同的版本Git还是解决不了问题.后来自己在网上找了一些办法,并进行总结 1. 进入git目录下的bin目录执行rebase -b ...

  7. css调试与样式优先级

    如何查看一个标签的当前css样式 如上图所示 先用标签选择器选择某个标签 然后在elements区域就会自动找到该标签 然后在右侧的styles区域整个区域都是该标签的样式,从上到下是显示的优先级,被 ...

  8. Windows下 dmp文件的产生

    一.windows下的崩溃捕获windows程序当遇到异常,没有try-catch或者try-catch也无法捕获到的异常时,程序就会自动退出.windows系统默认是不产生程序dmp文件的.dump ...

  9. 046、Java中使用if…else判断

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. 项目上线后,遇到IE浏览器不显示大部分组件的问题

    document.addEventListener('touchmove',(evt)=>{ }) 以上是ES6 语法,箭头函数,当然在IE下是不行的啦. 所以改为:ES5语法 document ...