QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#677959#7578. Salty FishfryanTL 0ms33976kbC++172.5kb2024-10-26 13:44:532024-10-26 13:44:55

Judging History

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

  • [2024-10-26 13:44:55]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:33976kb
  • [2024-10-26 13:44:53]
  • 提交

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];
	dp[v][dep[v]] = a[v];
	for (int u : adj[v]) {
		dfs(u);
		merge(dp[u],dp[v],dp[v]);
	}
/*	cout<<v<<": ";
	for (auto [x,y] : dp[v]) {
		cout<<x<<","<<y<<"  ";
	}
	cout<<"\n";
	*/
	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();
	}
	
	/*
	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();
			cout<<"# "<<a<<" "<<b<<"\n";
			cout<<dx<<" "<<y<<"\n\n";
			if (!b) {
				cam[v].pop_back();
				if (!sz(cam[v])) {
					dp[v].insert({x,y});
				}
				continue;
			}
			if (dx > dep[v]+a) {
				break;
//				cam[v].pop_back();
				continue;
			}
			if (b > y) {
				b -= y;
				cnt += y;
				break;
			} else {
				y -= b;
				cnt += b;
				b = 0;
				if (!sz(cam[v])) {
					dp[v].insert({x,y});
				}
			}
		}
		if (!sz(cam[v])) break;
	}
	*/
/*	cout<<"\n";
	for (auto [x,y] : dp[v]) {
		cout<<x<<","<<y<<"  ";
	}
	cout<<"\n\n";
*///	cout<<"\n------\n";
}

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<<"\n";
}

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: 33976kb

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:

20180
-9704
-41370
-77143
-108615
-133407
-159426
-193204
-227152
-263293
-291878
-316526
-351412
-385467
-415745
-447780
-477504
-509953
-535500
-567376
-589279
-631727
-660672
-688644
-729778
-750454
-783532
-811211
-844027
-873579
-908442
-933914
-958875
-995770
-1029757
-1067076
-1092330
-112390...

result: