Submission #916599

# Submission time Handle Problem Language Result Execution time Memory
916599 2024-01-26T06:54:04 Z GrindMachine Unique Cities (JOI19_ho_t5) C++17
64 / 100
1050 ms 61780 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(d-mx,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(d-mx,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 8 ms 24152 KB Output is correct
2 Incorrect 12 ms 24152 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 432 ms 31372 KB Output is correct
2 Correct 487 ms 43308 KB Output is correct
3 Correct 84 ms 27536 KB Output is correct
4 Correct 831 ms 35924 KB Output is correct
5 Correct 965 ms 56916 KB Output is correct
6 Correct 945 ms 46160 KB Output is correct
7 Correct 717 ms 36044 KB Output is correct
8 Correct 809 ms 38228 KB Output is correct
9 Correct 742 ms 37460 KB Output is correct
10 Correct 742 ms 37448 KB Output is correct
11 Correct 577 ms 36428 KB Output is correct
12 Correct 1050 ms 49708 KB Output is correct
13 Correct 892 ms 46312 KB Output is correct
14 Correct 957 ms 45564 KB Output is correct
15 Correct 510 ms 34324 KB Output is correct
16 Correct 872 ms 50012 KB Output is correct
17 Correct 928 ms 46640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 554 ms 34484 KB Output is correct
2 Correct 953 ms 59996 KB Output is correct
3 Correct 127 ms 28548 KB Output is correct
4 Correct 761 ms 39808 KB Output is correct
5 Correct 1020 ms 61780 KB Output is correct
6 Correct 991 ms 50208 KB Output is correct
7 Correct 768 ms 39756 KB Output is correct
8 Correct 889 ms 44668 KB Output is correct
9 Correct 824 ms 42816 KB Output is correct
10 Correct 750 ms 41444 KB Output is correct
11 Correct 750 ms 39880 KB Output is correct
12 Correct 1002 ms 57652 KB Output is correct
13 Correct 889 ms 49268 KB Output is correct
14 Correct 980 ms 49156 KB Output is correct
15 Correct 550 ms 38200 KB Output is correct
16 Correct 859 ms 54472 KB Output is correct
17 Correct 1004 ms 50320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 24152 KB Output is correct
2 Incorrect 12 ms 24152 KB Output isn't correct
3 Halted 0 ms 0 KB -