题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243

算法讨论:

树链剖分把树放到线段树上。然后线段树的每个节点要维护的东西有左端点的颜色,右端点的颜色,以及是否被改变过颜色,和颜色段数。

向上合并的过程中,要注意如果左孩子的右端点和右孩子的左端点颜色相同,那么就要把颜色段数减一。

然后我们考虑询问的问题:

对于一个询问,我们是按深度从下向上跳着计算的,所以每次统计一个路径的时候,我们要记录上一次的路径的两端点的颜色,如果本次路径计算的

右端点颜色和上次的左端点颜色相同,那么答案就要减去1.(注意树链剖分,如果在同一次线段树内查询,那么节点深度小的一定线段树的编号也小)。

当两个节点跳到一个重链上的时候,那么此时两边的颜色都要进行判断一下。至于为啥,很好想吧。

题目代码:

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype> using namespace std;
const int N = + ;
inline int read() {
int x = ;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)) {
x = x * + c - '';
c = getchar();
}
return x;
} struct SegmentTree {
int lc, rc, l, r, tag, sz;
}Node[N * ];
struct Edge {
int from, to, next;
}edges[N << ]; char ss[];
int n, m, cnt, pos, co[N], lco, rco;
int fa[N], head[N], son[N], size[N];
int num[N], top[N], depth[N], seg[N]; void insert(int from, int to) {
++ cnt;
edges[cnt].from = from; edges[cnt].to = to;
edges[cnt].next = head[from]; head[from] = cnt;
} void dfs_1(int u, int f) {
fa[u] = f; size[u] = ;
for(int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
if(v != f) {
depth[v] = depth[u] + ;
dfs_1(v, u);
size[u] += size[v];
if(!son[u] || size[v] > size[son[u]])
son[u] = v;
}
}
} void dfs_2(int u, int ances) {
top[u] = ances;
num[u] = ++ pos;
seg[pos] = u;
if(!son[u]) return;
dfs_2(son[u], ances);
for(int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
if(v != fa[u] && v != son[u]) {
dfs_2(v, v);
}
}
} void pushdown(int o) {
if(Node[o].l == Node[o].r) return;
int l = o << , r = o << | ;
if(Node[o].tag) {
Node[l].tag = Node[r].tag = Node[o].tag;
Node[l].lc = Node[l].rc = Node[o].tag;
Node[r].lc = Node[r].rc = Node[o].tag;
Node[l].sz = Node[r].sz = ;
Node[o].tag = ;
}
} void pushup(int o) {
if(Node[o].l == Node[o].r) return;
int l = o << , r = o << | ;
Node[o].lc = Node[l].lc;
Node[o].rc = Node[r].rc;
Node[o].sz = Node[l].sz + Node[r].sz - (Node[l].rc == Node[r].lc);
} void build(int o, int l, int r) {
Node[o].l = l; Node[o].r = r; Node[o].tag = ;
if(l == r) {
Node[o].sz = ;
Node[o].lc = Node[o].rc = co[seg[l]];
return;
}
int mid = (l + r) >> ;
build(o << , l, mid); build(o << | , mid + , r);
pushup(o);
} void update(int o, int l, int r, int v) {
if(Node[o].l == l && Node[o].r == r) {
Node[o].lc = Node[o].rc = v;
Node[o].tag = v; Node[o].sz = ;
return;
}
int mid = (Node[o].l + Node[o].r) >> ;
pushdown(o);
if(r <= mid) update(o << , l, r, v);
else if(l > mid) update(o << | , l, r, v);
else {
update(o << , l, mid, v);
update(o << | , mid + , r, v);
}
pushup(o);
} int query(int o, int l, int r, int L, int R) {
if(Node[o].l == L) lco = Node[o].lc;
if(Node[o].r == R) rco = Node[o].rc;
if(Node[o].l == l && Node[o].r == r) {
return Node[o].sz;
}
int mid = (Node[o].l + Node[o].r) >> ;
pushdown(o);
if(r <= mid) return query(o << , l, r, L, R);
else if(l > mid) return query(o << | , l, r, L, R);
else {
return query(o << , l, mid, L, R) + query(o << | , mid + , r, L, R) - (Node[o << ].rc == Node[o << | ].lc);
}
pushup(o);
} void Update(int x, int y, int z) {
int f1 = top[x], f2 = top[y];
while(f1 != f2) {
if(depth[f1] < depth[f2]) {
swap(x, y); swap(f1, f2);
}
update(, num[f1], num[x], z);
x = fa[f1]; f1 = top[x];
}
if(depth[x] < depth[y]) {
update(, num[x], num[y], z);
}
else {
update(, num[y], num[x], z);
}
} int Query(int x, int y) {
int f1 = top[x], f2 = top[y], res = ;
int ans1 = -, ans2 = -;
while(f1 != f2) {
if(depth[f1] < depth[f2]) {
swap(f1, f2); swap(x, y);
swap(ans1, ans2);
}
res += query(, num[f1], num[x], num[f1], num[x]);
if(rco == ans1) res --; ans1 = lco;
x = fa[f1]; f1 = top[x];
}
if(depth[x] < depth[y]) {
swap(x, y); swap(ans1, ans2);
}
res += query(, num[y], num[x], num[y], num[x]);
if(rco == ans1) res --;
if(lco == ans2) res --;
return res;
} int main() {
int x, y, z;
n = read(); m = read();
for(int i = ; i <= n; ++ i) co[i] = read();
for(int i = ; i < n; ++ i) {
x = read(); y = read();
insert(x, y); insert(y, x);
}
depth[] = ;
dfs_1(, -); dfs_2(, );
build(, , n);
for(int i = ; i <= m; ++ i) {
scanf("%s", ss);
if(ss[] == 'C') {
x = read(); y = read(); z = read();
Update(x, y, z);
}
else {
x = read(); y = read();
printf("%d\n", Query(x, y));
}
}
return ;
}

BZOJ2243

BZOJ 2243 SDOI 2011染色的更多相关文章

  1. [BZOJ 2243] [SDOI 2011] 染色 【树链剖分】

    题目链接:BZOJ - 2243 题目分析 树链剖分...写了200+行...Debug了整整一天+... 静态读代码读了 5 遍 ,没发现错误,自己做小数据也过了. 提交之后全 WA . ————— ...

  2. [SDOI 2011]染色

    Description 题库链接 给定一棵有 \(n\) 个节点的无根树和 \(m\) 个操作,操作有 \(2\) 类: 将节点 \(a\) 到节点 \(b\) 路径上所有点都染成颜色 \(c\) : ...

  3. [BZOJ 2242] [SDOI 2011] 计算器

    Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...

  4. [BZOJ 2285] [SDOI 2011] 保密

    Description 传送门 Solution 这道题的最大难点在于读懂题意(雾 分数规划求出 \(n\) 到 \(1\cdots n_1\) 每个点的最小 \(\sum\frac{t_i}{s_i ...

  5. 解题: SDOI 2011 染色

    题面 强行把序列问题通过树剖套在树上...算了算是回顾了一下树剖的思想=.= 每次树上跳的时候注意跳的同时维护当前拼出来的左右两条链的靠上的端点,然后拼起来的时候讨论一下拼接点,最后一下左右两边的端点 ...

  6. BZOJ 2245 SDOI 2011 工作安排 费用流

    题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...

  7. BZOJ 2243 染色 | 树链剖分模板题进阶版

    BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...

  8. BZOJ 2243 染色(树链剖分好题)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 7971  Solved: 2990 [Submit][Stat ...

  9. [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)

    [BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...

随机推荐

  1. 树形dp入门

    poj2057 某公司的上下级关系是一颗树状结构,每个人不能与他的上司同时出现,每个人有一个值,求最大值. 这个题需要注意的是如果不保存状态会超时,这似乎也是大部分dp应该注意的事情啊 #includ ...

  2. uva 10929 - You can say 11

    #include <cstdio> using namespace std; ]; int main() { while(gets(in)) { ] == ] == ) break; ; ...

  3. 如何使用JSONP

    1.使用$.getJSON() $.getJSON(" http://跨域的dns/document!searchJSONResult.action?name1="+value1+ ...

  4. ORACLE主要的系统表和系统视图

    ORACLE主要的系统表和系统视图 1.系统表 ORACLE数据库的系统参数都存储在数据库中,可以通过SQLPLUS,以用户SYS进行查询.几个重要的表或者视图如下: v$controlfile:控制 ...

  5. amchart

    amchart能够根据提供的数据便捷的生成好看的图标,曾在项目中遇到使用falsh版以支持对js支持不好的低版本浏览器,但是现在官网上都是js版本的,flash版的文档都没有,搜索结果一般都是链接到博 ...

  6. Typecho中文验证码Captcha插件

    前言实在是受不了每天都要删除掉上百条的垃圾评论,干脆自己做了个验证码插件,顺带做的完善了些,分享给大家. 本插件是在评论验证码插件基础上完善而来.所不同的是,采用了最新的securimage 3.0. ...

  7. HTML&CSS基础学习笔记1-简单网页中有哪些标签?

    一个简单网页中有哪些HTML标签? 平时我们看到的网页,都是由HTML的标签来组成的.HTML标签非常多,我们先来认识一部分. 1. <html></html>称为根标签,所有 ...

  8. 自制单片机之十……AT89S51的上拉电阻问题

    很多网友都问我AT89S51的P0口为什么要接一个上拉电阻.我就用一个篇幅来说一说 P0口和其它三个口的内部电路是不同的,如下图 P0口是接在两个三极管D0和D1之间的,而P1-P3口的上部是接一个电 ...

  9. Java Json开源解析包 google-gson download(下载)

    官方下载地址:http://code.google.com/p/google-gson/ http://files.cnblogs.com/hnrainll/google-gson-2.1-relea ...

  10. cf437B The Child and Set

    B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input standard ...