题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie is going on a ski trip with her friends. The mountain has $N$ waypoints ($1\leq N \leq 10^5$) labeled $1, 2, \ldots, N$ in increasing order of altitude (waypoint $1$ is the bottom of the mountain).
For each waypoint $i > 1$, there is a ski run starting from waypoint $i$ and ending at waypoint $p_i$ ($1\le p_i<i$). This run has difficulty $d_i$ ($0 \leq d_i \leq 10^9$) and enjoyment $e_i$ ($0 \leq e_i \leq 10^9$).
Each of Bessie's $M$ friends ($1\leq M \leq 10^5$) will do the following: They will pick some initial waypoint $i$ to start at, and then follow the runs downward (to $p_i$, then to $p_{p_i}$, and so forth) until they get to waypoint $1$.
The enjoyment each friend gets is equal to the sum of the enjoyments of the runs they follow. Each friend also has a different skill level $s_j$ ($0 \leq s_j \leq 10^9$) and courage level $c_j$ ($0 \leq c_j \leq 10$), which limits them to selecting an initial waypoint that results in them taking at most $c_j$ runs with difficulty greater than $s_j$.
For each friend, compute the maximum enjoyment they can get.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$.Then for each $i$ from $2$ to $N$, a line follows containing three space-separated integers $p_i$, $d_i$, and $e_i$.
The next line contains $M$.
The next $M$ lines each contain two space-separated integers $s_j$ and $c_j$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $M$ lines, with the answer for each friend on a separate line.Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).
SAMPLE INPUT:
4 1 20 200 2 30 300 2 10 100 8 19 0 19 1 19 2 20 0 20 1 20 2 29 0 30 0
SAMPLE OUTPUT:
0 300 500 300 500 500 300 500
- The first friend cannot start any waypoint other than $1$, since any other waypoint would cause them to take at least one run with difficulty greater than $19$. Their total enjoyment is $0$.
- The second friend can start at waypoint $4$ and take runs down to waypoint $2$ and then $1$. Their total enjoyment is $100+200=300$. They take one run with difficulty greater than $19$.
- The third friend can start at waypoint $3$ and take runs down to waypoint $2$ and then $1$. Their total enjoyment is $300+200=500$. They take two runs with difficulty greater than $19$.
SCORING:
- Inputs 2-4: $N, M\le 1000$
- Inputs 5-7: All $c_j=0$
- Inputs 8-17: No additional constraints.
Problem credits: Brandon Wang
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 13.0k平均输出 token(含思考) ↓
- 23.9输出速度 tokens/s ↑
- 549 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 9.01Pylint 得分 ↑
- 19圈复杂度 ↓
- 52.2可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 9.7k平均输出 token(含思考) ↓
- 209.7输出速度 tokens/s ↑
- 47 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.93Pylint 得分 ↑
- 21圈复杂度 ↓
- 46.3可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 10.8k平均输出 token(含思考) ↓
- 138.1输出速度 tokens/s ↑
- 79 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 9.11Pylint 得分 ↑
- 15圈复杂度 ↓
- 60.1可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) pos = 0 n = data[pos] pos += 1 parent = [0] * (n + 1) difficulty = [0] * (n + 1) enjoyment = [0] * (n + 1) for v in range(2, n + 1): p = data[pos] d = data[pos + 1] e = data[pos + 2] pos += 3 parent[v] = p difficulty[v] = d enjoyment[v] = enjoyment[p] + e m = data[pos] pos += 1 queries_by_courage = [[] for _ in range(11)] max_courage = 0 for qid in range(m): s = data[pos] c = data[pos + 1] pos += 2 queries_by_courage[c].append((s, qid)) if c > max_courage: max_courage = c del data # tops[v] contains the largest max_courage+1 difficulties on the # path from waypoint 1 to v, in nonincreasing order. limit = max_courage + 1 tops = [()] * (n + 1) for v in range(2, n + 1): a = tops[parent[v]] d = difficulty[v] if len(a) == limit and d <= a[-1]: tops[v] = a continue b = list(a) insert_at = 0 while insert_at < len(a) and b[insert_at] >= d: insert_at += 1 b.insert(insert_at, d) if len(b) > limit: b.pop() tops[v] = tuple(b) del parent, difficulty answers = [0] * m for c in range(max_courage + 1): queries = queries_by_courage[c] if not queries: continue candidates = [] append_candidate = candidates.append # With courage c, waypoint v is usable exactly when its # (c+1)-st largest path difficulty is at most s. If the path # has at most c runs, every skill level is sufficient. for v in range(1, n + 1): tv = tops[v] required_skill = tv[c] if len(tv) > c else -1 append_candidate((required_skill, enjoyment[v])) candidates.sort() queries.sort() candidate_index = 0 best = 0 for s, qid in queries: while (candidate_index < n and candidates[candidate_index][0] <= s): value = candidates[candidate_index][1] if value > best: best = value candidate_index += 1 answers[qid] = best sys.stdout.write("\n".join(map(str, answers)) + "\n") if __name__ == "__main__": main()
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return idx = 0 N = data[idx] idx += 1 parent = [0] * (N + 1) difficulty = [0] * (N + 1) total_enjoyment = [0] * (N + 1) for i in range(2, N + 1): p = data[idx] d = data[idx + 1] e = data[idx + 2] idx += 3 parent[i] = p difficulty[i] = d total_enjoyment[i] = total_enjoyment[p] + e M = data[idx] idx += 1 queries_by_c = [[] for _ in range(11)] max_c = 0 for qi in range(M): s = data[idx] c = data[idx + 1] idx += 2 queries_by_c[c].append((s, qi)) if c > max_c: max_c = c keep = max_c + 1 # tops[i] stores the largest `keep` edge difficulties on the path from root to i. tops = [None] * (N + 1) tops[1] = [] per_c = [[] for _ in range(max_c + 1)] # Waypoint 1 is always a valid starting point with enjoyment 0. for c in range(max_c + 1): per_c[c].append((0, 0)) for i in range(2, N + 1): prev = tops[parent[i]] x = difficulty[i] new = [] inserted = False for v in prev: if not inserted and x > v: new.append(x) inserted = True new.append(v) if not inserted: new.append(x) if len(new) > keep: del new[keep:] tops[i] = new value = total_enjoyment[i] length = len(new) for c in range(length): per_c[c].append((new[c], value)) for c in range(length, max_c + 1): per_c[c].append((0, value)) answers = [0] * M for c in range(max_c + 1): arr = per_c[c] arr.sort() qs = queries_by_c[c] qs.sort() ptr = 0 best = 0 size = len(arr) for s, qi in qs: while ptr < size and arr[ptr][0] <= s: if arr[ptr][1] > best: best = arr[ptr][1] ptr += 1 answers[qi] = best sys.stdout.write("\n".join(map(str, answers))) if __name__ == "__main__": main()
import sys from bisect import bisect_right, insort def solve(): tokens = list(map(int, sys.stdin.buffer.read().split())) if not tokens: return it = iter(tokens) n = next(it) MAX_C = 10 KEEP = MAX_C + 1 # top[v] contains up to 11 largest difficulties on the path v -> 1, # stored as negative values so that they are in ascending order. top = [None] * (n + 1) top[1] = [] enjoyment = [0] * (n + 1) # threshold[c][v] is the largest skill needed to allow at most c # runs with difficulty greater than the skill. threshold = [[0] * (n + 1) for _ in range(MAX_C + 1)] for node in range(2, n + 1): parent = next(it) difficulty = next(it) run_enjoyment = next(it) enjoyment[node] = enjoyment[parent] + run_enjoyment current = top[parent].copy() neg_difficulty = -difficulty if len(current) < KEEP: insort(current, neg_difficulty) elif neg_difficulty < current[-1]: insort(current, neg_difficulty) current.pop() top[node] = current for courage, neg_value in enumerate(current): threshold[courage][node] = -neg_value del top m = next(it) grouped_queries = [[] for _ in range(MAX_C + 1)] for query_index in range(m): skill = next(it) courage = next(it) grouped_queries[courage].append((skill, query_index)) del tokens, it answers = [0] * m for courage, queries in enumerate(grouped_queries): if not queries: continue values = threshold[courage] order = sorted(range(1, n + 1), key=values.__getitem__) sorted_thresholds = [values[node] for node in order] # prefix[k] is the maximum enjoyment among the first k waypoints # in threshold order. prefix = [0] * (n + 1) best = 0 for position, node in enumerate(order, 1): if enjoyment[node] > best: best = enjoyment[node] prefix[position] = best for skill, query_index in queries: count = bisect_right(sorted_thresholds, skill) answers[query_index] = prefix[count] sys.stdout.write("\n".join(map(str, answers)) + "\n") if __name__ == "__main__": solve()