Submission #957826

# Submission time Handle Problem Language Result Execution time Memory
957826 2024-04-04T11:16:50 Z Ghetto Magic Tree (CEOI19_magictree) C++17
100 / 100
1668 ms 454788 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAX_N = 1e5 + 5, MAX_K = 1e5 + 5;

int n, m, k;
vector<int> children[MAX_N];
int t[MAX_N];
lint val[MAX_N]; // val = 0 => not fruit

struct Node {
    lint ans, lazy_add, lazy_set;
};
vector<Node> segtree[MAX_N];
int n_nodes[MAX_N];
vector<int> lo[MAX_N], hi[MAX_N], l_child[MAX_N], r_child[MAX_N];
void init() {
    for (int i = 1; i <= n; i++) {
        n_nodes[i] = 1;
        segtree[i] = {{0, 0, 0}, {0, 0, 0}};
        l_child[i] = r_child[i] = {0, 0};
        lo[i] = {0, 1};
        hi[i] = {0, MAX_K};
    }
}
void expand(int tree, int u) {
    if (l_child[tree][u]) return;

    l_child[tree][u] = ++n_nodes[tree];
    r_child[tree][u] = ++n_nodes[tree];

    for (int i = 1; i <= 2; i++) {
        segtree[tree].push_back({0, 0, 0});
        lo[tree].push_back(0);
        hi[tree].push_back(0);
        l_child[tree].push_back(0);
        r_child[tree].push_back(0);
    }
    
    int mid = (lo[tree][u] + hi[tree][u]) / 2;
    lo[tree][l_child[tree][u]] = lo[tree][u];
    hi[tree][l_child[tree][u]] = mid;
    lo[tree][r_child[tree][u]] = mid + 1;
    hi[tree][r_child[tree][u]] = hi[tree][u];
}
void propogate(int tree, int u) {
    if (segtree[tree][u].lazy_set != 0) {
        segtree[tree][l_child[tree][u]].ans = segtree[tree][u].lazy_set;
        segtree[tree][r_child[tree][u]].ans = segtree[tree][u].lazy_set;
        segtree[tree][l_child[tree][u]].lazy_set = segtree[tree][u].lazy_set;
        segtree[tree][r_child[tree][u]].lazy_set = segtree[tree][u].lazy_set;
        segtree[tree][l_child[tree][u]].lazy_add = 0;
        segtree[tree][r_child[tree][u]].lazy_add = 0;
    }
    if (segtree[tree][u].lazy_add != 0) {
        segtree[tree][l_child[tree][u]].ans += segtree[tree][u].lazy_add;    
        segtree[tree][r_child[tree][u]].ans += segtree[tree][u].lazy_add;
        segtree[tree][l_child[tree][u]].lazy_add += segtree[tree][u].lazy_add;    
        segtree[tree][r_child[tree][u]].lazy_add += segtree[tree][u].lazy_add;
    }
    segtree[tree][u].lazy_set = segtree[tree][u].lazy_add = 0;
}
void set_update(int tree, int l, int r, lint x, int u = 1) {
    if (l <= lo[tree][u] && hi[tree][u] <= r) {
        // cout << lo[tree][u] << " " << hi[tree][u] << " " << u << ": " << x << endl;
        segtree[tree][u].ans = x;
        segtree[tree][u].lazy_set = x;
        segtree[tree][u].lazy_add = 0;
        return;
    } 

    expand(tree, u);
    propogate(tree, u);
    int mid = (lo[tree][u] + hi[tree][u]) / 2;
    if (l <= mid) set_update(tree, l, r, x, l_child[tree][u]);
    if (r > mid) set_update(tree, l, r, x, r_child[tree][u]);
    segtree[tree][u].ans = max(segtree[tree][l_child[tree][u]].ans, segtree[tree][r_child[tree][u]].ans);
}
void add_update(int tree, int l, int r, lint x, int u = 1) {
    if (l <= lo[tree][u] && hi[tree][u] <= r) {
        segtree[tree][u].ans += x;
        segtree[tree][u].lazy_add += x;
        return;
    } 

    expand(tree, u);
    propogate(tree, u);
    int mid = (lo[tree][u] + hi[tree][u]) / 2;
    if (l <= mid) add_update(tree, l, r, x, l_child[tree][u]);
    if (r > mid) add_update(tree, l, r, x, r_child[tree][u]);
    segtree[tree][u].ans = max(segtree[tree][l_child[tree][u]].ans, segtree[tree][r_child[tree][u]].ans);
}
lint query(int tree, int i, int u = 1) {
    // cout << "QUERY " << lo[tree][u] << " " << hi[tree][u] << " " << u << ": " << segtree[tree][u].ans << " " << segtree[tree][u].lazy_set << " " << segtree[tree][u].lazy_add << endl;
    if (lo[tree][u] == hi[tree][u]) {
        // cout << "QUERY" << lo[tree][u] << " " << segtree[tree][u].ans << endl;
        return segtree[tree][u].ans;
    }

    expand(tree, u);
    propogate(tree, u);
    int mid = (lo[tree][u] + hi[tree][u]) / 2;
    if (i <= mid) return query(tree, i, l_child[tree][u]);
    else return query(tree, i, r_child[tree][u]);
}
int walk(int tree, int l, int r, lint x, int u = 1) { // First i >= x
    if (segtree[tree][u].ans < x) return r + 1;
    if (lo[tree][u] > r || hi[tree][u] < l) return r + 1;
    if (lo[tree][u] == hi[tree][u]) return lo[tree][u];

    expand(tree, u);
    propogate(tree, u);
    int l_resp = walk(tree, l, r, x, l_child[tree][u]);
    if (l_resp != r + 1) return l_resp;
    return walk(tree, l, r, x, r_child[tree][u]);
}

void merge_update(int tree1, int tree2, int u = 1) {
    if (l_child[tree1][u] == 0) {
        // cout << tree1 << " " << tree2 << " " << segtree[tree1][u].ans << endl;
        add_update(tree2, lo[tree1][u], hi[tree1][u], segtree[tree1][u].ans);
        return;
    }

    expand(tree1, u);
    propogate(tree1, u);
    merge_update(tree1, tree2, l_child[tree1][u]);
    merge_update(tree1, tree2, r_child[tree1][u]);
}
void merge(int u, int v) {
    if (n_nodes[u] < n_nodes[v]) {
        swap(n_nodes[u], n_nodes[v]);
        segtree[u].swap(segtree[v]);
        lo[u].swap(lo[v]);
        hi[u].swap(hi[v]);
        l_child[u].swap(l_child[v]);
        r_child[u].swap(r_child[v]);
    }

    merge_update(v, u);
}

void invis_traverse(int tree, int u = 1) {
    cout << tree << " " << lo[tree][u] << " " << hi[tree][u] << ": " << segtree[tree][u].ans << " " << segtree[tree][u].lazy_set << " " << segtree[tree][u].lazy_add << endl;
    
    if (l_child[tree][u] == 0) return;

    invis_traverse(tree, l_child[tree][u]);
    invis_traverse(tree, r_child[tree][u]);
}

void dfs(int u) {
    for (int v : children[u]) {
        dfs(v);
        merge(u, v);
    }

    if (val[u] == 0) return;
    lint take = query(u, t[u]) + val[u];
    set_update(u, t[u], walk(u, t[u], MAX_K, take) - 1, take);
}

int main() {
    // freopen("tree.in", "r", stdin);
    // freopen("tree.out", "w", stdout);

    cin >> n >> m >> k;
    for (int i = 2; i <= n; i++) {
        int p; cin >> p;
        children[p].push_back(i);
    }
    for (int i = 1; i <= m; i++) {
        int u; cin >> u;
        cin >> t[u] >> val[u]; 
    }

    init();
    dfs(1);

    cout << query(1, MAX_K) << '\n';
}


# Verdict Execution time Memory Grader output
1 Correct 4 ms 15964 KB Output is correct
2 Correct 4 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 4 ms 15964 KB Output is correct
8 Correct 4 ms 15960 KB Output is correct
9 Correct 4 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 532 ms 174140 KB Output is correct
2 Correct 232 ms 96804 KB Output is correct
3 Correct 1668 ms 454788 KB Output is correct
4 Correct 704 ms 259996 KB Output is correct
5 Correct 791 ms 264064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16216 KB Output is correct
2 Correct 5 ms 16220 KB Output is correct
3 Correct 5 ms 16416 KB Output is correct
4 Correct 178 ms 53100 KB Output is correct
5 Correct 155 ms 53416 KB Output is correct
6 Correct 235 ms 52212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 151712 KB Output is correct
2 Correct 494 ms 151128 KB Output is correct
3 Correct 290 ms 84904 KB Output is correct
4 Correct 591 ms 252104 KB Output is correct
5 Correct 162 ms 42680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15964 KB Output is correct
2 Correct 4 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 4 ms 15964 KB Output is correct
8 Correct 4 ms 15960 KB Output is correct
9 Correct 4 ms 15964 KB Output is correct
10 Correct 520 ms 156536 KB Output is correct
11 Correct 495 ms 153084 KB Output is correct
12 Correct 295 ms 86364 KB Output is correct
13 Correct 586 ms 253132 KB Output is correct
14 Correct 154 ms 44412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24168 KB Output is correct
2 Correct 93 ms 40264 KB Output is correct
3 Correct 101 ms 40336 KB Output is correct
4 Correct 121 ms 41504 KB Output is correct
5 Correct 53 ms 38416 KB Output is correct
6 Correct 95 ms 39572 KB Output is correct
7 Correct 109 ms 41104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15964 KB Output is correct
2 Correct 4 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 4 ms 15964 KB Output is correct
8 Correct 4 ms 15960 KB Output is correct
9 Correct 4 ms 15964 KB Output is correct
10 Correct 5 ms 16216 KB Output is correct
11 Correct 5 ms 16220 KB Output is correct
12 Correct 5 ms 16416 KB Output is correct
13 Correct 178 ms 53100 KB Output is correct
14 Correct 155 ms 53416 KB Output is correct
15 Correct 235 ms 52212 KB Output is correct
16 Correct 520 ms 156536 KB Output is correct
17 Correct 495 ms 153084 KB Output is correct
18 Correct 295 ms 86364 KB Output is correct
19 Correct 586 ms 253132 KB Output is correct
20 Correct 154 ms 44412 KB Output is correct
21 Correct 181 ms 64784 KB Output is correct
22 Correct 589 ms 168648 KB Output is correct
23 Correct 903 ms 254960 KB Output is correct
24 Correct 1547 ms 413784 KB Output is correct
25 Correct 722 ms 262308 KB Output is correct
26 Correct 641 ms 188828 KB Output is correct
27 Correct 408 ms 104308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15964 KB Output is correct
2 Correct 4 ms 15964 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 4 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 4 ms 15964 KB Output is correct
8 Correct 4 ms 15960 KB Output is correct
9 Correct 4 ms 15964 KB Output is correct
10 Correct 532 ms 174140 KB Output is correct
11 Correct 232 ms 96804 KB Output is correct
12 Correct 1668 ms 454788 KB Output is correct
13 Correct 704 ms 259996 KB Output is correct
14 Correct 791 ms 264064 KB Output is correct
15 Correct 5 ms 16216 KB Output is correct
16 Correct 5 ms 16220 KB Output is correct
17 Correct 5 ms 16416 KB Output is correct
18 Correct 178 ms 53100 KB Output is correct
19 Correct 155 ms 53416 KB Output is correct
20 Correct 235 ms 52212 KB Output is correct
21 Correct 463 ms 151712 KB Output is correct
22 Correct 494 ms 151128 KB Output is correct
23 Correct 290 ms 84904 KB Output is correct
24 Correct 591 ms 252104 KB Output is correct
25 Correct 162 ms 42680 KB Output is correct
26 Correct 520 ms 156536 KB Output is correct
27 Correct 495 ms 153084 KB Output is correct
28 Correct 295 ms 86364 KB Output is correct
29 Correct 586 ms 253132 KB Output is correct
30 Correct 154 ms 44412 KB Output is correct
31 Correct 27 ms 24168 KB Output is correct
32 Correct 93 ms 40264 KB Output is correct
33 Correct 101 ms 40336 KB Output is correct
34 Correct 121 ms 41504 KB Output is correct
35 Correct 53 ms 38416 KB Output is correct
36 Correct 95 ms 39572 KB Output is correct
37 Correct 109 ms 41104 KB Output is correct
38 Correct 181 ms 64784 KB Output is correct
39 Correct 589 ms 168648 KB Output is correct
40 Correct 903 ms 254960 KB Output is correct
41 Correct 1547 ms 413784 KB Output is correct
42 Correct 722 ms 262308 KB Output is correct
43 Correct 641 ms 188828 KB Output is correct
44 Correct 408 ms 104308 KB Output is correct
45 Correct 182 ms 64992 KB Output is correct
46 Correct 572 ms 169068 KB Output is correct
47 Correct 917 ms 254180 KB Output is correct
48 Correct 1500 ms 418480 KB Output is correct
49 Correct 759 ms 262212 KB Output is correct
50 Correct 622 ms 189696 KB Output is correct
51 Correct 416 ms 104424 KB Output is correct