Submission #1359588

#TimeUsernameProblemLanguageResultExecution timeMemory
1359588PakinDioxideCollecting Stamps 5 (JOI26_stamps)C++17
100 / 100
1761 ms62904 KiB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <climits>

using namespace std;

using ll = long long;
using db = long double;
using str = string;

using pi = pair <int, int>;
using pl = pair <ll, ll>;
using pd = pair <db, db>;
#define mp make_pair
#define ff first
#define ss second

template <class T> using V = vector <T>;
using vi = V <int>;
using vb = V <bool>;
using vl = V <ll>;
using vd = V <db>;
using vs = V <str>;
using vpi = V <pi>;
using vpl = V <pl>;
using vpd = V <pd>;
template <class T> using PQ = priority_queue <T>;

#define sz(x) (int) size(x)
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define srt(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define emb emplace_back
#define em emplace
#define ft front()
#define bk back()

#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define F0R(i, a) FOR(i, 0, a-1)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a-1)
#define each(e, x) for (auto &e : x)

ll cdiv(ll a, ll b) { return ceil((db) a / b); }
ll fdiv(ll a, ll b) { return floor((db) a / b); }
template <class T> bool ckmin(T &a, const T &b) {
    return b < a ? a = b, 1 : 0;
}
template <class T> bool ckmax(T &a, const T &b) {
    return b > a ? a = b, 1 : 0;
}

template <typename T, typename = void>
struct is_iterable : false_type {};

template <typename T>
struct is_iterable<T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>> : true_type {};

void err(string name) {}
template <typename T, typename... Args>
void err(string name, T val, Args... args) {
    size_t pos = name.find(',');
    string first = (pos == string::npos) ? name : name.substr(0, pos);
    string rest = (pos == string::npos) ? "" : name.substr(pos + 2);

    cout << first << " = "; 
    
    if constexpr (is_iterable<T>::value && !is_same_v<T, string>) {
        cout << "{";
        bool fst = true;
        for (auto const& x : val) {
            if (!fst) cout << ", ";
            cout << x;
            fst = false;
        }
        cout << "}";
    } else {
        cout << val;
    }

    if (rest != "") {
        cout << " | ";
        err(rest, args...);
    } else {
        cout << '\n';
    }
}

#ifdef LOCAL
#define dbg(...) cout << "[" << __FUNCTION__ << ":" << __LINE__ << "] ", err(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 67
#endif

void setIO(string name = "") {
    ios::sync_with_stdio(0), cin.tie(0);
    if (sz(name)) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

const ll INF = 1e18;
// const pi d[4] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 
const int mod = 998244353; // 1e9+7;
const int mxN = 4e5+5;


struct FEN {
    ll fen[mxN];

    void upd(int idx, int x) {
        dbg("UPD", idx, x);
        idx = max(idx, 0);
        idx++;
        for (int i = idx; i < mxN; i += i & -i) fen[i] += x;
    }

    int qr(int idx) {
        idx = max(idx, 0);
        idx++; int x = 0;
        for (int i = idx; i > 0; i -= i & -i) x += fen[i];
        if (x < 0) {
            dbg("BRUH", x, idx);
        }
        // assert(x >= 0);
        return x;
    }
} F1, F2;

int T[mxN], n, d, sz[mxN], vis[mxN], dep[mxN], dp[mxN], ans[mxN];
vi adj[mxN];


int fsz(int u, int p) {
    sz[u] = 1;
    for (auto v : adj[u]) if (!vis[v] && v != p) sz[u] += fsz(v, u);
    return sz[u];
}

int fct(int u, int p, int SZ) {
    for (auto v : adj[u]) if (!vis[v] && v != p && 2 * sz[v] > SZ) return fct(v, u, SZ);
    return u;
}

void fdp(int u, int p) {
    if (u == p) dep[u] = 0, dp[u] = T[u] - dep[u];
    else dep[u] = dep[p] + 1, dp[u] = min(dp[p], T[u] - dep[u]);
    for (auto v : adj[u]) if (!vis[v] && v != p) fdp(v, u);
}

void ctd(int u) {
    fsz(u, u);
    int SZ = sz[u]+1;
    u = fct(u, u, sz[u]);
    fsz(u, u);
    fdp(u, u);
    vis[u] = 1;
    dbg(u);
    vi F1(SZ+1), F2(SZ+1);
    auto upd = [&] (vi &v, int idx, int x) {
        idx = min(idx, sz(v)-1);
        idx = max(idx, 0);
        v[idx] += x;
    };
    function <void(int, int)> d1 = [&] (int u, int p) {
        if (dp[u] <= d - dep[u] + 1) upd(F1, dp[u], 1), upd(F1, d - dep[u] + 1, -1);
        if (0 <= d - dep[u] + 1) upd(F2, 0, 1), upd(F2, d - dep[u] + 1, -1);
        for (auto v : adj[u]) if (!vis[v] && v != p) d1(v, u);
    };
    d1(u, u);
    for (int i = 1; i <= SZ; i++) F1[i] += F1[i-1], F2[i] += F2[i-1];
    for (auto v : adj[u]) if (!vis[v]) {
        dbg("DFS", v);
        int SZx = sz[v]+1;
        vi F1x(SZx+1), F2x(SZx+1);
        function <void(int, int)> d1 = [&] (int u, int p) {
            if (dp[u] <= d - dep[u] + 1) upd(F1x, dp[u], -1), upd(F1x, d - dep[u] + 1, 1);
            if (0 <= d - dep[u] + 1) upd(F2x, 0, -1), upd(F2x, d - dep[u] + 1, 1);
            for (auto v : adj[u]) if (!vis[v] && v != p) d1(v, u);
        };
        d1(v, v);
        for (int i = 1; i <= SZx; i++) F1x[i] += F1x[i-1], F2x[i] += F2x[i-1];
        function <void(int, int, int)> d2 = [&] (int u, int p, int mn) {
            mn = min(mn, T[u] + dep[u]);
            if (dep[u] >= mn) ans[u] += F2[dep[u]] + F2x[dep[u]];
            else ans[u] += F1[dep[u]] + F1x[dep[u]];
            // dbg(u, F1.qr(dep[u]));
            for (auto v : adj[u]) if (!vis[v] && v != p) d2(v, u, mn);
        };
        d2(v, v, INT_MAX);
        F1x.clear(), F2x.clear();
    }
    ans[u] += F1[0];
    F1.clear(), F2.clear();
    // dbg(u);
    // for (int i = 1; i <= n; i++) cerr << ans[i] << ' '; cerr << '\n';
    for (auto v : adj[u]) if (!vis[v]) ctd(v);
}

int main() {
    setIO();
    // D(u, ct) + D(v, ct) <= d
    // D(v, ct) <= d + D(u, ct)
    // collect dp that what is min time to walk from some node and to centroid to v. dp[v] = min(dp[p[v]], T_v - D(v, ct));
    // then D(u, ct) >= dp[v] && D(u, ct) <= d - D(v, ct)
    // dp[v] <= D(u, ct) <= d - D(v, ct)
    // do some kind of prefix sum yay ac.
    // will it mem exceed?
    cin >> n >> d;
    FOR(i, 1, n) cin >> T[i];
    FOR(i, 1, n-1) {
        int u, v; cin >> u >> v;
        adj[u].emb(v);
        adj[v].emb(u);
    }
    ctd(1);
    for (int i = 1; i <= n; i++) cout << ans[i] << '\n';
}










Compilation message (stderr)

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...