답안 #591421

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
591421 2022-07-07T12:18:00 Z alextodoran Magic Tree (CEOI19_magictree) C++17
71 / 100
421 ms 69068 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100000;
const int K_MAX = 100000;
const int BITS = 17;

int N, M, K;
int parent[N_MAX + 2];
vector <int> sons[N_MAX + 2];

int ripeTime[N_MAX + 2];
ll juice[N_MAX + 2];

struct Fenwick {
    map <int, ll> mp;
    multiset <pair <int, ll>> upds;
    void update (int pos, ll val) {
        upds.insert(make_pair(pos, val));
        for (int i = pos; i <= K; i += i & -i) {
            mp[i] += val;
        }
    }
    ll query (int pos) {
        ll val = 0;
        for (int i = pos; i >= 1; i -= i & -i) {
            if (mp.find(i) != mp.end()) {
                val += mp[i];
            }
        }
        return val;
    }
    void maxify (int pos, ll val) {
        update(pos, val);
        multiset <pair <int, ll>> :: iterator it = upds.lower_bound(make_pair(pos + 1, 0));
        while (val > 0 && it != upds.end()) {
            int itpos = it->first;
            ll itval = it->second;
            if (itval <= val) {
                update(itpos, -itval);
                upds.erase(upds.find(make_pair(itpos, -itval)));
                val -= itval;
                it++;
                upds.erase(upds.find(make_pair(itpos, itval)));
            } else {
                update(itpos, -val);
                val = 0;
            }
        }
    }
};

Fenwick Fen[N_MAX + 2];

void dfs (int u) {
    int heavy = -1;
    for (int v : sons[u]) {
        dfs(v);
        if (heavy == -1 || (int) Fen[heavy].upds.size() < (int) Fen[v].upds.size()) {
            heavy = v;
        }
    }
    if (heavy != -1) {
        swap(Fen[u], Fen[heavy]);
    }
    for (int v : sons[u]) {
        if (v != heavy) {
            for (pair <int, ll> upd : Fen[v].upds) {
                Fen[u].update(upd.first, upd.second);
            }
            Fen[v].upds.clear();
            Fen[v].mp.clear();
        }
    }
    if (juice[u] != 0) {
        Fen[u].maxify(ripeTime[u], juice[u]);
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N >> M >> K;
    for (int u = 2; u <= N; u++) {
        cin >> parent[u];
        sons[parent[u]].push_back(u);
    }
    for (int i = 1; i <= M; i++) {
        int u;
        cin >> u;
        cin >> ripeTime[u] >> juice[u];
    }

    dfs(1);
    cout << Fen[1].query(K) << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12000 KB Output is correct
2 Correct 6 ms 12080 KB Output is correct
3 Correct 6 ms 12068 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 21616 KB Output is correct
2 Correct 100 ms 28100 KB Output is correct
3 Correct 421 ms 31564 KB Output is correct
4 Correct 342 ms 69024 KB Output is correct
5 Correct 253 ms 26444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 6 ms 12372 KB Output is correct
4 Correct 157 ms 40404 KB Output is correct
5 Correct 153 ms 46668 KB Output is correct
6 Correct 270 ms 40416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 139 ms 21924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12000 KB Output is correct
2 Correct 6 ms 12080 KB Output is correct
3 Correct 6 ms 12068 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 146 ms 19592 KB Output is correct
11 Correct 121 ms 20144 KB Output is correct
12 Correct 94 ms 25580 KB Output is correct
13 Correct 80 ms 35408 KB Output is correct
14 Correct 91 ms 35832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 13012 KB Output is correct
2 Correct 48 ms 15436 KB Output is correct
3 Correct 49 ms 15504 KB Output is correct
4 Correct 39 ms 15504 KB Output is correct
5 Correct 16 ms 14616 KB Output is correct
6 Correct 36 ms 19120 KB Output is correct
7 Correct 41 ms 23856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12000 KB Output is correct
2 Correct 6 ms 12080 KB Output is correct
3 Correct 6 ms 12068 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 7 ms 12244 KB Output is correct
11 Correct 6 ms 12244 KB Output is correct
12 Correct 6 ms 12372 KB Output is correct
13 Correct 157 ms 40404 KB Output is correct
14 Correct 153 ms 46668 KB Output is correct
15 Correct 270 ms 40416 KB Output is correct
16 Correct 146 ms 19592 KB Output is correct
17 Correct 121 ms 20144 KB Output is correct
18 Correct 94 ms 25580 KB Output is correct
19 Correct 80 ms 35408 KB Output is correct
20 Correct 91 ms 35832 KB Output is correct
21 Correct 48 ms 14064 KB Output is correct
22 Correct 152 ms 18432 KB Output is correct
23 Correct 222 ms 22860 KB Output is correct
24 Correct 417 ms 26540 KB Output is correct
25 Correct 286 ms 69068 KB Output is correct
26 Correct 308 ms 31492 KB Output is correct
27 Correct 267 ms 32588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12000 KB Output is correct
2 Correct 6 ms 12080 KB Output is correct
3 Correct 6 ms 12068 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 165 ms 21616 KB Output is correct
11 Correct 100 ms 28100 KB Output is correct
12 Correct 421 ms 31564 KB Output is correct
13 Correct 342 ms 69024 KB Output is correct
14 Correct 253 ms 26444 KB Output is correct
15 Correct 7 ms 12244 KB Output is correct
16 Correct 6 ms 12244 KB Output is correct
17 Correct 6 ms 12372 KB Output is correct
18 Correct 157 ms 40404 KB Output is correct
19 Correct 153 ms 46668 KB Output is correct
20 Correct 270 ms 40416 KB Output is correct
21 Incorrect 139 ms 21924 KB Output isn't correct
22 Halted 0 ms 0 KB -