QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#678023#7578. Salty FishfryanTL 3ms23832kbC++202.8kb2024-10-26 13:52:142024-10-26 13:53:09

Judging History

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

  • [2024-10-26 13:53:09]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:23832kb
  • [2024-10-26 13:52:14]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

const int BUF_SZ = 1 << 15;
inline namespace Input {
	char buf[BUF_SZ];int pos;int len;
	char next_char() {
	if (pos == len) {pos = 0;
	len = (int)fread(buf, 1, BUF_SZ, stdin);
	if (!len) { return EOF; }}
	return buf[pos++];}
	int read_int() {
	int x;char ch;int sgn = 1;
	while (!isdigit(ch = next_char())) {
	if (ch == '-') { sgn *= -1; }}
	x = ch - '0';
	while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
	return x * sgn;}
}
inline namespace Output {
	char buf[BUF_SZ];int pos;
	void flush_out() {fwrite(buf, 1, pos, stdout);pos = 0;}
	void write_char(char c) {
	if (pos == BUF_SZ) { flush_out(); }
	buf[pos++] = c;}
	void write_int(int x) {
	static char num_buf[100];
	if (x < 0) {write_char('-');x *= -1;}
	int len = 0;
	for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); }
	write_char((char)('0' + x));
	while (len) { write_char(num_buf[--len]); }
	write_char('\n');}
	void init_output() { assert(atexit(flush_out) == 0); }
}

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() {
	n = read_int();
	m = read_int();
	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++) {
		f[i] = read_int(); --f[i];
		adj[f[i]].push_back(i);
		dep[i] = dep[f[i]]+1;
	}
	for (int i=0; i<n; i++) a[i] = read_int();
	for (int i=0; i<m; i++) {
		int x = read_int();
		int k = read_int();
		int c = read_int();
		--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];
	}
	write_int(su-cnt);
}

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	init_output();
	
	int t = read_int();
	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: 3ms
memory: 23832kb

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:


result: