「SPOJ10707」Count on a tree II

传送门

树上莫队板子题。

锻炼基础,没什么好说的。

参考代码:

  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cmath>
  4. #define rg register
  5. #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
  6. using namespace std;
  7. template < class T > inline void read(T& s) {
  8. s = 0; int f = 0; char c = getchar();
  9. while ('0' > c || c > '9') f |= c == '-', c = getchar();
  10. while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
  11. s = f ? -s : s;
  12. }
  13. const int _ = 40005, __ = 1e5 + 5;
  14. int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
  15. inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }; head[u] = tot; }
  16. int n, q, a[_], X0, X[_];
  17. int fir[_], las[_], vis[_], dep[_], fa[17][_];
  18. int len, ord[_ << 1], m, pos[_ << 1];
  19. int ans, cnt[_], res[__];
  20. struct node { int l, r, lca, id; } t[__];
  21. inline bool cmp(const node& x, const node& y)
  22. { return pos[x.l] != pos[y.l] ? pos[x.l] < pos[y.l] : ((pos[x.l] & 1) ? x.r < y.r : y.r < x.r); }
  23. inline void dfs(int u, int f) {
  24. fir[u] = ++len, ord[len] = u;
  25. dep[u] = dep[f] + 1, fa[0][u] = f;
  26. for (rg int i = 1; i <= 16; ++i) fa[i][u] = fa[i - 1][fa[i - 1][u]];
  27. for (rg int i = head[u]; i; i = edge[i].nxt) if (edge[i].ver != f) dfs(edge[i].ver, u);
  28. las[u] = ++len, ord[len] = u;
  29. }
  30. inline int LCA(int x, int y) {
  31. if (dep[x] < dep[y]) swap(x, y);
  32. for (rg int i = 16; ~i; --i) if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
  33. if (x == y) return x;
  34. for (rg int i = 16; ~i; --i) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
  35. return fa[0][x];
  36. }
  37. inline void calc(int x) { vis[x] ? ans -= !--cnt[a[x]] : ans += !cnt[a[x]]++, vis[x] ^= 1; }
  38. int main() {
  39. read(n), read(q);
  40. for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i];
  41. sort(X + 1, X + n + 1);
  42. X0 = unique(X + 1, X + n + 1) - X - 1;
  43. for (rg int i = 1; i <= n; ++i) a[i] = lower_bound(X + 1, X + X0 + 1, a[i]) - X;
  44. for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
  45. dfs(1, 0);
  46. for (rg int x, y, lca, i = 1; i <= q; ++i) {
  47. read(x), read(y), lca = LCA(x, y);
  48. if (fir[x] > fir[y]) swap(x, y);
  49. if (x == lca)
  50. t[i].l = fir[x], t[i].r = fir[y], t[i].lca = 0;
  51. else
  52. t[i].l = las[x], t[i].r = fir[y], t[i].lca = lca;
  53. t[i].id = i;
  54. }
  55. m = sqrt(1.0 * len);
  56. for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / m + 1;
  57. sort(t + 1, t + q + 1, cmp);
  58. for (rg int l = 1, r = 0, i = 1; i <= q; ++i) {
  59. while (l > t[i].l) calc(ord[--l]);
  60. while (r < t[i].r) calc(ord[++r]);
  61. while (l < t[i].l) calc(ord[l++]);
  62. while (r > t[i].r) calc(ord[r--]);
  63. if (t[i].lca) calc(t[i].lca);
  64. res[t[i].id] = ans;
  65. if (t[i].lca) calc(t[i].lca);
  66. }
  67. for (rg int i = 1; i <= q; ++i) printf("%d\n", res[i]);
  68. return 0;
  69. }

「SPOJ10707」Count on a tree II的更多相关文章

  1. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

  2. SPOJ10707 COT2 - Count on a tree II 【树上莫队】

    题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...

  3. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  4. 【BZOJ2589】[SPOJ10707]Count on a tree II

    [BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...

  5. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  6. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  7. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  8. AC日记——Count on a tree II spoj

    Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...

  9. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

随机推荐

  1. 喵星之旅-狂奔的兔子-centos7安装MySQL 5.5

    安装环境:https://www.cnblogs.com/kittybunny/p/12296078.html 一.下载安装文件 下载地址 https://downloads.mysql.com/ar ...

  2. php-cp(php连接池)扩展的安装

    今天看到php有连接池的扩展,不管效果怎么样,都值得一试,这样才会有突破. 先从guthub上搜索源码:[ https://github.com/swoole/php-cp ] 通过命令clone到自 ...

  3. nginx解决WordPress 上传到服务器后页面404错误的方法

    人啊,要说你傻了吧,真是啥事都能碰到: 因为换了nginx,把新做的上传到服务器配置好后,就主页和后台能打开,其他的所有页面,全是404,果真404和502是我最讨厌的数字啊,这让我很怀疑人生啊,怀疑 ...

  4. Shiro入门学习之shi.ini实现认证及源码分析(二)

    一.Shiro.ini文件 1.文件说明 ①ini(InitializationFile)初始文件:Window系统文件扩展名 ②Shiro使用时可以连接数据库,也可以不连接数据库(可以使用shiro ...

  5. python中getpass模块

    1 import getpass 2 name = input('请输入你的名字:') 3 passwd = getpass.getpass('请输入你的密码:') 4 print(name) 5 p ...

  6. ASP.NET Core搭建多层网站架构【9.1-使用Autofac代替原生的依赖注入】

    2020/01/30, ASP.NET Core 3.1, VS2019, Autofac.Extensions.DependencyInjection 5.0.1 摘要:基于ASP.NET Core ...

  7. 【笔记4-商品模块】从0开始 独立完成企业级Java电商网站开发(服务端)

    分类管理模块 数据表结构设计 分类表 CREATE TABLE.mmall_ category' ( 'id' int(11) NOT NULL AUTO_ INCREMENT COMMENT ' 类 ...

  8. traceback说明

    一:traceback说明 该模块提供了一个标准接口来提取,格式化和打印Python程序的堆栈跟踪.它完全模仿Python解释器在打印堆栈跟踪时的行为.当您想要在程序控制下打印堆栈跟踪时,这很有用. ...

  9. HtmlUnit-API的使用就介绍

    转自:https://www.cnblogs.com/luotinghao/p/3800054.html 网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源Htm ...

  10. 如何确认 fastboot unlock 解锁成功,如何确认DM-verity 已关闭

    如何确认 fastboot unlock 解锁成功 1.fastboot 模式下按音量上键后是否提示 Unlock Pass...return to fastboot in 3s 2.重启后界面是否显 ...