Submission #1095603

#TimeUsernameProblemLanguageResultExecution timeMemory
1095603cpptowinMagic Tree (CEOI19_magictree)C++17
100 / 100
133 ms101716 KiB
#include <bits/stdc++.h> #define fo(i, d, c) for (int i = d; i <= c; i++) #define fod(i, c, d) for (int i = c; i >= d; i--) #define maxn 1000010 #define N 1010 #define fi first #define se second #define pb emplace_back #define en cout << "\n"; #define int long long #define inf (int)1e18 #define bitcout(x) __builtin_popcountll(x) #define pii pair<int, int> #define vii vector<pii> #define lb(x) x & -x #define bit(i, j) ((i >> j) & 1) #define offbit(i, j) (i ^ (1LL << j)) #define onbit(i, j) (i | (1LL << j)) #define vi vector<int> #define all(x) x.begin(), x.end() #define ss(x) (int)x.size() template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } using namespace std; const int nsqrt = 450; const int mod = 1e9 + 7; void add(int &x, int k) { x += k; if (x >= mod) x -= mod; } map<int, int> f[maxn]; int d[maxn], w[maxn]; vi ke[maxn]; int n, m, k; void dfs(int u, int par = 0) { for (int v : ke[u]) if (v != par) { dfs(v, u); if (ss(f[u]) < ss(f[v])) swap(f[u], f[v]); for (auto it : f[v]) f[u][it.fi] += it.se; } if (d[u] and w[u]) { int sum = 0; while (1) { auto ub = f[u].upper_bound(d[u]); if (ub == f[u].end()) break; if (sum + ub->se < w[u]) { sum += ub->se; f[u].erase(ub); } else { ub->se += sum - w[u]; break; } } f[u][d[u]] += w[u]; } } main() { #define name "TASK" if (fopen(name ".inp", "r")) { freopen(name ".inp", "r", stdin); freopen(name ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; fo(i, 1, n) ke[i].clear(); fo(i, 2, n) { int x; cin >> x; ke[x].pb(i); } fo(i, 1, m) { int u; cin >> u >> d[u] >> w[u]; } dfs(1); int ans = 0; for (auto it : f[1]) ans += it.se; cout << ans; en; }

Compilation message (stderr)

magictree.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   88 | main()
      | ^~~~
magictree.cpp: In function 'int main()':
magictree.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen(name ".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...