willy108

smaller lca

Nov 20th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1.  
  2.  
  3. //misaka and elaina will carry me to red
  4.  
  5. #pragma GCC optimize("O3,unroll-loops")
  6. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <utility>
  11. #include <cassert>
  12. #include <algorithm>
  13. #include <vector>
  14. #include <array>
  15. #include <cstdio>
  16. #include <cstring>
  17. #include <functional>
  18. #include <numeric>
  19. #include <set>
  20. #include <queue>
  21. #include <map>
  22. #include <chrono>
  23. #include <random>
  24.  
  25. #define sz(x) ((int)(x.size()))
  26. #define all(x) x.begin(), x.end()
  27. #define pb push_back
  28. #define eb emplace_back
  29. #define kill(x, s) {if(x){ cout << s << "\n"; return ; }}
  30.  
  31. #ifndef LOCAL
  32. #define cerr while(0) cerr
  33. #endif
  34.  
  35. using ll = long long;
  36. using lb = long double;
  37.  
  38. const lb eps = 1e-9;
  39. const ll mod = 1e9 + 7, ll_max = 1e18;
  40. //const ll mod = (1 << (23)) * 119 +1, ll_max = 1e18;
  41. const int MX = 3e5 +10, int_max = 0x3f3f3f3f;
  42.  
  43. struct {
  44. template<class T>
  45. operator T() {
  46. T x; std::cin >> x; return x;
  47. }
  48. } in;
  49.  
  50. using namespace std;
  51.  
  52.  
  53. int n;
  54. vector<int> adj[MX];
  55. int super[20][MX];
  56. int par[MX], dep[MX];
  57. int tin[MX], tout[MX], tim = 1;
  58.  
  59. ll BIT[2][MX];
  60.  
  61. ll old[MX];
  62.  
  63. ll S(int k, int op){
  64. return BIT[op][k] + (k == 0 ? 0 : S(k - (k&-k), op));
  65. }
  66. void U(int k, ll v, int op){
  67. for(k++; k<=n+1; k+=k&-k) BIT[op][k] += v;
  68. }
  69.  
  70. void dfs(int u, int p){
  71. tin[u] = tim++;
  72. par[u] = p;
  73. dep[u] = dep[p]+1;
  74. for(int v : adj[u]){
  75. if(v != p) dfs(v, u);
  76. }
  77. tout[u] = tim;
  78. }
  79.  
  80. void precomp(){
  81. for(int i = 1; i<=n; i++){
  82. super[0][i] = par[i];
  83. }
  84. for(int j = 1; (1 << j) <=n; j++){
  85. for(int i = 1; i<=n; i++){
  86. super[j][i] = super[j-1][super[j-1][i]];
  87. }
  88. }
  89. }
  90.  
  91. int k_up(int u, int k){
  92. for(int i = 19; ~i; i--){
  93. if(k&(1 << i)){
  94. u = super[i][u];
  95. }
  96. }
  97. return u;
  98. }
  99.  
  100. int lca(int a, int b){
  101. if(dep[a] > dep[b]) swap(a, b);
  102. b = k_up(b, dep[b] - dep[a]);
  103. if(a == b) return a;
  104. for(int i = 19; ~i; i--){
  105. if(super[i][a]!=super[i][b]){
  106. a = super[i][a];
  107. b = super[i][b];
  108. }
  109. }
  110. assert(b != a && par[a] && par[a] == par[b]);
  111. return par[a];
  112. }
  113.  
  114. ll ans[MX];
  115. ll vval[MX];
  116. ll sum[MX], cnt[MX];
  117.  
  118. void prop(int u){
  119. ll tmp = S(tout[u], 0) - S(tin[u], 0);
  120. ans[u] += tmp;
  121. cerr << "prop\n";
  122. for(int v : adj[u]){
  123. if(v == par[u]) continue;
  124. ll tmp2 = (S(tout[v], 1) - S(tin[v], 1));
  125. vval[v] = tmp - tmp2;
  126. cerr << v << " " << u << " " << tmp << " " << tmp2 << "\n";
  127. }
  128. }
  129.  
  130. void go(int u, int p){
  131. for(int v : adj[u]){
  132. if(v == p) continue ;
  133. vval[v] += vval[u];
  134. go(v, u);
  135. }
  136. }
  137.  
  138. void go1(int u, int p){
  139. sum[u] = cnt[u] + sum[p];
  140. for(int v : adj[u]){
  141. if(v == p) continue;
  142. go1(v, u);
  143. }
  144. }
  145.  
  146. void solve(){
  147. n = in;
  148. for(int i = 1; i<n; i++){
  149. int a =in, b = in;
  150. adj[a].pb(b);
  151. adj[b].pb(a);
  152. }
  153. dfs(1, 0);
  154. precomp();
  155. vector<array<int, 4>> events;
  156. for(int i = 1; i<=n; i++){
  157. events.pb({i, 1, 0, 0});
  158. }
  159. ll tot = 0;
  160. for(int i = 1; i*i<=n; i++){
  161. for(int j = i; j*i<=n; j++){
  162. events.pb({j*i, 2, i, j});
  163. if(lca(i, j) > i*j){
  164. cnt[lca(i, j)]++;
  165. tot++;
  166. }
  167. }
  168. }
  169. go1(1, 0);
  170. for(int i = 1; i<=n; i++){
  171. ans[i] = tot - sum[i];
  172. cerr << ans[i] << "\n";
  173. }
  174.  
  175. sort(all(events));
  176. for(auto [v, op, x, y] : events){
  177. cerr << v << " " << op << " " << x << " " << y << "\n";
  178. if(op == 1){
  179. prop(v);
  180. }else{
  181. int l = lca(x, y);
  182. cerr << x << " " << y << " " << l << "\n";
  183. U(tin[x], 1, 0);
  184. U(tin[x], 1, 1);
  185. U(tin[y], 1, 0);
  186. U(tin[y], 1, 1);
  187. U(tin[l], -2, 1);
  188. U(tin[l], -1, 0);
  189. if(par[l]){
  190. U(tin[par[l]], -1, 0);
  191. }
  192. }
  193. }
  194. go(1, 0);
  195. for(int i = 1; i<=n; i++){
  196. ans[i] += vval[i];
  197. cout << 1ll*n*(n+1)/2ll - ans[i] << "\n";
  198. }
  199.  
  200.  
  201. }
  202.  
  203. signed main(){
  204. cin.tie(0) -> sync_with_stdio(0);
  205.  
  206. int T = 1;
  207. //cin >> T;
  208. for(int i = 1; i<=T; i++){
  209. //cout << "Case #" << i << ": ";
  210. solve();
  211. }
  212. return 0;
  213. }
  214.  
  215.  
Advertisement
Add Comment
Please, Sign In to add comment