[Luogu] 染色
https://www.luogu.org/problemnew/show/P2486
qizha
为什么会wa
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring> using std:: cin;
using std:: cout;
using std:: endl;
using std:: swap;
using std:: string; #define gc getchar() const int N = 1e5 + ; //...数据
int n, m, data[N];
//...图
int now = , head[N];
struct Node {int u, v, nxt;} G[N << ];
//...树剖
int tim;
int fa[N], deep[N], size[N], topp[N], son[N], tree[N], bef[N];
//...线段树
int L[N << ], R[N << ], F[N << ], l_col[N << ], r_col[N << ], tot[N << ];
//...查询操作
int Lc, Rc, ans; inline int read() {
int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x * f;
} inline void Add(int u, int v) {G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;} void Dfs_find_son(int u, int f_, int dep) {
fa[u] = f_;
deep[u] = dep;
size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != f_) {
Dfs_find_son(v, u, dep + );
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
} void Dfs_to_un(int u, int tp) {
topp[u] = tp;
tree[u] = ++ tim;
bef[tim] = u;
if(! son[u]) return ;
Dfs_to_un(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_to_un(v, v);
}
} #define lson jd << 1
#define rson jd << 1 | 1 void Up(int jd) {
l_col[jd] = l_col[lson]; r_col[jd] = r_col[rson];
tot[jd] = tot[lson] + tot[rson];
if(r_col[lson] == l_col[rson]) tot[jd] --;
} void Build_tree(int l, int r, int jd) {
L[jd] = l; R[jd] = r; F[jd] = -;
if(l == r) {
l_col[jd] = r_col[jd] = data[bef[l]];
tot[jd] = ;
return ;
}
int mid = (l + r) >> ;
Build_tree(l, mid, lson);
Build_tree(mid + , r, rson);
Up(jd);
} void Down_f(int jd) {
F[lson] = F[rson] = F[jd];
tot[lson] = tot[rson] = ;
l_col[lson] = r_col[lson] = l_col[rson] = r_col[rson] = F[jd];
F[jd] = -;
return ;
} void Sec_A(int l, int r, int jd, int x, int y) {
if(x == L[jd]) Lc = l_col[jd];
if(y == R[jd]) Rc = r_col[jd];
if(x <= l && r <= y) {ans += tot[jd]; return ;}
if(F[jd] != -) Down_f(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
if(x <= mid && y > mid && r_col[lson] == l_col[rson]) ans --;
} inline int Sec_A_imp(int x, int y) {
int tp1 = topp[x], tp2 = topp[y], ret = , prex = -, prey = -;
while(tp1 != tp2) {
if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(prex, prey), swap(x, y);
ans = ;
Sec_A(, n, , tree[tp1], tree[x]);
ret += ans;
if(Rc == prex) ret --;
prex = Lc;
x = fa[tp1];
tp1 = topp[x];
}
if(deep[x] < deep[y]) swap(x, y), swap(prex, prey);
ans = ;
Sec_A(, n, , tree[y], tree[x]);
ret += ans;
if(Lc == prey) ret --;
if(Rc == prex) ret --;
return ret;
} void Sec_G(int l, int r, int jd, int x, int y, int how) {
if(x <= l && r <= y) {
F[jd] = how; tot[jd] = ; l_col[jd] = r_col[jd] = how;
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, how);
if(y > mid) Sec_G(mid + , r, rson, x, y, how);
Up(jd);
} void Sec_G_imp(int x, int y, int how) {
int tp1 = topp[x], tp2 = topp[y];
while(tp1 != tp2) {
if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(x, y);
Sec_G(, n, , tree[tp1], tree[x], how);
x = fa[tp1];
tp1 = topp[x];
}
if(deep[x] < deep[y]) swap(x, y);
Sec_G(, n, , tree[y], tree[x], how);
return ;
} #define RR freopen("gg.in", "r", stdin) int main() {
//RR;
n = read(); m = read();
for(int i = ; i <= n; i ++) data[i] = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read();
Add(u, v); Add(v, u);
}
Dfs_find_son(, , );
Dfs_to_un(, );
Build_tree(, n, );
while(m --) {
string s;
cin >> s;
if(s[] == 'Q') {
int x = read(), y = read();
cout << Sec_A_imp(x, y) << endl;
} else {
int x = read(), y = read(), how = read();
Sec_G_imp(x, y, how);
}
}
return ;
}
/*
6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5
*/
[Luogu] 染色的更多相关文章
- [Luogu 2486] SDOI2011 染色
[Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...
- Luogu P2486 [SDOI2011]染色(树链剖分+线段树合并)
Luogu P2486 [SDOI2011]染色 题面 题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例: 6 5 2 2 1 2 1 1 1 ...
- LOJ #2527 Luogu P4491「HAOI2018」染色
好像网上没人....和我推出....同一个式子啊..... LOJ #2527 Luogu P4491 题意 $ n$个格子中每个格子可以涂$ m$种颜色中的一种 若有$ k$种颜色恰好涂了$ s$格 ...
- luogu P2486 [SDOI2011]染色
树剖做法: 就是两个dfs+一个线段树 难度的取决基本==线段树的维护难度 所以对有点线段树基础的,树剖也不难做吧 这里操作有二 一:两点间路径染色 线段树的区间赋值操作 二:查询路径段的个数 考虑线 ...
- Luogu P2486 染色(树链剖分+线段树)
题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...
- 【Luogu】P3155叶子的染色(树形DP)
题目链接 树形DP水题qwq. 设f[i][j]是以i为根的子树,染成j色,且满足内部需求的最少染色节点数. 设to是x的子节点,那么状态转移方程如此设计: 1.f[i][0] 这个状态表示i不染色, ...
- 【Luogu】P1330封锁阳光大学(bfs染色)
题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...
- BZOJ 4823 Luogu P3756 老C的方块 染色+最小割
题面太长了请各位自行品尝—>老C的方块 分析: 我们要解决掉所有使人弃疗的组合,还要保证花费最小,容易想到最小割(当然你要是想费用流的话,我们就没办法定义流量了) 我们来分析一下那些令人弃疗的组 ...
- luogu 4429 染色
bjoi 2018 染色 推了个错误结论得了60分? 题目大意: 一个无重边和自环的无向图,并且对每个点分别给了一个大小为2的颜色集合,只能从这个集合中选一种颜色给这个点染色 求一个染色方案使得没有两 ...
随机推荐
- java——内存中的数组
数组是一种引用类型,数组引用变量只是一个引用,数组元素和数组变量在内存中时分开存放的,下面我们看一下基本类型的数组和引用类型的数组在内存中的地址分布情况 基本类型数组: 我们先来看一段代码: publ ...
- 转------深入理解--Java按值传递和按引用传递
引言 最近刷牛客网上的题目时碰到不少有关Java按值传递和按引用传递的问题,这种题目就是坑呀,在做错了n次之后,查找了多方资料进行总结既可以让自己在总结中得到提高,又可以让其他人少走弯路.何乐而不为? ...
- Java HeapSort
Java HeapSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational & ...
- ligerui tab 部分记载
打开一个Tab $(".strength_box").click(function () { var id = $(this).attr("data"); va ...
- python爬视频实例
例:抓取PhotoShop视频教程 网址http://www.mxiaobei.com/?id=424 import requests import re from bs4 import Beauti ...
- JS 学习书籍电子版PDF下载
JavaScript权威指南(第6版)(中文版) 链接:https://pan.baidu.com/s/1H1v77UY-yh7oDxonRjd0GA 提取码:r3pu JavaScript DOM编 ...
- FPGA上外挂DDR2&DDR3&MIG IP的使用记录
前言 当需要大容量数据存储及处理的时候,FPGA内部自带的存储资源是远远不够的,所以问题来了,怎么使用外带的DDR3? 首要问题在于DDR3是什么?有没有协议?当然只是需要用Xilinx MIG IP ...
- ios编程时常见问题总结
(1)在UIViewController里面使用了timer,会使得controller被retain,因此在viewdisapper时应将timer置为nil,否则controller的deallo ...
- 免安装方式的Python之VSCode环境配置
概述 本文旨在介绍免安装方式,在VSCode中搭建Python(3.73)的配置环境.至于Python是什么.它能做些什么,诸如此类的介绍均不在此文中介绍,相信能看此文的人,多多少少都会有些了解. V ...
- Java基础加强-泛型
/*泛型*/ (泛型是给编译器看的) 泛型是提供给 /*javac编译器使用的*/,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入,编译器编译带类型带类型说明的集合时,会去掉 "类 ...