BZOJ 2243: [SDOI2011]染色 (树剖+线段树)
树链剖分后两个区间合并的时候就判一下相交颜色是否相同来算颜色段数就行了.
CODE
#include <vector>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
#define ls (i<<1)
#define rs (i<<1|1)
inline void read(int &num) {
char ch; int flg=1; while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
for(num=0; isdigit(ch); num=num*10+ch-'0',ch=getchar()); num*=flg;
}
const int MAXN = 100005;
int n, q, w[MAXN], dfn[MAXN], seq[MAXN], tmr;
int fir[MAXN], cnt;
struct edge { int to, nxt; }e[MAXN<<1];
inline void add(int u, int v) {
e[cnt] = (edge){ v, fir[u] }, fir[u] = cnt++;
e[cnt] = (edge){ u, fir[v] }, fir[v] = cnt++;
}
int dep[MAXN], fa[MAXN], sz[MAXN], top[MAXN], son[MAXN];
void dfs(int u, int ff) {
dep[u] = dep[fa[u]=ff] + (sz[u]=1);
for(int v, i = fir[u]; ~i; i = e[i].nxt)
if((v=e[i].to) != fa[u]) {
dfs(v, u), sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int tp) {
top[u] = tp; seq[dfn[u] = ++tmr] = u;
if(son[u]) dfs2(son[u], tp);
for(int v, i = fir[u]; ~i; i = e[i].nxt)
if((v=e[i].to) != son[u] && v != fa[u]) dfs2(v, v);
}
struct node {
int lc, rc, sum;
node(){ sum = -1; }
node(int l, int r, int s):lc(l), rc(r), sum(s){}
inline node operator +(const node &o)const {
if(sum == -1) return o;
if(o.sum == -1) return *this;
if(rc == o.lc) return node(lc, o.rc, sum+o.sum-1);
else return node(lc, o.rc, sum+o.sum);
}
inline friend node rev(const node &o) { return node(o.rc, o.lc, o.sum); }
}col[MAXN<<2];
int tag[MAXN<<2];
inline void upd(int i) {
col[i] = col[ls] + col[rs];
}
inline void mt(int i) {
if(~tag[i]) {
col[ls] = col[rs] = node(tag[i], tag[i], 1);
tag[ls] = tag[rs] = tag[i]; //忘了写这个WA到自闭
tag[i] = -1;
}
}
void build(int i, int l, int r) {
tag[i] = -1;
if(l == r) {
col[i] = node(w[seq[l]], w[seq[l]], 1);
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid+1, r);
upd(i);
}
void cover(int i, int l, int r, int x, int y, int val) {
if(l == x && r == y) {
col[i] = node(val, val, 1);
tag[i] = val;
return;
}
int mid = (l + r) >> 1;
mt(i);
if(y <= mid) cover(ls, l, mid, x, y, val);
else if(x > mid) cover(rs, mid+1, r, x, y, val);
else cover(ls, l, mid, x, mid, val), cover(rs, mid+1, r, mid+1, y, val);
upd(i);
}
node query(int i, int l, int r, int x, int y) {
if(l == x && r == y) return col[i];
int mid = (l + r) >> 1; node res;
mt(i);
if(y <= mid) res = query(ls, l, mid, x, y);
else if(x > mid) res = query(rs, mid+1, r, x, y);
else res = query(ls, l, mid, x, mid) + query(rs, mid+1, r, mid+1, y);
upd(i);
return res;
}
inline node Query(int x, int y) {
node resx, resy;
while(top[x] != top[y]) {
if(dep[top[x]] > dep[top[y]]) resx = resx + rev(query(1, 1, n, dfn[top[x]], dfn[x])), x = fa[top[x]];
else resy = query(1, 1, n, dfn[top[y]], dfn[y]) + resy, y = fa[top[y]];
}
if(dfn[x] < dfn[y]) return resx + query(1, 1, n, dfn[x], dfn[y]) + resy;
else return resx + rev(query(1, 1, n, dfn[y], dfn[x])) + resy;
}
inline void Cover(int x, int y, int val) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
cover(1, 1, n, dfn[top[x]], dfn[x], val);
x = fa[top[x]];
}
if(dfn[x] < dfn[y]) swap(x, y);
cover(1, 1, n, dfn[y], dfn[x], val);
}
int main() {
read(n); read(q);
for(int i = 1; i <= n; ++i) fir[i] = -1, read(w[i]);
for(int i = 1, x, y; i < n; ++i)
read(x), read(y), add(x, y);
dfs(1, 0); dfs2(1, 1);
build(1, 1, n);
char s[2]; int x, y, z;
while(q--) {
while(!isalpha(s[0]=getchar()));
read(x), read(y);
if(s[0] == 'Q') printf("%d\n", Query(x, y).sum);
else read(z), Cover(x, y, z);
}
}
BZOJ 2243: [SDOI2011]染色 (树剖+线段树)的更多相关文章
- [CF1007D]Ants[2-SAT+树剖+线段树优化建图]
题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...
- BZOJ_2238_Mst_树剖+线段树
BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- BZOJ_2157_旅游_树剖+线段树
BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...
- 【BZOJ5210】最大连通子块和 树剖线段树+动态DP
[BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...
- [LNOI2014]LCA(树剖+线段树)
\(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...
- LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)
题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...
- BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)
传送门 完了今天才知道原来线段树的动态开点和主席树是不一样的啊 我们先考虑没有宗教信仰的限制,那么就是一个很明显的树剖+线段树,路径查询最大值以及路径和 然后有了宗教信仰的限制该怎么做呢? 先考虑暴力 ...
- 【bzoj4699】树上的最短路(树剖+线段树优化建图)
题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...
- POJ3237 Tree(树剖+线段树+lazy标记)
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...
随机推荐
- Docker CE 下载方式
1. 找到一个网址挺好的 https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/arm64/ mark 一下 以后用.
- Java判断指定日期是否为工作日
Java判断指定日期是否为工作日 转自:https://www.jianshu.com/p/966659492f2f 转:https://www.jianshu.com/p/05ccb5783f65转 ...
- Hadoop集群搭建-05安装配置YARN
Hadoop集群搭建-04安装配置HDFS Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hadoop集群搭建-01前期准备 先保证集群5台虚 ...
- 第二周Java课堂作业
演示一: public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size ...
- CSS:盒子的定位与浮动
CSS--盒子定位.浮动与居中 HTML中的每个元素都是一个盒子 浏览器对HTML文档进行解析,根据盒子的属性对其进行排列. 每个元素默认使用标准文档流定位 标准文档流:是指浏览器读取HTML ...
- Linux7_MySQL5.7_主从复制_scripts
# cat my_full_backup.sh #!/bin/bash BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"` format_time=`dat ...
- Spring实战(十二) Spring中注入AspectJ切面
1.Spring AOP与AspectJ Spring AOP与AspectJ相比,是一个功能比较弱的AOP解决方案. AspectJ提供了许多它不能支持的类型切点,如在创建对象时应用通知,构造器切点 ...
- 史上最全的MySQL高性能优化实战总结!
1.1 前言 MySQL对于很多Linux从业者而言,是一个非常棘手的问题,多数情况都是因为对数据库出现问题的情况和处理思路不清晰.在进行MySQL的优化之前必须要了解的就是MySQL的查询过程,很多 ...
- date和time
time和date两个函数在Lua中实现所有的时钟查询功能.函数time在没有参数时返回当前时钟的数值. t=os.date()print(t) 05/07/19 16:49:18 --------- ...
- centos7.4 安装 .net core 2.2
Step 1:首先注册Microsoft签名密钥,每台机器注册一次就行. sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/pack ...