SPOJ10707 COT2 Count on a tree II


Solution

我会强制在线版本! Solution戳这里

代码实现

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<set>
  8. #include<map>
  9. #include<iostream>
  10. using namespace std;
  11. #define ll long long
  12. #define re register
  13. #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
  14. inline int gi()
  15. {
  16. int f=1,sum=0;char ch=getchar();
  17. while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
  18. while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
  19. return f*sum;
  20. }
  21. const int N=60010;
  22. int Bl[N],B,P[N],ans[310][N],a[N],b[N],bl[N],num,p[N][310],Anum,rt[310],F[N];
  23. struct array
  24. {
  25. int num[210];
  26. int operator[](int x){return p[num[Bl[x]]][P[x]];};
  27. void insert(const array &pre,int x,int dep)
  28. {
  29. int block=Bl[x],t=P[x];
  30. memcpy(num,pre.num,sizeof(num));
  31. memcpy(p[++Anum],p[num[block]],sizeof(p[0]));
  32. p[Anum][t]=dep;num[block]=Anum;
  33. }
  34. }s[N];
  35. int to[N<<1],nxt[N<<1],front[N],cnt,dep[N],f[N][22],st[N],sta,kind;
  36. inline void Add(int u,int v)
  37. {
  38. to[++cnt]=v;nxt[cnt]=front[u];front[u]=cnt;
  39. }
  40. inline int dfs(int u,int fa)
  41. {
  42. dep[u]=dep[fa]+1;
  43. f[u][0]=fa;
  44. s[u].insert(s[fa],a[u],dep[u]);
  45. st[++sta]=u;int mx=dep[u],now=sta;
  46. for(re int i=front[u];i;i=nxt[i])
  47. {
  48. int v=to[i];
  49. if(v==fa)continue;
  50. mx=max(mx,dfs(v,u));
  51. }
  52. if(mx-dep[u]>=B || now==1)
  53. {
  54. rt[++num]=u;
  55. for(re int i=now;i<=sta;i++)bl[st[i]]=num;
  56. sta=now-1;return dep[u]-1;
  57. }
  58. return mx;
  59. }
  60. int lca(int u,int v)
  61. {
  62. if(dep[u]<dep[v])swap(u,v);
  63. for(re int i=20;~i;i--)
  64. if(dep[u]-(1<<i)>=dep[v])u=f[u][i];
  65. if(u==v)return u;
  66. for(re int i=20;~i;i--)
  67. if(f[u][i]!=f[v][i])
  68. u=f[u][i],v=f[v][i];
  69. return f[u][0];
  70. }
  71. inline void getans(int u,int fa,int BL)
  72. {
  73. if(++F[a[u]]==1)kind++;
  74. ans[BL][u]=kind;
  75. for(re int i=front[u];i;i=nxt[i])
  76. {
  77. int v=to[i];
  78. if(v==fa)continue;
  79. getans(v,u,BL);
  80. }
  81. if(--F[a[u]]==0)kind--;
  82. }
  83. int solve_same(int x,int y)
  84. {
  85. sta=0;
  86. for(kind=0;x!=y;x=f[x][0])
  87. {
  88. if(dep[x]<dep[y])swap(x,y);
  89. if(!F[a[x]]++)++kind,st[++sta]=a[x];
  90. }
  91. int QAQ=kind+(!F[a[x]]);
  92. for(;sta;sta--)F[st[sta]]=0;
  93. return QAQ;
  94. }
  95. int solve_diff(int x,int y)
  96. {
  97. if(dep[rt[bl[x]]]<dep[rt[bl[y]]])swap(x,y);
  98. int sum=ans[bl[x]][y];
  99. int z=rt[bl[x]],d=dep[lca(x,y)];
  100. sta=0;
  101. for(;x!=z;x=f[x][0])
  102. {
  103. if(!F[a[x]] && s[z][a[x]]<d && s[y][a[x]]<d)
  104. F[st[++sta]=a[x]]=1,sum++;
  105. }
  106. for(;sta;sta--)F[st[sta]]=0;
  107. return sum;
  108. }
  109. int n,m;
  110. void print(int x)
  111. {
  112. if(x>=10)print(x/10);
  113. putchar(x%10+'0');
  114. }
  115. int main()
  116. {
  117. n=gi();m=gi();B=sqrt(n);
  118. for(int i=1;i<=n;i++)Bl[i]=(i-1)/B+1,P[i]=i%B;
  119. for(re int i=1;i<=n;i++)a[i]=b[i]=gi();
  120. sort(b+1,b+n+1);int N=unique(b+1,b+n+1)-b-1;
  121. for(re int i=1;i<=n;i++)
  122. a[i]=lower_bound(b+1,b+N+1,a[i])-b;
  123. for(re int i=1;i<n;i++)
  124. {
  125. int u=gi(),v=gi();
  126. Add(u,v);Add(v,u);
  127. }
  128. dfs(1,1);
  129. for(re int i=1;i<=num;i++)getans(rt[i],rt[i],i);
  130. for(re int j=1;j<=20;j++)
  131. for(re int i=1;i<=n;i++)
  132. f[i][j]=f[f[i][j-1]][j-1];
  133. int lastans=0;
  134. while(m--)
  135. {
  136. int u=gi(),v=gi();
  137. if(bl[u]==bl[v])lastans=solve_same(u,v);
  138. else lastans=solve_diff(u,v);
  139. print(lastans);putchar('\n');
  140. }
  141. return 0;
  142. }

【SPOJ10707】 COT2 Count on a tree II的更多相关文章

  1. 【SPOJ10707】COT2 - Count on a tree II

    题目大意:给定一棵 N 个节点的无根树,每个节点有一个颜色.现有 M 个询问,每次询问一条树链上的不同颜色数. 题解:学会了树上莫队. 树上莫队是将节点按照欧拉序进行排序,将树上问题转化成序列上的问题 ...

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

    Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...

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

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

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

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

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

  7. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

  8. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

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

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

随机推荐

  1. .NET获取城市信息(将三字代码转换成城市名)

    整理代码,发现有一个从两张表里读取城市列表,然后linq和lambda表达式来获取城市名的函数,代码如下: public static string GetCityHotelText(string c ...

  2. android Run模式也会出现"Waiting for debugger"的解决方法

    android Run模式也会出现"Waiting for debugger"的解决方法 出现“waiting for debugger”窗口是在debug模式下运行出现的.但是, ...

  3. 使用django发送邮件(smtp)

    首先在 seeting 最下面+上 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_USE_TLS = False ...

  4. Time.fixedDeltaTime和Time.DeltaTime

    在Update中使用 Time.deltaTime,获取到的是这一帧的时间,如果游戏卡,帧率低,那这个值就大.如果游戏流畅,帧率高,这个值就小,Time.deltaTime = 1.0f / 帧率 在 ...

  5. Java 读取HDFS文件系统

    最近有个需求,计算用户画像. 系统大概有800W的用户量,算每个用户的一些数据. 数据量比较大,算用hive还是毫无压力的,但是写的oracle,在给出数据给前端,就比较难受了. 然后换了种解决方法: ...

  6. idea在哪执行maven clean?

  7. c语言const和c++const

    1.常量 常量是指值不能被改变的量,又叫做字面值 1.1常量分类 1)字符常量:'a', 'A', '*'. 2)字符串常量:"helloworld","ilovechi ...

  8. mysql学习之路_字段类型与属性

    回顾 数据库基本知识:关系型数据库与非关系型数据库 关系型数据库:安全(磁盘) 非关系型数据库:高效(内存) 关系型数据库:建立在关系模型上的数据库, 数据结构:二维表(浪费空间) 数据库操作指令:s ...

  9. RecyclerView怎么能没有ItemClickListener?加一个!

    RecyclerView可以用来代替ListView来展现大量的数据.Google在RecyclerView中提升了性能,和更多好用的API. 简单介绍RecyclerView 使用RecyclerV ...

  10. 一个封存Id与状态对应键值的神器,BigInteger的setBit和testBit用法实例

    1,首先描述一下应用场景 比如,我们要对菜单做权限,控制不同角色菜单显示与不显示,角色为经理时,我们需要菜单id为 4,7,13,24的菜单显示,别的菜单不显示. 就是说,这时候我们要把4,7,13, ...