Submission #1363303

#TimeUsernameProblemLanguageResultExecution timeMemory
1363303modwweToy (CEOI24_toy)C++20
0 / 100
2 ms580 KiB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define ull unsigned long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "top1tst"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(k) (1ll<<k)
#define checktime   cerr <<  (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
#define getchar_unlocked getchar
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
using i128 = __int128;

int rand(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rd);
}

void phongbeo();
const int inf = 1e9;
const int mod2 = 1e9 + 7;
const double cx = 0.98;
const double lim = 0.9999999;
ll  n, m, s1, s2, s4, s3, sf, k, s5, s6, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2,
                                                               r2, root;
ll  i, s10, s12, k1, k2, k3, s11, w, l, r, dem5, dem6, dem7, dem9, q;
ll el = 19;
main() {
    if (fopen(task2".inp", "r")) {
        fin(task2);
        fou(task2);
    }

    if (fopen(task".inp", "r")) {
        fin(task);
        fou(task);
    }

    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    ll t = 1;

    //cin>>t;
    while (t--)
        phongbeo();
}
stack<int> s;
struct ib {
    int a, b;
};
struct id {
    int a, b, c, d;
};
bool cmc(id a, id b) {
    return a.a > b.a;
}
vector<id> vc;
vector<ib> v[200001];
ll dp[200001];
vector<int> vcl[200001];
int st[18][200001], sz[200001], in[200001], ou[200001], cnt[200001];
ll ans[200001];
int cost[200001];
bool vis[200001];
vector<int> ke[200001];
void gg(int x, int y) {
    sz[x] = 1;
    s.push(x);
    cost[x] = 0;

    for (auto f : ke[x])
        if (f ^ y && !vis[f]) {
            gg(f, x);
            sz[x] += sz[f];
        }
}
int center(int x) {
    gg(x, 0);
    int best = x;

    while (!s.empty()) {
        x = s.top();
        s.pop();

        if (sz[x] > n / 2 && sz[x] < sz[best])
            best = x;
    }

    return best;
}
void dfs(int x, int y) {
    in[x] = ++dem3;

    if (y == 0)
        dp[x] = 0;

    sz[x] = 1;
    st[0][x] = y;

    for (int i = 1; i < 18; i++)
        st[i][x] = st[i - 1][st[i - 1][x]];

    for (auto [f, cc] : v[x])
        if (f ^ y && !vis[f]) {
            dp[f] = dp[x] + cc;
            dfs(f, x);
            sz[x] += sz[f];
        }

    ou[x] = dem3;
}
struct fenwicktree {
    int bit[200001];
    void upd(int x, int y) {
        for (x; x <= dem3; x += x & -x)
            bit[x] += y;
    }
    int get(int x) {
        int s = 0;

        for (x; x; x -= x & -x)
            s += bit[x];

        return s;
    }
} fen;
void build(int x, int y, int z) {
    for (auto [f, cc] : v[x])
        if (f ^ y && !vis[f]) {
            if (y == 0)
                dem2++, z -= sz[f];

            build(f, x, z);

            if (y == 0)
                z += sz[f];
        }

    cnt[x]++;

    if (dp[x] <= k) {
        if (y != 0)
            vc.pb({k - dp[x], root, cnt[x], dem2});
    } else {
        int f = x;

        for (int i = 16; i >= 0; --i)
            if (st[i][f] != 0 && dp[x] - dp[st[i][f]] <= k)
                f = st[i][f];

        ans[f] += z * cnt[x];
        cnt[f] += cnt[x];
    }
}
void calc(int x, int y) {
    for (auto [f, cc] : v[x])
        if (f ^ y && !vis[f]) {
            if (x == root) {
                dem2++;

                for (auto cf : vcl[dem2])
                    fen.upd(in[cf], -cost[cf]),
                            fen.upd(ou[cf] + 1, cost[cf]);

            }

            if (x <= n)
                cost[x] = 0;

            if (x <= n && dp[f] > k) {
                int b1 = x;
                int b2 = f;

                for (int i = 16; i >= 0; --i)
                    if (st[i][b1] != 0 && dp[x] - dp[st[i][b1]] <= k)
                        b1 = st[i][b1];

                b1 = st[0][b1];

                if (dp[x] <= k)
                    b1 = 0;

                for (int i = 16; i >= 0; --i)
                    if (st[i][b2] != 0 && dp[f] - dp[st[i][b2]] <= k)
                        b2 = st[i][b2];

                b2 = st[0][b2];
                cost[x] = fen.get(in[b2]) - fen.get(in[b1]);
                ans[x] += 1ll * cost[x] * sz[f];
            }

            if (x == root)
                cost[x]++;

            fen.upd(in[x], cost[x]);
            fen.upd(ou[x] + 1, -cost[x]);
            calc(f, x);
            fen.upd(in[x], -cost[x]);
            fen.upd(ou[x] + 1, cost[x]);

            if (x == root) {
                for (auto cf : vcl[dem2])
                    fen.upd(in[cf], cost[cf]),
                            fen.upd(ou[cf] + 1, -cost[cf]);
            }
        }

    cnt[x] = 0;
    cost[x] = 0;
}
void dnc(int x) {
    x = center(x);
    root = x;
    dp[x] = 0;
    dem2 = 0;
    dem3 = 0;
    dfs(x, 0);
    build(x, 0, sz[x]);
    sort(vc.begin(), vc.end(), cmc);
    dem = n;
    int last = x;
    s2 = k;

    for (auto t : vc) {
        dem++;
        v[dem].pb({last, s2 - t.a});
        cost[dem] = t.c;
        vcl[t.d].pb(dem);
        s2 = t.a;
        last = dem;
    }

    cost[x] = 1;
    dem3 = 0;
    dfs(last, 0);
    dem2 = 0;
    calc(last, 0);

    for (int i = n + 1; i <= dem; i++)
        v[i].clear(), cost[i] = 0;

    s4 += dem3;

    for (int j = 1; j <= dem2; j++)
        vcl[j].clear();

    for (int i = 1; i <= dem3; i++)
        fen.bit[i] = 0;

    vc.clear();
    vis[x] = 1;

    for (auto f : ke[x])
        if (!vis[f])
            dnc(f);
}
void phongbeo() {
    cin >> n >> k;

    for (int i = 1; i < n; i++)
        cin >> l >> r >> s2, l++, r++, ke[l].pb(r), ke[r].pb(l), v[l].pb({r, s2}), v[r].pb({l, s2});
    dnc(1);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << "\n";
}

Compilation message (stderr)

Main.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   36 | main() {
      | ^~~~
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:152:22: warning: narrowing conversion of '(k - dp[x])' from 'long long int' to 'int' [-Wnarrowing]
  152 |             vc.pb({k - dp[x], root, cnt[x], dem2});
      |                    ~~^~~~~~~
Main.cpp:152:31: warning: narrowing conversion of 'root' from 'long long int' to 'int' [-Wnarrowing]
  152 |             vc.pb({k - dp[x], root, cnt[x], dem2});
      |                               ^~~~
Main.cpp:152:45: warning: narrowing conversion of 'dem2' from 'long long int' to 'int' [-Wnarrowing]
  152 |             vc.pb({k - dp[x], root, cnt[x], dem2});
      |                                             ^~~~
Main.cpp: In function 'void dnc(int)':
Main.cpp:235:29: warning: narrowing conversion of '(s2 - ((long long int)t.id::a))' from 'long long int' to 'int' [-Wnarrowing]
  235 |         v[dem].pb({last, s2 - t.a});
      |                          ~~~^~~~~
Main.cpp: In function 'void phongbeo()':
Main.cpp:270:75: warning: narrowing conversion of 'r' from 'long long int' to 'int' [-Wnarrowing]
  270 |         cin >> l >> r >> s2, l++, r++, ke[l].pb(r), ke[r].pb(l), v[l].pb({r, s2}), v[r].pb({l, s2});
      |                                                                           ^
Main.cpp:270:78: warning: narrowing conversion of 's2' from 'long long int' to 'int' [-Wnarrowing]
  270 |         cin >> l >> r >> s2, l++, r++, ke[l].pb(r), ke[r].pb(l), v[l].pb({r, s2}), v[r].pb({l, s2});
      |                                                                              ^~
Main.cpp:270:93: warning: narrowing conversion of 'l' from 'long long int' to 'int' [-Wnarrowing]
  270 |         cin >> l >> r >> s2, l++, r++, ke[l].pb(r), ke[r].pb(l), v[l].pb({r, s2}), v[r].pb({l, s2});
      |                                                                                             ^
Main.cpp:270:96: warning: narrowing conversion of 's2' from 'long long int' to 'int' [-Wnarrowing]
  270 |         cin >> l >> r >> s2, l++, r++, ke[l].pb(r), ke[r].pb(l), v[l].pb({r, s2}), v[r].pb({l, s2});
      |                                                                                                ^~
Main.cpp: In function 'int main()':
Main.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:38:9: note: in expansion of macro 'fin'
   38 |         fin(task2);
      |         ^~~
Main.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".ans","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:39:9: note: in expansion of macro 'fou'
   39 |         fou(task2);
      |         ^~~
Main.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:43:9: note: in expansion of macro 'fin'
   43 |         fin(task);
      |         ^~~
Main.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".ans","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:44:9: note: in expansion of macro 'fou'
   44 |         fou(task);
      |         ^~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...