제출 #1264500

#제출 시각아이디문제언어결과실행 시간메모리
1264500icebearMagic Tree (CEOI19_magictree)C++20
100 / 100
85 ms30024 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"

const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int n, k, q;
vector<int> G[N];
int d[N], w[N];

map<int, ll> dfs(int u, int par) {
    map<int, ll> res;
    for(int v : G[u]) if (v != par) {
        auto tmp = dfs(v, u);
        if (tmp.size() > res.size()) swap(res, tmp);
        for(auto &x : tmp) res[x.fi] += x.se;
    }

    if (d[u] > 0) {
        res[d[u]] += w[u];
        auto it = res.upper_bound(d[u]);
        while(it != res.end()) {
            if (it->se > w[u]) {
                it->se -= w[u];
                break;
            } else {
                w[u] -= it->se;
                it = res.erase(it);
            }
        }
    }
    return res;
}

void init(void) {
    cin >> n >> q >> k;
    FOR(i, 2, n) {
        int p; cin >> p;
        G[p].pb(i);
        G[i].pb(p);
    }
    FOR(i, 1, q) {
        int u;
        cin >> u >> d[u] >> w[u];
    }
}

void process(void) {
    auto res = dfs(1, -1);
    ll ans = 0;
    for(auto &x : res) ans += x.se;
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

magictree.cpp: In function 'int main()':
magictree.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen(task".out", "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...
#Verdict Execution timeMemoryGrader output
Fetching results...