Submission #21473312


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
// vid : id -> vid          head, heavy, parent : unused
// depth : id -> depth      inv : vid -> id
// in(vid), out(vid) : for euler tree [in[v], out[v])
struct HLDecomposition {
    vector<set<int>> g; vector<int> vid, head, heavy, parent, depth, inv, in, out;
    HLDecomposition() {} HLDecomposition(int n) { init(n); }
    void init(int n) {
        g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1);
        parent.resize(n); depth.resize(n); inv.resize(n); in.resize(n); out.resize(n);
    }
    void add(int u, int v) { g[u].insert(v); g[v].insert(u); }
	void build(int root) { dfs(root, -1); t = 0; dfs_hld(root); }

    int dfs(int curr, int prev) {
        parent[curr] = prev; int sub = 1, max_sub = 0;
        heavy[curr] = -1;
        for (int next : g[curr]) if (next != prev) {
            depth[next] = depth[curr] + 1;
            int sub_next = dfs(next, curr); sub += sub_next;
            if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
        }return sub;
    }

    int t = 0;
    void dfs_hld(int v = 0)
    {
        vid[v] = in[v] = t;
        t++;
        inv[in[v]] = v;
        if (0 <= heavy[v]) {
            head[heavy[v]] = head[v];
            dfs_hld(heavy[v]);
        }
        for (auto u : g[v]) if(depth[v] < depth[u])  if (u != heavy[v]) {
            head[u] = u;
            dfs_hld(u);
        }
        out[v] = t;
    }


    void foreach(int u, int v, function<void(int, int)> f) { // [x,y]
        if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
        if (head[u] != head[v]) foreach(u, parent[head[v]], f);
    }
    int ancestor(int from, int times) {
        while (true) {
            if (depth[head[from]] > depth[from] - times) {
                times -= depth[from] - depth[head[from]] + 1; if (head[from] == 0)return -1; from = parent[head[from]];
            }
            else return inv[vid[from] - times];
        }
    }
    int lca(int u, int v) {
        if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
        return lca(u, parent[head[v]]);
    }
    int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
    int child(int parent, int child, int times) {
        assert(depth[parent]<depth[child]);
        int d = distance(parent, child); assert(times - 1 <= d); return ancestor(child, d - times);
    }
    int go(int from, int to, int times) {
        int d = distance(from, to); assert(0 <= times and times <= d);
        int lc = lca(from, to); if (lc == to)return ancestor(from, times); if (lc == from)return child(from, to, times);
        int dd = distance(from, lc); if (dd <= times)return go(lc, to, times - dd); return ancestor(from, times);
    }
};

/*


int ancestor(int u, int d)
// uのd個上の頂点を返す(無いなら-1)

int child(int u, int v, int d)
// uからv方向へd個進んだ頂点を返す(子供方向のみ)

int go(int u, int v, int d)
// uからv方向へd個進んだ頂点を返す(どの方向でもOK)

foreachの区間は閉区間

*/



template<class V, int NV> struct LazySegTree { // [L,R)
    vector<V> dat, lazy; LazySegTree() { dat.resize(NV * 2, def); lazy.resize(NV * 2, ldef); }
    void update(int a, int b, V v, int k, int l, int r) {
        push(k, l, r); if (r <= a || b <= l) return;
        if (a <= l && r <= b) { setLazy(k, v); push(k, l, r); }
        else {
            update(a, b, v, k * 2, l, (l + r) / 2); update(a, b, v, k * 2 + 1, (l + r) / 2, r);
            dat[k] = comp(dat[k * 2], dat[k * 2 + 1]);
        }
    }
    V get(int a, int b, int k, int l, int r) {
        push(k, l, r); if (r <= a || b <= l) return def;
        if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2, l, (l + r) / 2);
        auto y = get(a, b, k * 2 + 1, (l + r) / 2, r); return comp(x, y);
    }
    void update(int a, int b, V v) { update(a, b, v, 1, 0, NV); }
    V get(int a, int b) { return get(a, b, 1, 0, NV); }
    // ---- Template ---------------------------------------------------------------------------------
    // 区間代入,区間和
    const V def = 0, ldef = -1;
    V comp(V l, V r) { return l + r; }
    void setLazy(int i, V v) { lazy[i] = v; }
    void push(int k, int l, int r) {
        if (lazy[k] != ldef) {
            // modify------------------------------
            dat[k] = lazy[k] * (r - l);
            // ------------------------------------
            if (r - l > 1) { setLazy(k * 2, lazy[k]); setLazy(k * 2 + 1, lazy[k]); }
            lazy[k] = ldef;
        }
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, Q, A[101010], B[101010];
HLDecomposition hld;
LazySegTree<int, 1 << 17> st;
int ans[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> Q;

    hld.init(N);
    rep(i, 0, N - 1) {
        cin >> A[i] >> B[i];
        A[i]--; B[i]--;
        hld.add(A[i], B[i]);
    }
    hld.build(0);

    rep(_, 0, Q) {
        int u, v, c; cin >> u >> v >> c;
        u--; v--;
        int lc = hld.lca(u, v);
        int tmp = st.get(hld.vid[lc], hld.vid[lc] + 1);
        hld.foreach(u, v, [&](int a, int b) {
            st.update(a, b + 1, c);
        });
        st.update(hld.vid[lc], hld.vid[lc] + 1, tmp);
    }

    map<int, int> dic;
    rep(i, 0, N - 1) {
        int x = A[i];
        int y = B[i];
        if (hld.depth[x] > hld.depth[y]) swap(x, y);
        dic[y] = i;
    }

    rep(i, 1, N) {
        int c = st.get(hld.vid[i], hld.vid[i] + 1);
        ans[dic[i]] = c;
    }

    rep(i, 0, N - 1) printf("%d\n", ans[i]);
}





Submission Info

Submission Time
Task M - 筆塗り
User hamayanhamayan
Language C++ (GCC 9.2.1)
Score 6
Code Size 7032 Byte
Status AC
Exec Time 536 ms
Memory 32500 KiB

Compile Error

./Main.cpp: In member function ‘void HLDecomposition::foreach(int, int, std::function<void(int, int)>)’:
./Main.cpp:55:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   55 |         if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
      |         ^~
./Main.cpp:55:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   55 |         if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
      |                                          ^
./Main.cpp: In member function ‘int HLDecomposition::lca(int, int)’:
./Main.cpp:67:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   67 |         if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
      |         ^~
./Main.cpp:67:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   67 |         if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
      |                                          ^~
./Main.cpp: In member function ‘V LazySegTree<V, NV>::get(int, int, int, int, int)’:
./Main.cpp:112:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
  112 |         if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2, l, (l + r) / 2);
      |         ^~
./Main.cpp:112:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
  112 |         if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2, l, (l + r) / 2);
      |                                              ^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 6 / 6
Status
AC × 2
AC × 32
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
random_01.txt AC 10 ms 5072 KiB
random_02.txt AC 417 ms 28156 KiB
random_03.txt AC 189 ms 25980 KiB
random_04.txt AC 42 ms 7120 KiB
random_05.txt AC 105 ms 18392 KiB
random_06.txt AC 19 ms 6996 KiB
random_07.txt AC 256 ms 10816 KiB
random_08.txt AC 102 ms 14588 KiB
random_09.txt AC 252 ms 25980 KiB
random_10.txt AC 222 ms 14776 KiB
random_11.txt AC 7 ms 5132 KiB
random_12.txt AC 536 ms 28056 KiB
random_13.txt AC 163 ms 26244 KiB
random_14.txt AC 443 ms 22460 KiB
random_15.txt AC 324 ms 18772 KiB
random_16.txt AC 116 ms 26056 KiB
random_17.txt AC 264 ms 32500 KiB
random_18.txt AC 67 ms 16636 KiB
random_19.txt AC 207 ms 30892 KiB
random_20.txt AC 86 ms 7108 KiB
random_21.txt AC 39 ms 11476 KiB
random_22.txt AC 139 ms 6848 KiB
random_23.txt AC 59 ms 11048 KiB
random_24.txt AC 68 ms 15804 KiB
random_25.txt AC 101 ms 11344 KiB
random_26.txt AC 62 ms 14388 KiB
random_27.txt AC 144 ms 10424 KiB
random_28.txt AC 185 ms 22208 KiB
random_29.txt AC 155 ms 26108 KiB
random_30.txt AC 149 ms 22136 KiB
sample_01.txt AC 6 ms 5004 KiB
sample_02.txt AC 4 ms 5040 KiB