题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少,

题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn)

  1. //#pragma comment(linker, "/stack:200000000")
  2. //#pragma GCC optimize("Ofast,no-stack-protector")
  3. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4. //#pragma GCC optimize("unroll-loops")
  5. #include<cstdio>
  6. #include<algorithm>
  7. #include<cstring>
  8. #define fi first
  9. #define se second
  10. #define mp make_pair
  11. #define pb push_back
  12. #define pi acos(-1.0)
  13. #define ll long long
  14. //#define vi vector<int>
  15. #define mod 998244353
  16. #define ld long double
  17. #define C 0.5772156649
  18. //#define ls l,m,rt<<1
  19. //#define rs m+1,r,rt<<1|1
  20. #define pil pair<int,ll>
  21. #define pli pair<ll,int>
  22. #define pii pair<int,int>
  23. #define cd complex<double>
  24. #define ull unsigned long long
  25. #define base 1000000000000000000
  26. #define fio ios::sync_with_stdio(false);cin.tie(0)
  27. using namespace std;
  28. const double eps=1e-6;
  29. const int N=1000000+10,maxn=50000+10,inf=0x3f3f3f3f;
  30. vector<int> v[N];
  31. int deep[N];
  32. void dfs(int u,int f,int dep)
  33. {
  34. deep[u]=dep;
  35. for(int i=0;i<v[u].size();i++)
  36. {
  37. int x=v[u][i];
  38. if(x!=f)dfs(x,u,dep+1);
  39. }
  40. }
  41. int root[N],ma[N*22],ind[N*22];
  42. int ls[N*22],rs[N*22],tot;
  43. inline void pushup(int o)
  44. {
  45. if(ma[ls[o]]>=ma[rs[o]])ma[o]=ma[ls[o]],ind[o]=ind[ls[o]];
  46. else ma[o]=ma[rs[o]],ind[o]=ind[rs[o]];
  47. }
  48. inline int Merge(int x,int y,int l,int r)
  49. {
  50. // printf("%d----------------------------%d\n",l,r);
  51. if(l==r)
  52. {
  53. if(!x||!y)
  54. {
  55. ma[x+y]=ma[x]+ma[y];
  56. // printf("%d %d %d %d %d %d\n",x,ma[x],y,ma[y],l,r);
  57. return x+y;
  58. }
  59. else
  60. {
  61. ma[x]=ma[x]+ma[y];
  62. // printf("%d %d %d %d %d %d\n",x,ma[x],y,ma[y],l,r);
  63. return x;
  64. }
  65. }
  66. if(!x||!y)return x+y;
  67. int m=(l+r)>>1;
  68. ls[x]=Merge(ls[x],ls[y],l,m);
  69. rs[x]=Merge(rs[x],rs[y],m+1,r);
  70. pushup(x);
  71. return x;
  72. }
  73. void update(int &o,int pos,int l,int r)
  74. {
  75. if(!o)o=++tot;
  76. // printf("%d %d %d %d---\n",l,r,pos,o);
  77. if(l==r){ma[o]++;ind[o]=l;return ;}
  78. int m=(l+r)>>1;
  79. if(pos<=m)update(ls[o],pos,l,m);
  80. else update(rs[o],pos,m+1,r);
  81. pushup(o);
  82. }
  83. void debug(int o,int l,int r)
  84. {
  85. // printf("%d+++%d %d %d %d\n",o,ma[o],ind[o],l,r);
  86. if(l==r)return ;
  87. int m=(l+r)>>1;
  88. if(ls[o])debug(ls[o],l,m);
  89. if(rs[o])debug(rs[o],m+1,r);
  90. }
  91. int ans[N],n;
  92. void solve(int u,int f)
  93. {
  94. for(int i=0;i<v[u].size();i++)
  95. {
  96. int x=v[u][i];
  97. if(x!=f)solve(x,u);
  98. }
  99. for(int i=0;i<v[u].size();i++)
  100. {
  101. int x=v[u][i];
  102. if(x!=f)
  103. {
  104. root[u]=Merge(root[u],root[x],1,n);
  105. // puts("****************8");
  106. // debug(root[id[u]],1,n);
  107. // puts("!!!!!");
  108. }
  109. }
  110. // debug(root[id[u]],1,n);
  111. // puts("------------");
  112. ans[u]=ind[root[u]]-deep[u];
  113. }
  114. int main()
  115. {
  116. scanf("%d",&n);
  117. for(int i=1;i<n;i++)
  118. {
  119. int a,b;
  120. scanf("%d%d",&a,&b);
  121. v[a].pb(b),v[b].pb(a);
  122. }
  123. dfs(1,-1,1);
  124. for(int i=1;i<=n;i++)
  125. update(root[i],deep[i],1,n);
  126. solve(1,-1);
  127. for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
  128. return 0;
  129. }
  130. /********************
  131. 4
  132. 1 2
  133. 1 3
  134. 1 4
  135. ********************/

Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并的更多相关文章

  1. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  2. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  3. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  4. Educational Codeforces Round 47 (Rated for Div. 2) 题解

    题目链接:http://codeforces.com/contest/1009 A. Game Shopping 题目: 题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi.你 ...

  5. Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling

    题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...

  6. Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph

    题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...

  7. Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)

    题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...

  8. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...

  9. Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping

    题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...

随机推荐

  1. Linux服务器---DansGuardian

    DansGuardian DansGuardian可以限制客户端的访问,通过这个软件,我们可以限制哪些网站不可以访问.哪些内容不能下载. 1.下载DansGuardian,提供一个网址http://w ...

  2. Linux基础命令---cpio

    cpio 从归档中复制文件,或者复制文件到归档中.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. Cpio命令有三种工作模式: 1)c ...

  3. Vue源码解析之nextTick

    Vue源码解析之nextTick 前言 nextTick是Vue的一个核心功能,在Vue内部实现中也经常用到nextTick.但是,很多新手不理解nextTick的原理,甚至不清楚nextTick的作 ...

  4. SNMP学习笔记之SNMP报文协议详解

    0x00 简介 简单网络管理协议(SNMP)是TCP/IP协议簇的一个应用层协议.在1988年被制定,并被Internet体系结构委员会(IAB)采纳作为一个短期的网络管理解决方案:由于SNMP的简单 ...

  5. velocity #parse抽象重用部分组件

    在某些时候,处于重用的目的,我们会选择将可以重用的部分内容剥离在单独的模板文件中,比如对于查询页面的表格部分,因为现在很多的条件可能是通过弹出查询框的方式来实现,而作为普通页面的时候,他们会有更多的功 ...

  6. 在terminal下的快捷键

    1.回到行首的快捷键:ctrl + esc 2.ctrl+[可以替代esc

  7. BZOJ5168: [HAOI2014]贴海报 线段树

    Description Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委 员 会为选民准备了一个张贴海报的electoral墙.张贴规则如下 ...

  8. springmvc基础知识及注解

    SpringMVC 1.概念 Spring的MVC框架是一个基于DispatcherServlet的MVC框架,主要由DispatcherServlet.处理器映射.处理器.视图解析器.视图组成.每一 ...

  9. Netty原理剖析

    1. Netty简介 Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作 ...

  10. 一个对iBatis的总结写的不错(转载)

    转载自:http://blog.csdn.net/panxueji/article/details/9852795 一. ibatis介绍 ibatis始于2002年,2010年更名为mybatis, ...