_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路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...
随机推荐
- MD5加密Java工具类
原文:http://www.open-open.com/code/view/1421764946296 import java.security.MessageDigest; public class ...
- pip命令自动补全功能;设置代理;使用国内源
这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...
- grep使用正则表达式搜索IP地址
递归搜索当前目录及其子目录.子目录的子目录……所包含文件是否包含IP地址 grep -r "[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit: ...
- 【深入探索c++对象模型】data语义学二
单一继承中,base class 和derived class的对象都是从相同的地址开始,其间差异只在于derived class比较大,用以容纳自己的nonstatic members. 若vptr ...
- topcoder srm 551
div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...
- [React] Extend styles with styled-components in React
In this lesson, you will learn how to extend styles from one styled-component to another in a React ...
- mysql手记
myisam innoDB是mysql经常使用的存储引擎 MyISAM不支持事务.也不支持外键.但其訪问速度快.对事务完整性没有要求. InnoDB存储引擎提供了具有提交.回滚和崩溃恢复能力的事务安全 ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- Ioc 器管理的应用程序设计,前奏:容器属于哪里? 控制容器的反转和依赖注入模式
Ioc 器管理的应用程序设计,前奏:容器属于哪里? 我将讨论一些我认为应该应用于“容器管理”应用程序设计的原则. 模式1:服务字典 字典或关联数组是我们在软件工程中学到的第一个构造. 很容易看到使 ...
- Socketclient与服务端
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR ...