[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的颜色集合,只能从这个集合中选一种颜色给这个点染色 求一个染色方案使得没有两 ...
随机推荐
- Composer安装yii2-imagine 压缩,剪切,旋转,水印
安装:composer require --prefer-dist yiisoft/yii2-imagine 查看是否安装成功, 安装了两个目录分别是 vendor/imagine vendor/yi ...
- dg环境连接ORA-00604,ORA-16000: database open for read-only access
报错信息 根据客户提供的报错信息, ORA-: error occurred at recursive SQL level ORA-: database open for read-only acce ...
- 怎样修改原型对象prototype
修改原型对象的方法分为两种情况, 一种是对原型对象的属性方法做增删改, 一种改变原型对象的指向. 第一种: 对原型对象的属性/方法做增删改 function Person(name){ this.na ...
- (一)Struts2 基础
一.Struts简介 1.1 历史 虽然Struts 2号称是一个全新的框架,但这仅仅是相对Struts 1而言.Struts 2与Struts 1相比,确实有很多革命性的改进,但它并不是新发布的新框 ...
- Linux 安装Mysql(图文教程)
原文:Linux 安装Mysql(图文教程) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...
- 在CentOS部署AspNetCore网站
前段时间某云服务器大促,就买了一台打算折腾一下,买了几个月,却啥也没做,就改了个初始密码.最近快到双十一了,另一家厂商相同配置的服务器价格又便宜了一大截,看来又得剁手了.从今年开始,搜索一下云服务器, ...
- RMQ((Range Minimum/Maximum Query))ST算法
给定一个数组,求出给定区间[l,r]中元素的最大值或最小值或者最值的索引. 一看到这个题目,简单,看我暴力出奇迹.暴力当然是可行的.但是时间复杂度很高(O(n^2)).线段树,树状数组也可以解决这个问 ...
- pymsql及事务
MySQL知识点补充 1.去重 distinct select distinct name,age from t1; # 针对查找出来的结果整行(记录)进行去重,也就是相同行只保存一个 注意点:dis ...
- 自定义centos
目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...
- html 输入框ios苹果手机显示九宫格数字键盘
只需要在input标签加上type=‘tel’ 即可