Submission #868687

#TimeUsernameProblemLanguageResultExecution timeMemory
868687fanwenMagic Tree (CEOI19_magictree)C++17
100 / 100
86 ms32900 KiB
#include <bits/stdc++.h> using namespace std; #define MASK(x) (1LL << (x)) #define BIT(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0, _n = n; i < _n; ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } const int MAX = 1e5 + 5; int N, M, K; map <int, long long> dp[MAX]; vector <int> adj[MAX]; pair <int, int> A[MAX]; void insert(int x, int d, int w) { dp[x][d] += w; auto it = dp[x].upper_bound(d); while(it != dp[x].end() && it->second <= w) { w -= it->second; it = dp[x].erase(it); } if(it != dp[x].end() && it->second >= w) { it->second -= w; } } void dfs(int u) { for (auto v : adj[u]) { dfs(v); if((int) dp[u].size() < (int) dp[v].size()) dp[u].swap(dp[v]); for (auto [x, d] : dp[v]) { dp[u][x] += d; } dp[v].clear(); } if(A[u].first != 0) insert(u, A[u].first, A[u].second); } void you_make_it(void) { cin >> N >> M >> K; FOR(i, 2, N) { int u; cin >> u; adj[u].emplace_back(i); } FOR(i, 1, M) { int u; cin >> u >> A[u].first >> A[u].second; } dfs(1); long long ans = 0; for (auto tmp : dp[1]) ans += tmp.second; cout << ans; } signed main() { #ifdef LOCAL freopen("TASK.inp", "r", stdin); freopen("TASK.out", "w", stdout); #endif file("magictree"); auto start_time = chrono::steady_clock::now(); cin.tie(0), cout.tie(0) -> sync_with_stdio(0); you_make_it(); auto end_time = chrono::steady_clock::now(); cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl; return (0 ^ 0); } // Dream it. Wish it. Do it.

Compilation message (stderr)

magictree.cpp: In function 'int main()':
magictree.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:75:5: note: in expansion of macro 'file'
   75 |     file("magictree");
      |     ^~~~
magictree.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:75:5: note: in expansion of macro 'file'
   75 |     file("magictree");
      |     ^~~~
#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...