2243: [SDOI2011]染色

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 7925  Solved: 2975
[Submit][Status][Discuss]

Description

给定一棵有n个节点的无根树和m个操作,操作有2类:

1、将节点a到节点b路径上所有点都染成颜色c;

2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。

请你写一个程序依次完成这m个操作。

Input

第一行包含2个整数n和m,分别表示节点数和操作数;

第二行包含n个正整数表示n个节点的初始颜色

下面 行每行包含两个整数x和y,表示xy之间有一条无向边。

下面 行每行描述一个操作:

“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;

“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。

Output

对于每个询问操作,输出一行答案。

Sample Input

6 5

2 2 1 2 1 1

1 2

1 3

2 4

2 5

2 6

Q 3 5

C 2 1 1

Q 3 5

C 5 1 2

Q 3 5

Sample Output

3

1

2

HINT

数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。

Source

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct edge
{
int v,next;
} edge[N<<];
int head[N<<],edg,id,n;
/// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int a[N],ran[N],top[N],tid[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
edg=;
id=;
} void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
} void dfs1(int u,int fath,int deep)
{
fa[u]=fath;
siz[u]=;
dep[u]=deep;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fath)continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
} void dfs2(int u,int tp)
{
tid[u]=++id;
top[u]=tp;
ran[tid[u]]=u;
if(son[u]==-)return;
dfs2(son[u],tp);
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u])continue;
if(v!=son[u])
dfs2(v,v);
}
} struct SGT
{
int la[N<<],ra[N<<],ma[N<<],lazy[N<<];
void pushup(int pos)
{
if(ra[pos<<]==la[pos<<|])ma[pos]=ma[pos<<]+ma[pos<<|]-;
else ma[pos]=ma[pos<<|]+ma[pos<<];
la[pos]=la[pos<<];
ra[pos]=ra[pos<<|];
}
void pushdown(int pos)
{
if(lazy[pos])
{
la[pos<<]=la[pos<<|]=lazy[pos];
ra[pos<<]=ra[pos<<|]=lazy[pos];
ma[pos<<]=ma[pos<<|]=;
lazy[pos<<]=lazy[pos<<|]=lazy[pos];
lazy[pos]=;
}
}
pair<int,pair<int,int> > Union( pair<int,pair<int,int> > a, pair<int,pair<int,int> > b)
{
if(a.second.second==b.second.first)
return make_pair(a.first+b.first-,make_pair(a.second.first,b.second.second));
return make_pair(a.first+b.first,make_pair(a.second.first,b.second.second));
}
void build(int l,int r,int pos)
{
lazy[pos]=;
if(l==r)
{
la[pos]=ra[pos]=a[ran[l]];
ma[pos]=;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int L,int R,int c,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
lazy[pos]=c;
la[pos]=ra[pos]=c;
ma[pos]=;
return;
}
pushdown(pos);
int mid=(l+r)>>;
if(L<=mid)update(L,R,c,l,mid,pos<<);
if(R>mid)update(L,R,c,mid+,r,pos<<|);
pushup(pos);
}
pair<int,pair<int,int> > query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)
return make_pair(ma[pos],make_pair(la[pos],ra[pos]));
pushdown(pos);
int mid=(l+r)>>;
if(L>mid)return query(L,R,mid+,r,pos<<|);
else if(R<=mid)return query(L,R,l,mid,pos<<);
else
{
pair<int,pair<int,int> > a=query(L,mid,l,mid,pos<<);
pair<int,pair<int,int> > b=query(mid+,R,mid+,r,pos<<|);
return Union(a,b);
}
}
}tree; int lca(int l,int r)
{
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
l=fa[top[l]];
}
if(dep[l]<dep[r])swap(l,r);
return r;
}
int up(int l,int r)
{
int pre=-,ans=;
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
pair<int,pair<int,int> > x=tree.query(tid[top[l]],tid[l],,n,);
//cout<<tid[top[l]]<<" "<<tid[l]<<" "<<x.first<<endl;
ans+=x.first;
if(pre==x.second.second)ans--;
pre=x.second.first;
l=fa[top[l]];
}
if(dep[l]<dep[r])swap(l,r);
pair<int,pair<int,int> > x=tree.query(tid[r],tid[l],,n,);
//cout<<tid[r]<<" "<<tid[l]<<" "<<x.first<<endl;
ans+=x.first;
if(pre==x.second.second)ans--;
return ans;
}
void go(int l,int r,int c)
{
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
tree.update(tid[top[l]],tid[l],c,,n,);
l=fa[top[l]];
}
if(dep[l]<dep[r])swap(l,r);
tree.update(tid[r],tid[l],c,,n,);
}
char ch[];
int main()
{
init();
int q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(,-,);
dfs2(,);
tree.build(,n,);
while(q--)
{
int u,v;
scanf("%s%d%d",ch,&u,&v);
if(ch[]=='C')
{
int c;
scanf("%d",&c);
go(u,v,c);
}
else
{
int x=lca(u,v);
printf("%d\n",up(u,x)+up(v,x)-);
}
}
return ;
}

bzoj 2243: [SDOI2011]染色 线段树区间合并+树链剖分的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. BZOJ 2243: [SDOI2011]染色 (树链剖分+线段树合并)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 树链剖分的点剖分+线段树.漏了一个小地方,调了一下午...... 还是要细心啊! 结 ...

随机推荐

  1. Array和ArrayList不同

    Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...

  2. Tomcat启动程序端口冲突、确认相应进程及杀死冲突进程的解决方案

    一. 查看所有进程占用的端口 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 二.查看占用指定端口的程序(1)命令窗口输出 命令:netstat –ano | findstr & ...

  3. Python笔记 #19# 实现bpnn

    代码编辑&解释工具:Jupyter Notebook 快速入门 形象说明BP神经网络的用法(图片来自推特): Bpnn类最主要的三个方法: initialize方法,用于设定神经网络的层数.各 ...

  4. 如何使用 lsyncd 实时同步并执行 shell 命令

    修改 lsyncd 的默认配置,不直接执行rsync 进行同步,而是改用自己的脚本. binary 指定我们的脚本 vim /usr/local/lsyncd/etc/lsyncd.conf sett ...

  5. 基于SSL的MySQL主从

    master 端 配置CA和证书 [root@baseos-1_192.168.31.140 ~]# cd /etc/pki/CA/ #生成根证书的私钥 [root@baseos-1_192.168. ...

  6. Golang原生sql操作Mysql数据库增删改查

    Golang要操作mysql数据库,首先需要在当期系统配置GOPATH,因为需要使用go get命令把驱动包下载到GOPATH下使用. 首先配置好你的GOPATH,执行以下命令,下载安装mysql驱动 ...

  7. spring使用@Autowired为抽象父类注入依赖

    有时候为了管理或者避免不一致性,希望具体服务统一继承抽象父类,同时使用@Autowired为抽象父类注入依赖.搜了了网上,有些解决方法实现实在不敢恭维,靠子类去注入依赖,那还有什么意义,如下: 父类: ...

  8. android拨号

    android:textColor="#A0ff1400" A0表示透明度00完全透明FF完全不透明,后面6位是RGB问题:类中类的findViewById方法为何用不了?解:类中 ...

  9. php 输出缓存,每秒打印一个数字

    <?php set_time_limit(0); //以上三行不加上nginx下不执行,一次性显示出来 header('Content-Type: text/event-stream'); // ...

  10. 牛客网数据库SQL实战(16-20)

    16.统计出当前各个title类型对应的员工当前薪水对应的平均工资.结果给出title以及平均工资avg.CREATE TABLE `salaries` (`emp_no` int(11) NOT N ...