大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可.

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <math.h>
  5. #include <set>
  6. #include <map>
  7. #include <queue>
  8. #include <string>
  9. #include <string.h>
  10. #include <bitset>
  11. #define REP(i,a,n) for(int i=a;i<=n;++i)
  12. #define PER(i,a,n) for(int i=n;i>=a;--i)
  13. #define hr putchar(10)
  14. #define pb push_back
  15. #define lc (o<<1)
  16. #define rc (lc|1)
  17. #define mid ((l+r)>>1)
  18. #define ls lc,l,mid
  19. #define rs rc,mid+1,r
  20. #define x first
  21. #define y second
  22. #define io std::ios::sync_with_stdio(false)
  23. #define endl '\n'
  24. #define DB(a) ({REP(i,1,n) cout<<a[i]<<' ';hr;})
  25. using namespace std;
  26. typedef long long ll;
  27. typedef pair<int,int> pii;
  28. const int P = 1e9+7, INF = 0x3f3f3f3f;
  29. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  30. ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
  31. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  32. //head
  33.  
  34. const int N = 1e5+10;
  35. int n, m, a[N], b[N];
  36. vector<int> g[N];
  37. int fa[N], son[N], L[N], R[N], no[N];
  38. int top[N], dep[N], sz[N];
  39. int sqn, cnt, ans[N], blo[N], ff[N], vis[N];
  40. struct _ {
  41. int l,r,lca,id;
  42. bool operator < (const _ & rhs) const {
  43. return blo[l]^blo[rhs.l]?l<rhs.l:blo[l]&1?r<rhs.r:r>rhs.r;
  44. }
  45. } q[N];
  46. void dfs(int x, int d, int f) {
  47. fa[x]=f,sz[x]=1,dep[x]=d,L[x]=++*L,no[*L]=x;
  48. for (int y:g[x]) if (y!=f) {
  49. dfs(y,d+1,x), sz[x]+=sz[y];
  50. if (sz[y]>sz[son[x]]) son[x]=y;
  51. }
  52. R[x]=++*L,no[*L]=x;
  53. }
  54. void dfs(int x, int tf) {
  55. top[x]=tf;
  56. if (son[x]) dfs(son[x],tf);
  57. for (int y:g[x]) if (!top[y]) dfs(y,y);
  58. }
  59. int lca(int x, int y) {
  60. while (top[x]!=top[y]) {
  61. if (dep[top[x]]<dep[top[y]]) swap(x,y);
  62. x = fa[top[x]];
  63. }
  64. return dep[x]<dep[y]?x:y;
  65. }
  66. void add(int x) {
  67. if (vis[x]) {if (--ff[a[x]]==0) --cnt;}
  68. else {if (++ff[a[x]]==1) ++cnt;}
  69. vis[x] ^= 1;
  70. }
  71.  
  72. int main() {
  73. scanf("%d%d", &n, &m), sqn=sqrt(n);
  74. REP(i,1,n) scanf("%d",a+i),b[i]=a[i];
  75. sort(b+1,b+1+n),*b=unique(b+1,b+1+n)-b-1;
  76. REP(i,1,n) a[i]=lower_bound(b+1,b+1+*b,a[i])-b;
  77. REP(i,1,2*n) blo[i]=i/sqn;
  78. REP(i,2,n) {
  79. int u, v;
  80. scanf("%d%d", &u, &v);
  81. g[u].pb(v),g[v].pb(u);
  82. }
  83. dfs(1,1,0),dfs(1,1);
  84. REP(i,1,m) {
  85. int x, y;
  86. scanf("%d%d", &x, &y);
  87. if (L[x]>L[y]) swap(x,y);
  88. int _lca = lca(x,y);
  89. if (_lca==x) q[i].l=L[x];
  90. else q[i].l=R[x],q[i].lca=_lca;
  91. q[i].id=i, q[i].r=L[y];
  92. }
  93. sort(q+1,q+1+m);
  94. int ql=1,qr=0;
  95. REP(i,1,m) {
  96. while (ql<q[i].l) add(no[ql++]);
  97. while (qr>q[i].r) add(no[qr--]);
  98. while (ql>q[i].l) add(no[--ql]);
  99. while (qr<q[i].r) add(no[++qr]);
  100. if (q[i].lca) add(q[i].lca);
  101. ans[q[i].id] = cnt;
  102. if (q[i].lca) add(q[i].lca);
  103. }
  104. REP(i,1,m) printf("%d\n", ans[i]);
  105. }

SP10707 COT2 - Count on a tree II (树上莫队)的更多相关文章

  1. SP10707 COT2 - Count on a tree II [树上莫队学习笔记]

    树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...

  2. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  3. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  4. [SPOJ]Count on a tree II(树上莫队)

    树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...

  5. SP10707 COT2 - Count on a tree II 莫队

    链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...

  6. [SP10707]COT2 - Count on a tree II

    题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序 ...

  7. SP10707 COT2 - Count on a tree II 莫队上树

    题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 ...

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

  9. COT2 - Count on a tree II(树上莫队)

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

随机推荐

  1. Vue内置的Component标签用于动态切换组件

    html <div id="app"> <component :is="cut"></component> <butt ...

  2. Unity3D学习笔记(三十三):矩阵

    矩阵 矩阵就是一行和列组织起来的矩形数字块. 矩阵可以理解为是向量的数组.   矩阵的维度和记法 矩阵的维度是包含多少行多少列!例如1行2列的矩阵 记法:矩阵m中,对于第1行第2列的元素,我们记为m1 ...

  3. Hadoop技术内幕1——源代码环境准备

    Hadoop核心 1.HDFS:高容错性.高伸缩性……,允许用户将Hadoop部署在廉价的硬件上,构建分布式系统 2.MapReduce:分布式计算框架,允许用户在不了解分布式系统底层细节的情况下,开 ...

  4. Latex 算法过长 分页显示方法

    参考: Algorithm tag and page break Latex 算法过长 分页显示方法 1.引用algorithm包: 2.在\begin{document}前加上以下Latex代码: ...

  5. 1、iptables-netfilter基础

    .iptables: 包过滤型防火墙功能.四表五链 .iptables规则.规则管理工具.iptables命令 .iptables链管理.规则管理.查看等 .iptables匹配条件.目标.显式扩展. ...

  6. 手把手教你学习R语言

    本文为带大家了解R语言以及分段式的步骤教程! 人们学习R语言时普遍存在缺乏系统学习方法的问题.学习者不知道从哪开始,如何进行,选择什么学习资源.虽然网络上有许多不错的免费学习资源,然而它们多过了头,反 ...

  7. 【Selenium2】【环境搭建】

    Windows7  64位 Mozilla Firefox 36.0.4 + Firebug 2.0.19 + FirePath 0.9.7.1.1-signed.1-signed 火狐历史版本:ht ...

  8. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Easyui使用心得(1)--DateGrid表格

    最近一直在用easyui这个控件,有一点心得,在这里和大家分享一下,也是对自己工作的一个小小的总结,希望可以形成一个完整的Easyui的笔记体系,可以方便更多的人 因为自己也是在摸索中前进,难免有遗漏 ...

  10. scikit_learn逻辑回归类库

    来自:刘建平 1.概述 在scikit-learn中,与逻辑回归有关的主要有3个类.LogisticRegression, LogisticRegressionCV 和 logistic_regres ...