BZOJ 2243 SDOI 2011染色
题目链接: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染色的更多相关文章
- [BZOJ 2243] [SDOI 2011] 染色 【树链剖分】
题目链接:BZOJ - 2243 题目分析 树链剖分...写了200+行...Debug了整整一天+... 静态读代码读了 5 遍 ,没发现错误,自己做小数据也过了. 提交之后全 WA . ————— ...
- [SDOI 2011]染色
Description 题库链接 给定一棵有 \(n\) 个节点的无根树和 \(m\) 个操作,操作有 \(2\) 类: 将节点 \(a\) 到节点 \(b\) 路径上所有点都染成颜色 \(c\) : ...
- [BZOJ 2242] [SDOI 2011] 计算器
Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...
- [BZOJ 2285] [SDOI 2011] 保密
Description 传送门 Solution 这道题的最大难点在于读懂题意(雾 分数规划求出 \(n\) 到 \(1\cdots n_1\) 每个点的最小 \(\sum\frac{t_i}{s_i ...
- 解题: SDOI 2011 染色
题面 强行把序列问题通过树剖套在树上...算了算是回顾了一下树剖的思想=.= 每次树上跳的时候注意跳的同时维护当前拼出来的左右两条链的靠上的端点,然后拼起来的时候讨论一下拼接点,最后一下左右两边的端点 ...
- BZOJ 2245 SDOI 2011 工作安排 费用流
题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...
- BZOJ 2243 染色 | 树链剖分模板题进阶版
BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...
- BZOJ 2243 染色(树链剖分好题)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 7971 Solved: 2990 [Submit][Stat ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
随机推荐
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- Ghost克隆软件
克隆软件Ghost初级使用教程 一.什么是Ghost ? Ghost(幽灵)软件是美国赛门铁克公司推出的一款出色的硬盘备份还原工具,可以实现FAT16.FAT32.NTFS.OS2等多种硬盘分区格式的 ...
- input 不支持HTML5的placeholder属性
解决方法如下: <input type="text" value="搜索乐高资讯" onfocus="if(this.value=='搜索乐高资 ...
- openpyxl
openpyxl库的使用,这个处理xlsx还是挺有用的 ref:传送门 from openpyxl import Workbook from openpyxl import load_workbook ...
- FB是磁珠的符号
本文来自:http://www.coofish.org/post/FB-cizhu.html今天实验室一哥们研究DSP电路图,发现图中有一个符号是FB,外形有点像电阻(R),但是不清楚是什么电子元器件 ...
- 各类XML parser的比较
基于以上的比较 再为公司的项目选择解析器的时候,我选择Xerces.准备把Qt自带的XML库给去掉. references: http://stackoverflow.com/questions/17 ...
- C++基础回顾2(函数, 指针和引用)
接着回顾函数.指针和应用. 函数 1.多维数组作为形参时,第一维的大小可以省略(也可以不省略),但是其他维的大小必须指定.比如二维数组形参,int array[3][]不正确,int arry[][1 ...
- 修改过mysql数据库字段内容默认值为当前时间
--添加CreateTime 设置默认时间 CURRENT_TIMESTAMP ALTER TABLE `table_name`ADD COLUMN `CreateTime` datetime N ...
- python删除指定位置 2个元素
# -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#排序说明:http://en.wikipedia.org/wiki/i ...
- (转)iOS7界面设计规范(6) - UI基础 - 模态情境
继续规范.现在听着Clapton的Wonderful Tonight,想想看,整个高二暑假都在为这首歌着迷,经常夜里一边做英语暑期作业一边循环这首歌,心里特别静的赶脚.13年过去了,再听起来,就像隔着 ...