QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#677994 | #7578. Salty Fish | fryan | TL | 4ms | 21252kb | C++20 | 1.7kb | 2024-10-26 13:49:08 | 2024-10-26 13:49:08 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
const int mxn = 3e5+5;
int n,m,f[mxn],a[mxn],dep[mxn];
vector<int> adj[mxn];
vector<array<int,2>> cam[mxn];
map<int,int> dp[mxn]; //y = how many exist at depth[x] in subtree
void merge(map<int,int> &a, map<int,int> &b, map<int,int> &c) {
if (sz(a) < sz(b)) swap(a,b);
for (auto [x,y] : b) {
if (!a.count(x)) a[x] = 0;
a[x] += y;
}
c = a;
}
int cnt = 0;
void dfs(int v) {
dp[v][dep[v]] = a[v];
for (int u : adj[v]) {
dfs(u);
merge(dp[u],dp[v],dp[v]);
}
while (sz(cam[v])) {
auto &[a,b] = cam[v].back();
while (b) {
auto it = dp[v].upper_bound(dep[v]+a);
if (it==dp[v].begin()) {
break;
}
--it;
auto [x,y] = *it;
dp[v].erase(it);
if (b > y) {
b -= y;
cnt += y;
continue;
} else {
y -= b;
cnt += b;
b = 0;
dp[v].insert({x,y});
}
}
cam[v].pop_back();
}
}
void solve() {
cin>>n>>m; dep[0] = 0;
for (int i=0; i<n; i++) adj[i].clear();
for (int i=0; i<n; i++) cam[i].clear();
for (int i=0; i<n; i++) dp[i].clear();
for (int i=1; i<n; i++) {
cin>>f[i]; --f[i];
adj[f[i]].push_back(i);
dep[i] = dep[f[i]]+1;
}
for (int i=0; i<n; i++) cin>>a[i];
for (int i=0; i<m; i++) {
int x,k,c; cin>>x>>k>>c; --x;
cam[x].push_back({k,c});
}
for (int i=0; i<n; i++) {
sort(all(cam[i]));
}
cnt = 0;
dfs(0);
int su = 0;
for (int i=0; i<n; i++) {
su += a[i];
}
cout<<su-cnt<<"\n";
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin>>t;
for (int i=0; i<t; i++) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 21252kb
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: -100
Time Limit Exceeded
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...