답안 #198118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198118 2020-01-24T18:19:22 Z model_code Magic Tree (CEOI19_magictree) C++17
100 / 100
503 ms 110312 KB
// Stack of interval trees over implicit HLD.
// O(N log^2 N)
#include <bits/stdc++.h>
using namespace std;

using wgt = unsigned long long;

class Solver {
    using day = int;

    class IntervalTree {
        struct node {
            wgt val, add;
        };

        int b;
        vector<node> T;

        inline __attribute__((always_inline)) void upd(int i, bool leaf) {
            wgt add = T[i].add;
            if(add == 0) return;
            T[i].add = 0;
            T[i].val += add;
            if(!leaf) {
                T[2*i].add += add;
                T[2*i+1].add += add;
            }
        }

        void traverse_add_internal(vector< pair<int, wgt> > & L_values, int & pos, wgt & cur_max, int i, int n_l, int n_r) {
            if(n_l+1 == n_r) {
                T[i].val = cur_max = max(cur_max, T[i].val + T[i].add);
                T[i].add = 0;
                if(pos+1 != (int)L_values.size() && L_values[pos+1].first == n_l) pos++;
                T[i].val += L_values[pos].second;
                return;
            }
            if(L_values[pos].first < n_l && (pos+1 == (int)L_values.size() || n_r <= L_values[pos+1].first)) {
                cur_max = max(cur_max, T[i].val + T[i].add);
                T[i].add += L_values[pos].second;
                upd(i, n_l+1 == n_r);
                return;
            }
            upd(i, n_l+1 == n_r);
            int c = (n_l + n_r) / 2;
            traverse_add_internal(L_values, pos, cur_max, 2*i, n_l, c);
            traverse_add_internal(L_values, pos, cur_max, 2*i+1, c, n_r);
            T[i].val = max(T[2*i].val, T[2*i+1].val);
        }

    public:
        IntervalTree() {}

        IntervalTree(int N) {
            b = 1;
            while(b < N) b *= 2;
            T.resize(2*b+1, {0, 0});
        }

        void add(int l, int r, wgt w_add, int i = 1, int n_l = 0, int n_r = 0) {
            if(i == 1) n_r = b;
            if(n_l == l && n_r == r) {
                T[i].add += w_add;
                upd(i, n_l+1 == n_r);
                return;
            }
            upd(i, n_l+1 == n_r);
            if(n_l >= r || l >= n_r) return;
            int c = (n_l + n_r) / 2;
            add(l, min(r, c), w_add, 2*i, n_l, c);
            add(max(l, c), r, w_add, 2*i+1, c, n_r);
            T[i].val = max(T[2*i].val, T[2*i+1].val);
        }

        void put(int pos, wgt w) {
            int cur = 1, n_l = 0, n_r = b;
            while(n_l+1 != n_r) {
                upd(cur, false);
                int c = (n_l + n_r) / 2;
                if(pos < c) {
                    upd(2*cur+1, c+1 == n_r);
                    cur = 2*cur;
                    n_r = c;
                }
                else {
                    upd(2*cur, n_l+1 == c);
                    cur = 2*cur+1;
                    n_l = c;
                }
            }
            T[cur].val = w;
            T[cur].add = 0;
            for(cur /= 2; cur > 0; cur /= 2)
                T[cur].val = max(T[2*cur].val, T[2*cur+1].val);
        }

        wgt get_max(int pos) { // max [0..pos]
            wgt ret = 0;
            int cur = 1, n_l = 0, n_r = b;
            while(true) {
                upd(cur, n_l+1 == n_r);
                if(pos+1 == n_r) {
                    ret = max(ret, T[cur].val);
                    break;
                }
                if(T[cur].val <= ret) break;
                int c = (n_l + n_r) / 2;
                if(pos < c) {
                    cur = 2*cur;
                    n_r = c;
                    continue;
                }
                upd(2*cur, n_l+1 == c);
                ret = max(ret, T[2*cur].val);
                cur = 2*cur+1;
                n_l = c;
            }
            return ret;
        }

        void traverse_add(vector< pair<int, wgt> > & L_values) {
            int pos = 0;
            wgt cur_max = 0;
            traverse_add_internal(L_values, pos, cur_max, 1, 0, b);
        }

        void traverse_extract_clear(vector< pair<int, wgt> > & L_values, wgt add = 0, int i = 1, int n_l = 0, int n_r = 0) {
            if(i == 1) n_r = b;
            add += T[i].add;
            T[i].add = 0;
            if(T[i].val == 0 || n_l+1 == n_r) {
                if(add+T[i].val > 0)
                    if(L_values.empty() || L_values.back().second < add+T[i].val)
                        L_values.push_back({n_l, add+T[i].val});
                T[i].val = 0;
                return;
            }
            T[i].val = 0;
            int c = (n_l + n_r) / 2;
            traverse_extract_clear(L_values, add, 2*i, n_l, c);
            traverse_extract_clear(L_values, add, 2*i+1, c, n_r);
        }
    };

    int N, K;
    vector< vector<int> > son;
    vector<wgt> W;
    vector<day> D;
    vector<int> sz, max_son;
    vector<wgt> LIS_W; // largest increasing subgraph weight
    vector<IntervalTree> interval_trees;
    int tree_stack_depth;

    void DFS_init(int R) {
        int max_son_sz = 0;
        for(auto v : son[R]) {
            DFS_init(v);
            sz[R] += sz[v];
            if(sz[v] > max_son_sz) {
                max_son_sz = sz[v];
                max_son[R] = v;
            }
        }
    }

    void DFS_solve(int R, IntervalTree & this_itree) {
        if(max_son[R] == 0) {
            if(D[R] > 0) {
                LIS_W[R] = W[R];
                this_itree.put(D[R]-1, W[R]);
            }
            return;
        }

        DFS_solve(max_son[R], this_itree);

        for(auto v : son[R]) if(v != max_son[R]) {
            IntervalTree & subtree_itree = interval_trees[tree_stack_depth];
            tree_stack_depth++;

            DFS_solve(v, subtree_itree);

            vector< pair<int, wgt> > subtree_L_values;
            subtree_L_values.reserve(sz[v]+1);
            subtree_itree.traverse_extract_clear(subtree_L_values);
            tree_stack_depth--;

            if(subtree_L_values.empty()) continue;
            if(subtree_L_values[0].first > 0) subtree_L_values.insert(begin(subtree_L_values), {0, 0});
            this_itree.traverse_add(subtree_L_values);
        }

        if(D[R] > 0) {
            LIS_W[R] = this_itree.get_max(D[R]-1) + W[R];
            this_itree.put(D[R]-1, LIS_W[R]);
        }
    }

public:
    Solver(vector< vector<int> > & son_, vector<wgt> & W_, vector<day> & D_, int K_)
            : N(son_.size()), W(W_), K(K_), son(son_), D(D_) {
        // D[v] == 0: v is empty
        sz.resize(N, 1);
        max_son.resize(N, 0); // 0: leaf
        LIS_W.resize(N, 0);
        interval_trees.resize(18, IntervalTree(K));
        tree_stack_depth = 0;

        DFS_init(0);
    }

    wgt solve() {
        IntervalTree & full_itree = interval_trees[tree_stack_depth];
        tree_stack_depth++;

        DFS_solve(0, full_itree);

        wgt ans = full_itree.get_max(K-1);

        vector< pair<int, wgt> > V;
        full_itree.traverse_extract_clear(V);
        tree_stack_depth--;

        return ans;
    }
};

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> par(N, 0);
    vector< vector<int> > son(N);
    for(int i = 1; i < N; i++) {
        cin >> par[i];
        par[i]--;
        son[par[i]].push_back(i);
    }
    vector<wgt> W(N, 0);
    vector<int> D(N, 0);
    for(int i = 0; i < M; i++) {
        int v, d, w;
        cin >> v >> d >> w;
        v--;
        D[v] = d;
        W[v] = w;
    }
    Solver solver(son, W, D, K);
    cout << solver.solve() << "\n";
}

Compilation message

magictree.cpp: In constructor 'Solver::Solver(std::vector<std::vector<int> >&, std::vector<long long unsigned int>&, std::vector<int>&, int)':
magictree.cpp:147:17: warning: 'Solver::W' will be initialized after [-Wreorder]
     vector<wgt> W;
                 ^
magictree.cpp:145:12: warning:   'int Solver::K' [-Wreorder]
     int N, K;
            ^
magictree.cpp:200:5: warning:   when initialized here [-Wreorder]
     Solver(vector< vector<int> > & son_, vector<wgt> & W_, vector<day> & D_, int K_)
     ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 297 ms 90268 KB Output is correct
2 Correct 170 ms 96660 KB Output is correct
3 Correct 472 ms 88600 KB Output is correct
4 Correct 273 ms 88336 KB Output is correct
5 Correct 294 ms 88700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1244 KB Output is correct
2 Correct 3 ms 1272 KB Output is correct
3 Correct 3 ms 1244 KB Output is correct
4 Correct 175 ms 108400 KB Output is correct
5 Correct 177 ms 110312 KB Output is correct
6 Correct 191 ms 108400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 12644 KB Output is correct
2 Correct 101 ms 12684 KB Output is correct
3 Correct 99 ms 21924 KB Output is correct
4 Correct 62 ms 10328 KB Output is correct
5 Correct 85 ms 34452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 129 ms 12676 KB Output is correct
11 Correct 121 ms 12588 KB Output is correct
12 Correct 106 ms 21952 KB Output is correct
13 Correct 77 ms 10336 KB Output is correct
14 Correct 82 ms 34424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 41848 KB Output is correct
2 Correct 114 ms 51584 KB Output is correct
3 Correct 132 ms 90636 KB Output is correct
4 Correct 141 ms 90592 KB Output is correct
5 Correct 94 ms 88372 KB Output is correct
6 Correct 131 ms 90064 KB Output is correct
7 Correct 133 ms 95856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 3 ms 1244 KB Output is correct
11 Correct 3 ms 1272 KB Output is correct
12 Correct 3 ms 1244 KB Output is correct
13 Correct 175 ms 108400 KB Output is correct
14 Correct 177 ms 110312 KB Output is correct
15 Correct 191 ms 108400 KB Output is correct
16 Correct 129 ms 12676 KB Output is correct
17 Correct 121 ms 12588 KB Output is correct
18 Correct 106 ms 21952 KB Output is correct
19 Correct 77 ms 10336 KB Output is correct
20 Correct 82 ms 34424 KB Output is correct
21 Correct 41 ms 4080 KB Output is correct
22 Correct 154 ms 13868 KB Output is correct
23 Correct 340 ms 90692 KB Output is correct
24 Correct 466 ms 90652 KB Output is correct
25 Correct 258 ms 88224 KB Output is correct
26 Correct 282 ms 91444 KB Output is correct
27 Correct 233 ms 96624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 6 ms 348 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 297 ms 90268 KB Output is correct
11 Correct 170 ms 96660 KB Output is correct
12 Correct 472 ms 88600 KB Output is correct
13 Correct 273 ms 88336 KB Output is correct
14 Correct 294 ms 88700 KB Output is correct
15 Correct 3 ms 1244 KB Output is correct
16 Correct 3 ms 1272 KB Output is correct
17 Correct 3 ms 1244 KB Output is correct
18 Correct 175 ms 108400 KB Output is correct
19 Correct 177 ms 110312 KB Output is correct
20 Correct 191 ms 108400 KB Output is correct
21 Correct 109 ms 12644 KB Output is correct
22 Correct 101 ms 12684 KB Output is correct
23 Correct 99 ms 21924 KB Output is correct
24 Correct 62 ms 10328 KB Output is correct
25 Correct 85 ms 34452 KB Output is correct
26 Correct 129 ms 12676 KB Output is correct
27 Correct 121 ms 12588 KB Output is correct
28 Correct 106 ms 21952 KB Output is correct
29 Correct 77 ms 10336 KB Output is correct
30 Correct 82 ms 34424 KB Output is correct
31 Correct 49 ms 41848 KB Output is correct
32 Correct 114 ms 51584 KB Output is correct
33 Correct 132 ms 90636 KB Output is correct
34 Correct 141 ms 90592 KB Output is correct
35 Correct 94 ms 88372 KB Output is correct
36 Correct 131 ms 90064 KB Output is correct
37 Correct 133 ms 95856 KB Output is correct
38 Correct 41 ms 4080 KB Output is correct
39 Correct 154 ms 13868 KB Output is correct
40 Correct 340 ms 90692 KB Output is correct
41 Correct 466 ms 90652 KB Output is correct
42 Correct 258 ms 88224 KB Output is correct
43 Correct 282 ms 91444 KB Output is correct
44 Correct 233 ms 96624 KB Output is correct
45 Correct 35 ms 4080 KB Output is correct
46 Correct 154 ms 13912 KB Output is correct
47 Correct 350 ms 90616 KB Output is correct
48 Correct 503 ms 90652 KB Output is correct
49 Correct 291 ms 88336 KB Output is correct
50 Correct 293 ms 91476 KB Output is correct
51 Correct 246 ms 96672 KB Output is correct