QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#853447#7578. Salty FishliyujiaAC ✓1434ms113312kbC++141.0kb2025-01-11 17:02:322025-01-11 17:02:40

Judging History

你现在查看的是最新测评结果

  • [2025-01-11 17:02:40]
  • 评测
  • 测评结果:AC
  • 用时:1434ms
  • 内存:113312kb
  • [2025-01-11 17:02:32]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 500005;
map <int, int> f[N];
vector <int> e[N];
vector <pair <int, int> > h[N];
int n, m, T, a[N], dep[N];
void dfs(int x){
	for(int i: e[x]){
		dfs(i);
		if(f[i].size() > f[x].size()) swap(f[i], f[x]);
		for(auto j: f[i]) f[x][j.first] += j.second;
	} f[x][dep[x]] += a[x];
	for(auto i: h[x]) while(i.second){
		auto t = f[x].upper_bound(dep[x] + i.first);
		if(t == f[x].begin()) break; t--;
		int h = min(t->second, i.second);
		i.second -= h, t->second -= h;
		if(!t->second) f[x].erase(t);
	}
}
signed main(){
	cin >> T;
	while(T--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++) e[i].clear(), f[i].clear(), h[i].clear();
		for(int i = 2, x; i <= n; i++) cin >> x, dep[i] = dep[x] + 1, e[x].push_back(i);
		for(int i = 1; i <= n; i++) cin >> a[i];
		for(int i = 1, x, k, c; i <= m; i++) cin >> x >> k >> c, h[x].push_back({k, c});
		dfs(1); int ans = 0;
		for(auto i: f[1]) ans += i.second;
		cout << ans << '\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 54508kb

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: 1434ms
memory: 113312kb

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