https://www.luogu.org/problemnew/show/P2486

qizha

为什么会wa

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6. using std:: cin;
  7. using std:: cout;
  8. using std:: endl;
  9. using std:: swap;
  10. using std:: string;
  11.  
  12. #define gc getchar()
  13.  
  14. const int N = 1e5 + ;
  15.  
  16. //...数据
  17. int n, m, data[N];
  18. //...图
  19. int now = , head[N];
  20. struct Node {int u, v, nxt;} G[N << ];
  21. //...树剖
  22. int tim;
  23. int fa[N], deep[N], size[N], topp[N], son[N], tree[N], bef[N];
  24. //...线段树
  25. int L[N << ], R[N << ], F[N << ], l_col[N << ], r_col[N << ], tot[N << ];
  26. //...查询操作
  27. int Lc, Rc, ans;
  28.  
  29. inline int read() {
  30. int x = , f = ; char c = gc;
  31. while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
  32. while(c >= '' && c <= '') x = x * + c - '', c = gc;
  33. return x * f;
  34. }
  35.  
  36. inline void Add(int u, int v) {G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;}
  37.  
  38. void Dfs_find_son(int u, int f_, int dep) {
  39. fa[u] = f_;
  40. deep[u] = dep;
  41. size[u] = ;
  42. for(int i = head[u]; ~ i; i = G[i].nxt) {
  43. int v = G[i].v;
  44. if(v != f_) {
  45. Dfs_find_son(v, u, dep + );
  46. size[u] += size[v];
  47. if(size[v] > size[son[u]]) son[u] = v;
  48. }
  49. }
  50. }
  51.  
  52. void Dfs_to_un(int u, int tp) {
  53. topp[u] = tp;
  54. tree[u] = ++ tim;
  55. bef[tim] = u;
  56. if(! son[u]) return ;
  57. Dfs_to_un(son[u], tp);
  58. for(int i = head[u]; ~ i; i = G[i].nxt) {
  59. int v = G[i].v;
  60. if(v != fa[u] && v != son[u]) Dfs_to_un(v, v);
  61. }
  62. }
  63.  
  64. #define lson jd << 1
  65. #define rson jd << 1 | 1
  66.  
  67. void Up(int jd) {
  68. l_col[jd] = l_col[lson]; r_col[jd] = r_col[rson];
  69. tot[jd] = tot[lson] + tot[rson];
  70. if(r_col[lson] == l_col[rson]) tot[jd] --;
  71. }
  72.  
  73. void Build_tree(int l, int r, int jd) {
  74. L[jd] = l; R[jd] = r; F[jd] = -;
  75. if(l == r) {
  76. l_col[jd] = r_col[jd] = data[bef[l]];
  77. tot[jd] = ;
  78. return ;
  79. }
  80. int mid = (l + r) >> ;
  81. Build_tree(l, mid, lson);
  82. Build_tree(mid + , r, rson);
  83. Up(jd);
  84. }
  85.  
  86. void Down_f(int jd) {
  87. F[lson] = F[rson] = F[jd];
  88. tot[lson] = tot[rson] = ;
  89. l_col[lson] = r_col[lson] = l_col[rson] = r_col[rson] = F[jd];
  90. F[jd] = -;
  91. return ;
  92. }
  93.  
  94. void Sec_A(int l, int r, int jd, int x, int y) {
  95. if(x == L[jd]) Lc = l_col[jd];
  96. if(y == R[jd]) Rc = r_col[jd];
  97. if(x <= l && r <= y) {ans += tot[jd]; return ;}
  98. if(F[jd] != -) Down_f(jd);
  99. int mid = (l + r) >> ;
  100. if(x <= mid) Sec_A(l, mid, lson, x, y);
  101. if(y > mid) Sec_A(mid + , r, rson, x, y);
  102. if(x <= mid && y > mid && r_col[lson] == l_col[rson]) ans --;
  103. }
  104.  
  105. inline int Sec_A_imp(int x, int y) {
  106. int tp1 = topp[x], tp2 = topp[y], ret = , prex = -, prey = -;
  107. while(tp1 != tp2) {
  108. if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(prex, prey), swap(x, y);
  109. ans = ;
  110. Sec_A(, n, , tree[tp1], tree[x]);
  111. ret += ans;
  112. if(Rc == prex) ret --;
  113. prex = Lc;
  114. x = fa[tp1];
  115. tp1 = topp[x];
  116. }
  117. if(deep[x] < deep[y]) swap(x, y), swap(prex, prey);
  118. ans = ;
  119. Sec_A(, n, , tree[y], tree[x]);
  120. ret += ans;
  121. if(Lc == prey) ret --;
  122. if(Rc == prex) ret --;
  123. return ret;
  124. }
  125.  
  126. void Sec_G(int l, int r, int jd, int x, int y, int how) {
  127. if(x <= l && r <= y) {
  128. F[jd] = how; tot[jd] = ; l_col[jd] = r_col[jd] = how;
  129. return ;
  130. }
  131. int mid = (l + r) >> ;
  132. if(x <= mid) Sec_G(l, mid, lson, x, y, how);
  133. if(y > mid) Sec_G(mid + , r, rson, x, y, how);
  134. Up(jd);
  135. }
  136.  
  137. void Sec_G_imp(int x, int y, int how) {
  138. int tp1 = topp[x], tp2 = topp[y];
  139. while(tp1 != tp2) {
  140. if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(x, y);
  141. Sec_G(, n, , tree[tp1], tree[x], how);
  142. x = fa[tp1];
  143. tp1 = topp[x];
  144. }
  145. if(deep[x] < deep[y]) swap(x, y);
  146. Sec_G(, n, , tree[y], tree[x], how);
  147. return ;
  148. }
  149.  
  150. #define RR freopen("gg.in", "r", stdin)
  151.  
  152. int main() {
  153. //RR;
  154. n = read(); m = read();
  155. for(int i = ; i <= n; i ++) data[i] = read();
  156. for(int i = ; i <= n; i ++) head[i] = -;
  157. for(int i = ; i < n; i ++) {
  158. int u = read(), v = read();
  159. Add(u, v); Add(v, u);
  160. }
  161. Dfs_find_son(, , );
  162. Dfs_to_un(, );
  163. Build_tree(, n, );
  164. while(m --) {
  165. string s;
  166. cin >> s;
  167. if(s[] == 'Q') {
  168. int x = read(), y = read();
  169. cout << Sec_A_imp(x, y) << endl;
  170. } else {
  171. int x = read(), y = read(), how = read();
  172. Sec_G_imp(x, y, how);
  173. }
  174. }
  175. return ;
  176. }
  177. /*
  178. 6 5
  179. 2 2 1 2 1 1
  180. 1 2
  181. 1 3
  182. 2 4
  183. 2 5
  184. 2 6
  185. Q 3 5
  186. C 2 1 1
  187. Q 3 5
  188. C 5 1 2
  189. Q 3 5
  190. */

[Luogu] 染色的更多相关文章

  1. [Luogu 2486] SDOI2011 染色

    [Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...

  2. Luogu P2486 [SDOI2011]染色(树链剖分+线段树合并)

    Luogu P2486 [SDOI2011]染色 题面 题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例: 6 5 2 2 1 2 1 1 1 ...

  3. LOJ #2527 Luogu P4491「HAOI2018」染色

    好像网上没人....和我推出....同一个式子啊..... LOJ #2527 Luogu P4491 题意 $ n$个格子中每个格子可以涂$ m$种颜色中的一种 若有$ k$种颜色恰好涂了$ s$格 ...

  4. luogu P2486 [SDOI2011]染色

    树剖做法: 就是两个dfs+一个线段树 难度的取决基本==线段树的维护难度 所以对有点线段树基础的,树剖也不难做吧 这里操作有二 一:两点间路径染色 线段树的区间赋值操作 二:查询路径段的个数 考虑线 ...

  5. Luogu P2486 染色(树链剖分+线段树)

    题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...

  6. 【Luogu】P3155叶子的染色(树形DP)

    题目链接 树形DP水题qwq. 设f[i][j]是以i为根的子树,染成j色,且满足内部需求的最少染色节点数. 设to是x的子节点,那么状态转移方程如此设计: 1.f[i][0] 这个状态表示i不染色, ...

  7. 【Luogu】P1330封锁阳光大学(bfs染色)

    题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...

  8. BZOJ 4823 Luogu P3756 老C的方块 染色+最小割

    题面太长了请各位自行品尝—>老C的方块 分析: 我们要解决掉所有使人弃疗的组合,还要保证花费最小,容易想到最小割(当然你要是想费用流的话,我们就没办法定义流量了) 我们来分析一下那些令人弃疗的组 ...

  9. luogu 4429 染色

    bjoi 2018 染色 推了个错误结论得了60分? 题目大意: 一个无重边和自环的无向图,并且对每个点分别给了一个大小为2的颜色集合,只能从这个集合中选一种颜色给这个点染色 求一个染色方案使得没有两 ...

随机推荐

  1. Composer安装yii2-imagine 压缩,剪切,旋转,水印

    安装:composer require --prefer-dist yiisoft/yii2-imagine 查看是否安装成功, 安装了两个目录分别是 vendor/imagine vendor/yi ...

  2. 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 ...

  3. 怎样修改原型对象prototype

    修改原型对象的方法分为两种情况, 一种是对原型对象的属性方法做增删改, 一种改变原型对象的指向. 第一种: 对原型对象的属性/方法做增删改 function Person(name){ this.na ...

  4. (一)Struts2 基础

    一.Struts简介 1.1 历史 虽然Struts 2号称是一个全新的框架,但这仅仅是相对Struts 1而言.Struts 2与Struts 1相比,确实有很多革命性的改进,但它并不是新发布的新框 ...

  5. Linux 安装Mysql(图文教程)

    原文:Linux 安装Mysql(图文教程) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...

  6. 在CentOS部署AspNetCore网站

    前段时间某云服务器大促,就买了一台打算折腾一下,买了几个月,却啥也没做,就改了个初始密码.最近快到双十一了,另一家厂商相同配置的服务器价格又便宜了一大截,看来又得剁手了.从今年开始,搜索一下云服务器, ...

  7. RMQ((Range Minimum/Maximum Query))ST算法

    给定一个数组,求出给定区间[l,r]中元素的最大值或最小值或者最值的索引. 一看到这个题目,简单,看我暴力出奇迹.暴力当然是可行的.但是时间复杂度很高(O(n^2)).线段树,树状数组也可以解决这个问 ...

  8. pymsql及事务

    MySQL知识点补充 1.去重 distinct select distinct name,age from t1; # 针对查找出来的结果整行(记录)进行去重,也就是相同行只保存一个 注意点:dis ...

  9. 自定义centos

    目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...

  10. html 输入框ios苹果手机显示九宫格数字键盘

    只需要在input标签加上type=‘tel’  即可