分析

树上莫队裸题。
好博客

树剖的时候不能再次dfs重儿子。(好像是废话,但我因为这个问题调了三小时)

代码

  1. #include<cstdlib>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<iostream>
  7. #include<string>
  8. #include<vector>
  9. #include<list>
  10. #include<deque>
  11. #include<stack>
  12. #include<queue>
  13. #include<map>
  14. #include<set>
  15. #include<bitset>
  16. #include<algorithm>
  17. #include<complex>
  18. #pragma GCC optimize ("O0")
  19. using namespace std;
  20. template<class T> inline T read(T&x){
  21. T data=0;
  22. int w=1;
  23. char ch=getchar();
  24. while(!isdigit(ch))
  25. {
  26. if(ch=='-')
  27. w=-1;
  28. ch=getchar();
  29. }
  30. while(isdigit(ch))
  31. data=10*data+ch-'0',ch=getchar();
  32. return x=data*w;
  33. }
  34. typedef long long ll;
  35. const int INF=0x7fffffff;
  36. const int MAXN=1e5+7;
  37. int n,q;
  38. map<int,int>M;
  39. int a[MAXN],id;
  40. int belong[MAXN],block;
  41. struct Edge
  42. {
  43. int nx,to;
  44. }E[MAXN<<2];
  45. int head[MAXN],ecnt;
  46. void addedge(int x,int y)
  47. {
  48. E[++ecnt].to=y;
  49. E[ecnt].nx=head[x],head[x]=ecnt;
  50. }
  51. int fa[MAXN],dep[MAXN],sz[MAXN],son[MAXN],top[MAXN];
  52. int st[MAXN],ed[MAXN],pot[MAXN],clk;
  53. void dfs1(int x,int f)
  54. {
  55. fa[x]=f,sz[x]=1;
  56. st[x]=++clk,pot[clk]=x;
  57. dep[x]=dep[f]+1;
  58. for(int i=head[x];i;i=E[i].nx)
  59. {
  60. int y=E[i].to;
  61. if(y==f)
  62. continue;
  63. dfs1(y,x);
  64. sz[x]+=sz[y];
  65. if(sz[y]>sz[son[x]])
  66. son[x]=y;
  67. }
  68. ed[x]=++clk,pot[clk]=x;
  69. }
  70. void dfs2(int x,int topf)
  71. {
  72. top[x]=topf;
  73. if(!son[x])
  74. return;
  75. dfs2(son[x],topf);
  76. for(int i=head[x];i;i=E[i].nx)
  77. {
  78. int y=E[i].to;
  79. if(y==fa[x]||y==son[x]) // edit 1:cannot dfs son[x] twice and wrongly
  80. continue;
  81. dfs2(y,y);
  82. }
  83. }
  84. int lca(int x,int y)
  85. {
  86. while(top[x]!=top[y])
  87. {
  88. if(dep[top[x]]<dep[top[y]])
  89. swap(x,y);
  90. x=fa[top[x]];
  91. }
  92. return dep[x]<dep[y]?x:y;
  93. }
  94. struct Quiz
  95. {
  96. int l,r,f,id;
  97. bool operator<(const Quiz&x)
  98. {
  99. if(belong[l]^belong[x.l])
  100. return l<x.l;
  101. else
  102. return r<x.r;
  103. }
  104. }Q[MAXN];
  105. int ql,qr,res;
  106. bool used[MAXN];
  107. int cnt[MAXN],ans[MAXN];
  108. void add(int x)
  109. {
  110. if(++cnt[x]==1)
  111. ++res;
  112. }
  113. void del(int x)
  114. {
  115. if(--cnt[x]==0)
  116. --res;
  117. }
  118. void change(int x)
  119. {
  120. used[x]?del(a[x]):add(a[x]);
  121. used[x]^=1;
  122. }
  123. int main()
  124. {
  125. // freopen(".in","r",stdin);
  126. // freopen(".out","w",stdout);
  127. read(n);read(q);
  128. block=sqrt(n);
  129. for(int i=1;i<=n;++i)
  130. {
  131. read(a[i]);
  132. if(!M[a[i]])
  133. M[a[i]]=++id;
  134. a[i]=M[a[i]];
  135. }
  136. for(int i=1;i<=2*n;++i) // edit 1:belong should be inited to 2n
  137. belong[i]=(i-1)/block+1;
  138. for(int i=1;i<n;++i)
  139. {
  140. int x,y;
  141. read(x);read(y);
  142. addedge(x,y);
  143. addedge(y,x);
  144. }
  145. dfs1(1,0);
  146. dfs2(1,1);
  147. for(int i=1;i<=q;++i)
  148. {
  149. int x,y;
  150. read(x);read(y);
  151. if(st[x]>st[y])
  152. swap(x,y);
  153. int f=lca(x,y);
  154. if(f==x)
  155. Q[i].l=st[x],Q[i].r=st[y];
  156. else
  157. Q[i].l=ed[x],Q[i].r=st[y],Q[i].f=f;
  158. Q[i].id=i;
  159. }
  160. sort(Q+1,Q+q+1);
  161. ql=1,qr=0,res=0;
  162. for(int i=1;i<=q;++i)
  163. {
  164. while(ql>Q[i].l)
  165. change(pot[--ql]);
  166. while(qr<Q[i].r)
  167. change(pot[++qr]);
  168. while(ql<Q[i].l)
  169. change(pot[ql++]);
  170. while(qr>Q[i].r)
  171. change(pot[qr--]);
  172. if(Q[i].f)
  173. change(Q[i].f);
  174. ans[Q[i].id]=res;
  175. if(Q[i].f)
  176. change(Q[i].f);
  177. }
  178. for(int i=1;i<=q;++i)
  179. printf("%d\n",ans[i]);
  180. // fclose(stdin);
  181. // fclose(stdout);
  182. return 0;
  183. }

SPOJCOT2 Count on a tree II的更多相关文章

  1. SPOJcot2 Count on a tree II (树上莫队)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...

  2. 【SPOJ10707】 COT2 Count on a tree II

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

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

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

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

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

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

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

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

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

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

  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. ORACLE COMMENTON 使用

    oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON  { TABLE [ schema. ]    { table | view }  | COLUMN [ s ...

  2. LeetCode--225--用队列实现栈

    问题描述: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队 ...

  3. yarn的淘宝镜像

    现在有很多人使用npm但是很多人也开始使用了更快的更方便额的yarn,可是yarn也有下载速度慢,容易被墙的担忧~又不能像npm那样直接安装淘宝镜像,所以就有了更改yarn的yarn源~ yarn c ...

  4. JavaScript In OA Framework

    原文地址:JavaScript In OA Framework (需FQ) “To be or not to be…… is the question…..!” The famous soliloqu ...

  5. JAVA计算文件的crc32校验码

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  6. Hibernate---hbm2ddl和数据库方言的配置

    Hibernate---hbm2ddl和数据库方言的配置 hibernate配置文件--hbm2ddl.auto属性值的区别: update: 最常用的取值,如果但其数据库中不存在表结构,那么自动创建 ...

  7. spring--boot @Valid的使用

    spring--boot @Valid的使用 每天一个小知识点,每天进步一点点,总结是积累. springBoot @Valid的使用,解释一下.就是给摸个bean类属性(数据库字段)加一个门槛,比如 ...

  8. 一篇关于oracle psu的文章(转)

    Oracle Database PSU/CPU Posted on 2011-07-28 16:27 dbblog 阅读(2569) 评论(0) 编辑 收藏 1. 什么是PSU/CPU?CPU: Cr ...

  9. React脚手架create-react-app+elementUI使用

    一.介绍 1.create-react-app是FaceBook官方发布了一个无需配置的.用于快速构建开发环境的脚手架工具. 2.优点 a.无需配置:官方的配置堪称完美,几乎不用你再配置任何东西,就可 ...

  10. ArrayList与List<T>笔记

    ArrayList与List<T>笔记 ArrayList是在System.Collections命名空间的一个类, 通过Add的方法添加一个项, 当进到这个类的元数据时, 可以看到这个方 ...