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:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
随机推荐
- MongoDB资料--Java驱动, Hadoop驱动, Spark使用
MongoDB数据库备份: mongodump -h 192.168.1.160 -d MapLoc -o /usr/local/myjar/mongo/MapLoc/数据库还原:mongoresto ...
- 字符串时间日期转为Date格式和long格式
public static Long compare_date(String DATE1, String DATE2) { DateFormat df = new SimpleDateFormat(& ...
- ubuntu配置android开发环境和编译源码遇到的一些问题
---------------------------------------------环境变量设置--------------------------------------------- 1.设 ...
- hdu2089:不要62(基础数位dp)
题意:规定一个合法的号码不能含有4或者是连续的62 给定区间[n,m] 问此区间内合法的号码的个数 分析:数位dp dp[i][j]代表 最高位为 j 的 i 位数有多少个合法的 然后按题目规则进行转 ...
- 按每k个结点反转链表
//按每k个结点反转链表 Node* turn_k(Node* head,int n,int k) { Node* p=head; ;t<k;t++) p=p->next; //为了获取最 ...
- JAVA并发实现二(线程中止)
package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...
- iphone真机开发流程之--证书申请
一.申请 1.进入ios开发者中心 http://www.apple.com.cn/developer/ios/index.html 2.点击登录 输入用户名和密码(前提:已经有Apple ID,且花 ...
- Pojo和JavaBean的区别(转载)
OJO(Plain Old Java Object)这个名字用来强调它是一个普通java对象,而不是一个特殊的对象. 2005年11月时,“POJO”主要用来指代那些没用遵从特定的Java对象模型,约 ...
- 高性能MySql进化论【转】
高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...
- Understanding Abstractions of Secure Channels 的研读