QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#677006#7578. Salty FishfryanTL 0ms33220kbC++171.7kb2024-10-26 08:28:352024-10-26 08:28:36

Judging History

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

  • [2024-10-26 08:28:36]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:33220kb
  • [2024-10-26 08:28:35]
  • 提交

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(dp[v]) && sz(cam[v])) {
		auto [x,y] = *dp[v].begin();
		int dx = -x;
		dp[v].erase(dp[v].begin());
		while (sz(cam[v])) {
			auto &[a,b] = cam[v].back();
			if (!b) {
				cam[v].pop_back();
				continue;
			}
			if (dx > dep[v]+a) {
				cam[v].pop_back();
				continue;
			}
			if (b > y) {
				b -= y;
				cnt += y;
				break;
			} else {
				y -= b;
				cnt += b;
				b = 0;
				dp[v].insert({x,y});
			}
		}
		if (!sz(cam[v])) break;
	}
}

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]));
	}
	dfs(0);
	int su = 0;
	for (int i=0; i<n; i++) {
		su += a[i];
	}
	cout<<su-cnt;
}

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	
	int t; cin>>t;
	while (t--) solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 33220kb

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...

output:

265482203-15233-45346-72574-90178-110088-138966-166406-193185-215127-235451-263292-292815-315909-341248-366787-386237-407001-433069-452167-485655-511155-527113-564881-578684-606231-627907-655120-680485-706355-729155-751148-782580-801958-836060-855642-882151-909604-934027-946599-973418-994504-1012964...

result: