QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#678121#7578. Salty FishfryanAC ✓615ms103896kbC++202.7kb2024-10-26 14:04:372024-10-26 14:04:41

Judging History

This is the latest submission verdict.

  • [2024-10-26 14:04:41]
  • Judged
  • Verdict: AC
  • Time: 615ms
  • Memory: 103896kb
  • [2024-10-26 14:04:37]
  • Submitted

answer

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

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) {
	if (sz(a) < sz(b)) a.swap(b);
	for (auto [x,y] : b) {
		a[x] += y;
	}
}

int cnt = 0;

void dfs(int v) {
	dp[v][dep[v]] = a[v];
	for (int u : adj[v]) {
		dfs(u);
		merge(dp[v],dp[u]);
	}
	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: 0ms
memory: 24508kb

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: 615ms
memory: 103896kb

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