Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //misaka and elaina will carry me to red
- #pragma GCC optimize("O3,unroll-loops")
- #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
- #include <iostream>
- #include <cmath>
- #include <utility>
- #include <cassert>
- #include <algorithm>
- #include <vector>
- #include <array>
- #include <cstdio>
- #include <cstring>
- #include <functional>
- #include <numeric>
- #include <set>
- #include <queue>
- #include <map>
- #include <chrono>
- #include <random>
- #define sz(x) ((int)(x.size()))
- #define all(x) x.begin(), x.end()
- #define pb push_back
- #define eb emplace_back
- #define kill(x, s) {if(x){ cout << s << "\n"; return ; }}
- #ifndef LOCAL
- #define cerr while(0) cerr
- #endif
- using ll = long long;
- using lb = long double;
- const lb eps = 1e-9;
- const ll mod = 1e9 + 7, ll_max = 1e18;
- //const ll mod = (1 << (23)) * 119 +1, ll_max = 1e18;
- const int MX = 3e5 +10, int_max = 0x3f3f3f3f;
- struct {
- template<class T>
- operator T() {
- T x; std::cin >> x; return x;
- }
- } in;
- using namespace std;
- int n;
- vector<int> adj[MX];
- int super[20][MX];
- int par[MX], dep[MX];
- int tin[MX], tout[MX], tim = 1;
- ll BIT[2][MX];
- ll old[MX];
- ll S(int k, int op){
- return BIT[op][k] + (k == 0 ? 0 : S(k - (k&-k), op));
- }
- void U(int k, ll v, int op){
- for(k++; k<=n+1; k+=k&-k) BIT[op][k] += v;
- }
- void dfs(int u, int p){
- tin[u] = tim++;
- par[u] = p;
- dep[u] = dep[p]+1;
- for(int v : adj[u]){
- if(v != p) dfs(v, u);
- }
- tout[u] = tim;
- }
- void precomp(){
- for(int i = 1; i<=n; i++){
- super[0][i] = par[i];
- }
- for(int j = 1; (1 << j) <=n; j++){
- for(int i = 1; i<=n; i++){
- super[j][i] = super[j-1][super[j-1][i]];
- }
- }
- }
- int k_up(int u, int k){
- for(int i = 19; ~i; i--){
- if(k&(1 << i)){
- u = super[i][u];
- }
- }
- return u;
- }
- int lca(int a, int b){
- if(dep[a] > dep[b]) swap(a, b);
- b = k_up(b, dep[b] - dep[a]);
- if(a == b) return a;
- for(int i = 19; ~i; i--){
- if(super[i][a]!=super[i][b]){
- a = super[i][a];
- b = super[i][b];
- }
- }
- assert(b != a && par[a] && par[a] == par[b]);
- return par[a];
- }
- ll ans[MX];
- ll vval[MX];
- ll sum[MX], cnt[MX];
- void prop(int u){
- ll tmp = S(tout[u], 0) - S(tin[u], 0);
- ans[u] += tmp;
- cerr << "prop\n";
- for(int v : adj[u]){
- if(v == par[u]) continue;
- ll tmp2 = (S(tout[v], 1) - S(tin[v], 1));
- vval[v] = tmp - tmp2;
- cerr << v << " " << u << " " << tmp << " " << tmp2 << "\n";
- }
- }
- void go(int u, int p){
- for(int v : adj[u]){
- if(v == p) continue ;
- vval[v] += vval[u];
- go(v, u);
- }
- }
- void go1(int u, int p){
- sum[u] = cnt[u] + sum[p];
- for(int v : adj[u]){
- if(v == p) continue;
- go1(v, u);
- }
- }
- void solve(){
- n = in;
- for(int i = 1; i<n; i++){
- int a =in, b = in;
- adj[a].pb(b);
- adj[b].pb(a);
- }
- dfs(1, 0);
- precomp();
- vector<array<int, 4>> events;
- for(int i = 1; i<=n; i++){
- events.pb({i, 1, 0, 0});
- }
- ll tot = 0;
- for(int i = 1; i*i<=n; i++){
- for(int j = i; j*i<=n; j++){
- events.pb({j*i, 2, i, j});
- if(lca(i, j) > i*j){
- cnt[lca(i, j)]++;
- tot++;
- }
- }
- }
- go1(1, 0);
- for(int i = 1; i<=n; i++){
- ans[i] = tot - sum[i];
- cerr << ans[i] << "\n";
- }
- sort(all(events));
- for(auto [v, op, x, y] : events){
- cerr << v << " " << op << " " << x << " " << y << "\n";
- if(op == 1){
- prop(v);
- }else{
- int l = lca(x, y);
- cerr << x << " " << y << " " << l << "\n";
- U(tin[x], 1, 0);
- U(tin[x], 1, 1);
- U(tin[y], 1, 0);
- U(tin[y], 1, 1);
- U(tin[l], -2, 1);
- U(tin[l], -1, 0);
- if(par[l]){
- U(tin[par[l]], -1, 0);
- }
- }
- }
- go(1, 0);
- for(int i = 1; i<=n; i++){
- ans[i] += vval[i];
- cout << 1ll*n*(n+1)/2ll - ans[i] << "\n";
- }
- }
- signed main(){
- cin.tie(0) -> sync_with_stdio(0);
- int T = 1;
- //cin >> T;
- for(int i = 1; i<=T; i++){
- //cout << "Case #" << i << ": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment