Submission #916596

# Submission time Handle Problem Language Result Execution time Memory
916596 2024-01-26T06:52:20 Z GrindMachine Unique Cities (JOI19_ho_t5) C++17
32 / 100
1055 ms 59872 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);
    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],1ll);
        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 24152 KB Output is correct
2 Incorrect 14 ms 24156 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 391 ms 32952 KB Output is correct
2 Correct 395 ms 44888 KB Output is correct
3 Correct 126 ms 27728 KB Output is correct
4 Correct 745 ms 38992 KB Output is correct
5 Correct 856 ms 59872 KB Output is correct
6 Correct 1055 ms 49084 KB Output is correct
7 Correct 683 ms 38876 KB Output is correct
8 Correct 771 ms 41296 KB Output is correct
9 Correct 711 ms 40468 KB Output is correct
10 Correct 771 ms 40272 KB Output is correct
11 Correct 585 ms 39364 KB Output is correct
12 Correct 1041 ms 52452 KB Output is correct
13 Correct 904 ms 49340 KB Output is correct
14 Correct 831 ms 48328 KB Output is correct
15 Correct 483 ms 37136 KB Output is correct
16 Correct 868 ms 53052 KB Output is correct
17 Correct 945 ms 49136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 607 ms 37260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 24152 KB Output is correct
2 Incorrect 14 ms 24156 KB Output isn't correct
3 Halted 0 ms 0 KB -