_bzoj2243 [SDOI2011]染色【树链剖分】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2243
裸的树链剖分,最开始我保存一个线段树节点的color值时(若有多种颜色则为-1),不小心使“若其两个子节点color相等,则该节点color值为其儿子的color,并把这个节点内颜色个数设为1”,这是致命的错误——万一两个儿子color都为-1呢?
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> const int maxn = 100005, maxm = 100005; int n, m, ini_c[maxn], t1, t2, t3, root;
int head[maxn], to[maxn << 1], next[maxn << 1], lb;
int fa[maxn], heavy[maxn], top[maxn], siz[maxn], dep[maxn], id[maxn], a[maxn], cnt;
char opr;
struct Node {
int ql, qr, c, num, lc, rc;
} tree[maxn << 2]; inline void ist(int aa, int ss) {
to[lb] = ss;
next[lb] = head[aa];
head[aa] = lb;
++lb;
}
void dfs1(int r, int dp) {
dep[r] = dp;
siz[r] = 1;
if (r == 6671) {
++r;
--r;
}
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != fa[r]) {
fa[to[j]] = r;
dfs1(to[j], dp + 1);
siz[r] += siz[to[j]];
if (siz[to[j]] > siz[heavy[r]]) {
heavy[r] = to[j];
}
}
}
}
void dfs2(int r, int tp) {
if (!r) {
return;
}
id[r] = ++cnt;
a[cnt] = ini_c[r];
top[r] = tp;
dfs2(heavy[r], tp);
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != fa[r] && to[j] != heavy[r]) {
dfs2(to[j], to[j]);
}
}
}
void make_tree(int p, int left, int right) {
tree[p].ql = left;
tree[p].qr = right;
if (left == right) {
tree[p].c = tree[p].lc = tree[p].rc = a[left];
tree[p].num = 1;
return;
}
int mid = (left + right) >> 1;
make_tree(p << 1, left, mid);
make_tree(p << 1 | 1, mid + 1, right);
if (tree[p << 1].c == tree[p << 1 | 1].c && tree[p << 1].c != -1) {
tree[p].c = tree[p].lc = tree[p].rc = tree[p << 1].c;
tree[p].num = 1;
}
else {
tree[p].c = -1;
tree[p].lc = tree[p << 1].lc;
tree[p].rc = tree[p << 1 | 1].rc;
tree[p].num = tree[p << 1].num + tree[p << 1 | 1].num;
if (tree[p << 1].rc == tree[p << 1 | 1].lc) {
--tree[p].num;
}
}
}
void upd(int p, int left, int right, int c) {
if (tree[p].ql == left && tree[p].qr == right) {
tree[p].c = tree[p].lc = tree[p].rc = c;
tree[p].num = 1;
return;
}
if (tree[p].c >= 0) {
if (tree[p].c == c) {
return;
}
tree[p << 1].c = tree[p << 1 | 1].c = tree[p].c;
tree[p << 1].lc = tree[p << 1].rc = tree[p].c;
tree[p << 1 | 1].lc = tree[p << 1 | 1].rc = tree[p].c;
tree[p << 1].num = tree[p << 1 | 1].num = 1;
tree[p].c = -1;
}
int mid = (tree[p].ql + tree[p].qr) >> 1;
if (right <= mid) {
upd(p << 1, left, right, c);
}
else if (left > mid) {
upd(p << 1 | 1, left, right, c);
}
else {
upd(p << 1, left, mid, c);
upd(p << 1 | 1, mid + 1, right, c);
}
tree[p].c = -1;
tree[p].lc = tree[p << 1].lc;
tree[p].rc = tree[p << 1 | 1].rc;
tree[p].num = tree[p << 1].num + tree[p << 1 | 1].num;
if (tree[p << 1].rc == tree[p << 1 | 1].lc) {
--tree[p].num;
}
}
int qry(int p, int left, int right, int & lc, int & rc) {
if (tree[p].ql == left && tree[p].qr == right) {
lc = tree[p].lc;
rc = tree[p].rc;
return tree[p].num;
}
if (tree[p].c >= 0) {
lc = rc = tree[p].c;
return 1;
}
int mid = (tree[p].ql + tree[p].qr) >> 1;
if (right <= mid) {
return qry(p << 1, left, right, lc, rc);
}
else if (left > mid) {
return qry(p << 1 | 1, left, right, lc, rc);
}
else {
int tlc, trc, rt1, rt2;
rt1 = qry(p << 1, left, mid, lc, trc);
rt2 = qry(p << 1 | 1, mid + 1, right, tlc, rc);
return rt1 + rt2 - (trc == tlc? 1: 0);
}
}
inline void C(int u, int v, int c) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) {
std::swap(u, v);
}
upd(1, id[top[u]], id[u], c);
u = fa[top[u]];
}
if (dep[u] < dep[v]) {
std::swap(u, v);
}
upd(1, id[v], id[u], c);
}
inline int Q(int u, int v) {
int rt = 0;
int u_lc1 = -666, u_rc1 = -666, u_lc2 = -666, u_rc2 = -666;
int v_lc1 = -666, v_rc1 = -666, v_lc2 = -666, v_rc2 = -666;
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
rt += qry(1, id[top[u]], id[u], u_lc2, u_rc2);
if (u_rc2 == u_lc1) {
--rt;
}
u_lc1 = u_lc2;
u_rc1 = u_rc2;
u = fa[top[u]];
}
else {
rt += qry(1, id[top[v]], id[v], v_lc2, v_rc2);
if (v_rc2 == v_lc1) {
--rt;
}
v_lc1 = v_lc2;
v_rc1 = v_rc2;
v = fa[top[v]];
}
}
if (dep[u] > dep[v]) {
rt += qry(1, id[v], id[u], u_lc2, u_rc2);
if (u_rc2 == u_lc1) {
--rt;
}
if (u_lc2 == v_lc1) {
--rt;
}
}
else {
rt += qry(1, id[u], id[v], v_lc2, v_rc2);
if (v_rc2 == v_lc1) {
--rt;
}
if (v_lc2 == u_lc1) {
--rt;
}
}
return rt;
} int main(void) {
//freopen("paint.in", "r", stdin);
//freopen("paint.out", "w", stdout);
memset(head, -1, sizeof head);
memset(next, -1, sizeof next);
unsigned seed;
scanf("%d%d", &n, &m);
seed += n + m;
for (int i = 1; i <= n; ++i) {
scanf("%d", ini_c + i);
seed += ini_c[i];
}
for (int i = 1; i < n; ++i) {
scanf("%d%d", &t1, &t2);
ist(t1, t2);
ist(t2, t1);
seed += t1;
} srand(seed);
root = rand() % n + 1;
//root = 1;
dfs1(root, 0);
dfs2(root, root);
make_tree(1, 1, n); while (m--) {
while ((opr = getchar()) < 'A');
if (opr == 'C') {
scanf("%d%d%d", &t1, &t2, &t3);
C(t1, t2, t3);
}
else {
scanf("%d%d", &t1, &t2);
printf("%d\n", Q(t1, t2));
}
}
return 0;
}
_bzoj2243 [SDOI2011]染色【树链剖分】的更多相关文章
- BZOJ 2243: [SDOI2011]染色 [树链剖分]
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6651 Solved: 2432[Submit][Status ...
- bzoj-2243 2243: [SDOI2011]染色(树链剖分)
题目链接: 2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6267 Solved: 2291 Descript ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5020 Solved: 1872[Submit][Status ...
- bzoj2243[SDOI2011]染色 树链剖分+线段树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 9012 Solved: 3375[Submit][Status ...
- BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- BZOJ 2243: [SDOI2011]染色 树链剖分+线段树区间合并
2243: [SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数 ...
- 2243: [SDOI2011]染色(树链剖分+线段树)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 8400 Solved: 3150[Submit][Status ...
- Luogu P2486 [SDOI2011]染色(树链剖分+线段树合并)
Luogu P2486 [SDOI2011]染色 题面 题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例: 6 5 2 2 1 2 1 1 1 ...
- [bzoj 2243]: [SDOI2011]染色 [树链剖分][线段树]
Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...
随机推荐
- wikioi 2147 bitset+map解决
题目描写叙述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘宝上买下来了一个高级望远镜.他十分开心.于是他晚上去操场上看星星. 不同的星星发出不同的光,他的望远镜能够计算 ...
- HDU 4115 Eliminate the Conflict(2-sat)
HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...
- centos 5.11修改ssh默认端口号
查看下服务器端口号范围: # sysctl -a|grep ip_local_port_range net.ipv4.ip_local_port_range = 32768 61000 新 ...
- vue :src 文件路径错误
首先先说明下vue-cli的assets和static的两个文件的区别,因为这对你理解后面的解决办法会有所帮助 assets:在项目编译的过程中会被webpack处理解析为模块依赖,只支持相对路径的形 ...
- Zookeeper 简单操作
1. 连接到zookeeper服务 [java2000_wl@localhost zookeeper-3]$ bin/zkCli.sh -server 127.0.0.1:2181 也可以连接远端的 ...
- 大数据处理之道 (htmlparser获取数据<一>)
一:简单介绍 (1)HTML Parser是一个用于解析Html的Java的库.可採用线性或嵌套两种方式.主要用于网页的转换或提取,他有一些特性:过滤器filter,遍历器visitors,通常的标签 ...
- Ubuntu下配置Tomcat以指定(非root)身份执行
My Blog:http://www.outflush.com/ 通常情况下.在配置Tomcat生产环境时,一般会配置Tomcat以特定的身份执行(非root).这样有利于提高安全性,防止站点被黑后的 ...
- Navicat for MySQL中文破解版免费下载
不知道用过MySQL workbench数据库的朋友或站长有没有都遇到过这样的问题? 就是用ssh的连接方式连接数据库的时候,第一次可以连接的上,但是第二次就不行了,以后再用workbench就再也连 ...
- bzoj2101【Usaco2010 Dec】Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 418 Solved: ...
- HDU 5056 Boring count(不超过k个字符的子串个数)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...