QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226864 | #7578. Salty Fish | 8BQube# | AC ✓ | 714ms | 93448kb | C++20 | 1.8kb | 2023-10-26 17:26:48 | 2023-10-26 17:26:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define pb push_back
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
const int N = 3e5 + 5;
ll a[N], d[N], h[N];
map<ll, ll> mp[N];
vector<int> v[N];
vector<pll> con[N];
void szdfs(int now) {
h[now] = 0;
for (int x : v[now])
d[x] = d[now] + 1, szdfs(x), h[now] = max(h[now], h[x] + 1);
}
void dfs(int now) {
map<ll, ll> res;
for (int x : v[now]) {
dfs(x);
if (SZ(mp[x]) > SZ(res))
mp[x].swap(res);
for (auto [i, j] : mp[x])
res[i] += j;
mp[x].clear();
}
res[d[now]] = res[d[now] + 1] + a[now];
res[d[now] + 1] = -a[now];
for (auto [k, c] : con[now]) {
// d[now] -= c, d[now] + min(k, h[now]) + 1 += c
k = min(k, h[now]);
res[d[now]] -= c;
auto [it, succ] = res.insert({d[now] + k + 1, c});
if (!succ)
it->Y += c;
while (it->X != d[now] && it->Y >= 0) {
prev(it)->Y += it->Y;
it = prev(res.erase(it));
}
}
mp[now].swap(res);
}
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
mp[i].clear(), v[i].clear(), con[i].clear();
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
v[p].pb(i);
}
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++) {
int x, k, c;
cin >> x >> k >> c;
con[x].pb({k, c});
}
d[1] = 0;
szdfs(1);
dfs(1);
assert(SZ(mp[1]));
cout << mp[1].begin()->Y << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 37780kb
input:
1 6 3 1 1 2 2 3 2 5 4 3 3 2 2 1 3 3 1 7 1 2 4
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 714ms
memory: 93448kb
input:
1003 100 100 1 2 3 4 5 6 7 8 9 10 6 1 2 4 1 11 17 14 17 2 13 8 8 5 11 7 18 6 2 10 23 11 13 3 9 1 33 20 3 9 32 35 11 41 42 29 33 45 21 35 9 36 12 54 19 24 57 31 32 5 3 10 46 15 46 48 20 44 5 41 67 7 18 30 27 6 29 69 57 75 62 74 18 64 17 21 38 60 79 69 54 90 83 83 31 96 31 93 53 152 498 653 559 287 38...
output:
20180 17083 14650 19924 15814 20189 20894 18175 18478 13758 20217 23680 15562 12882 18046 18132 20548 17000 23734 18740 24814 16728 20979 19727 16450 21717 15739 22081 17803 23024 14820 21503 23497 15804 18097 17197 21236 21286 14250 11632 18335 12317 16313 22840 18583 15245 19331 25978 22388 17091 ...
result:
ok 1003 numbers