Submission #916608

# Submission time Handle Problem Language Result Execution time Memory
916608 2024-01-26T07:04:54 Z GrindMachine Unique Cities (JOI19_ho_t5) C++17
64 / 100
1016 ms 57888 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi
https://codeforces.com/blog/entry/65042?#comment-491880

*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct lazysegtree {
    /*=======================================================*/

    struct data {
        ll mn,cnt;
    };

    struct lazy {
        ll a;
    };

    data d_neutral = {inf2,1};
    lazy l_neutral = {0};

    void merge(data &curr, data &left, data &right) {
        if(left.mn < right.mn) curr = left;
        else if(left.mn > right.mn) curr = right;
        else curr = {left.mn,left.cnt+right.cnt};
    }

    void create(int x, int lx, int rx, T v) {

    }

    void modify(int x, int lx, int rx, T v) {
        lz[x].a = v;
    }

    void propagate(int x, int lx, int rx) {
        ll v = lz[x].a;
        if(!v) return;

        tr[x].mn += v;

        if(rx-lx > 1){
            lz[2*x+1].a += v;
            lz[2*x+2].a += v;
        }

        lz[x] = l_neutral;
    }

    /*=======================================================*/

    int siz = 1;
    vector<data> tr;
    vector<lazy> lz;

    lazysegtree() {

    }

    lazysegtree(int n) {
        while (siz < n) siz *= 2;
        tr.assign(2 * siz, d_neutral);
        lz.assign(2 * siz, l_neutral);
    }

    void build(vector<T> &a, int n, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                create(x, lx, rx, a[lx]);
            }

            return;
        }

        int mid = (lx + rx) / 2;

        build(a, n, 2 * x + 1, lx, mid);
        build(a, n, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void build(vector<T> &a, int n) {
        build(a, n, 0, 0, siz);
    }

    void rupd(int l, int r, T v, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return;
        if (lx >= l and rx <= r) {
            modify(x, lx, rx, v);
            propagate(x, lx, rx);
            return;
        }

        int mid = (lx + rx) / 2;

        rupd(l, r, v, 2 * x + 1, lx, mid);
        rupd(l, r, v, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void rupd(int l, int r, T v) {
        rupd(l, r + 1, v, 0, 0, siz);
    }

    data query(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return d_neutral;
        if (lx >= l and rx <= r) return tr[x];

        int mid = (lx + rx) / 2;

        data curr;
        data left = query(l, r, 2 * x + 1, lx, mid);
        data right = query(l, r, 2 * x + 2, mid, rx);

        merge(curr, left, right);
        return curr;
    }

    data query(int l, int r) {
        return query(l, r + 1, 0, 0, siz);
    }
};

vector<ll> adj[N];
vector<ll> a(N);
pll diam = {-1,-1};

void dfs1(ll u, ll p, ll d){
    pll px = {d,u};
    amax(diam,px);
    trav(v,adj[u]){
        if(v == p) conts;
        dfs1(v,u,d+1);
    }
}

ll dis[N][2];
ll deepest[N][2];

void dfs2(ll u, ll p, ll t){
    trav(v,adj[u]){
        if(v == p) conts;
        dis[v][t] = dis[u][t]+1;
        dfs2(v,u,t);
        amax(deepest[u][t],deepest[v][t]+1);
    }
}

lazysegtree<ll> st(N);
vector<ll> ans(N);
vector<ll> mx_without(N);

void dfs3(ll u, ll p, ll d, ll t){
    ll mx = deepest[u][t];
    st.rupd(max(d-mx,0ll),d-1,1);
    auto [mn,mn_cnt] = st.query(0,d);
    assert(mn == 0);

    if(dis[u][t] >= dis[u][t^1]){
        ans[u] = mn_cnt-1;
    }

    st.rupd(max(d-mx,0ll),d-1,-1);

    mx = 0;

    trav(v,adj[u]){
        if(v == p) conts;
        mx_without[v] = mx;
        amax(mx,deepest[v][t]+1);
    }

    mx = 0;
    reverse(all(adj[u]));

    trav(v,adj[u]){
        if(v == p) conts;
        amax(mx_without[v],mx);
        amax(mx,deepest[v][t]+1);
    }

    reverse(all(adj[u]));

    trav(v,adj[u]){
        if(v == p) conts;
        mx = mx_without[v];
        st.rupd(d-mx,d-1,1);
        dfs3(v,u,d+1,t);
        st.rupd(d-mx,d-1,-1);
    }
}

void solve(int test_case)
{
    ll n,m; cin >> n >> m;
    rep1(i,n-1){
        ll u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }
    rep1(i,n) cin >> a[i];

    dfs1(1,-1,0);
    ll s = diam.ss;
    diam = {-1,-1};
    dfs1(s,-1,0);
    ll t = diam.ss;

    dfs2(s,-1,0);
    dfs2(t,-1,1);

    st.rupd(0,n,-inf2);

    dfs3(s,-1,0,0);
    dfs3(t,-1,0,1);

    rep1(i,n){
        amin(ans[i],m);
        cout << ans[i] << endl;
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 24156 KB Output is correct
2 Incorrect 11 ms 24156 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 348 ms 31360 KB Output is correct
2 Correct 475 ms 43308 KB Output is correct
3 Correct 82 ms 27536 KB Output is correct
4 Correct 670 ms 36028 KB Output is correct
5 Correct 976 ms 57176 KB Output is correct
6 Correct 891 ms 46160 KB Output is correct
7 Correct 702 ms 35968 KB Output is correct
8 Correct 829 ms 38396 KB Output is correct
9 Correct 734 ms 37552 KB Output is correct
10 Correct 864 ms 37544 KB Output is correct
11 Correct 533 ms 36568 KB Output is correct
12 Correct 1016 ms 49716 KB Output is correct
13 Correct 873 ms 46304 KB Output is correct
14 Correct 858 ms 45444 KB Output is correct
15 Correct 506 ms 34320 KB Output is correct
16 Correct 782 ms 50200 KB Output is correct
17 Correct 954 ms 46416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 551 ms 34484 KB Output is correct
2 Correct 945 ms 56392 KB Output is correct
3 Correct 106 ms 28132 KB Output is correct
4 Correct 767 ms 36032 KB Output is correct
5 Correct 905 ms 57888 KB Output is correct
6 Correct 849 ms 46620 KB Output is correct
7 Correct 732 ms 35976 KB Output is correct
8 Correct 810 ms 40828 KB Output is correct
9 Correct 799 ms 39000 KB Output is correct
10 Correct 836 ms 37916 KB Output is correct
11 Correct 641 ms 36272 KB Output is correct
12 Correct 865 ms 54016 KB Output is correct
13 Correct 834 ms 45504 KB Output is correct
14 Correct 877 ms 45348 KB Output is correct
15 Correct 523 ms 34304 KB Output is correct
16 Correct 797 ms 50644 KB Output is correct
17 Correct 834 ms 46536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 24156 KB Output is correct
2 Incorrect 11 ms 24156 KB Output isn't correct
3 Halted 0 ms 0 KB -