#P16435. [APIO 2026 中国赛区] 集宝

[APIO 2026 中国赛区] 集宝

Background

When submitting, please choose a language standard higher than C++17, and do not include the header file gems.h.

Problem Description

The 1000th Gem Collecting Contest has begun.

The audience is tired of gem collecting problems on linear sequences, so this contest has a clear innovation compared to previous ones: contestants now need to collect gems on an unrooted tree with nn nodes.

There are mm gems on the tree. The ii-th gem (1≤i≤m1 \le i \le m) has a pair of parameters (ai,di)(a_i, d_i), meaning that when a contestant is currently at a node whose simple path to node aia_i contains at most did_i edges, they may choose to collect this gem immediately (of course, they may also choose not to collect it).

At the same time, many contestants have signed up, with a total of qq contestants. For each contestant, they are assigned a starting node xx and a gem interval [l,r][l, r]. They need to complete the following task: starting from node xx, repeatedly choose an adjacent edge of the current node and move across it, and collect gems from the ll-th to the rr-th in order, i.e., collect all r−l+1r - l + 1 gems in the order l,l+1,…,rl, l + 1, \dots, r.

Since each contestant encounters more or less route planning difficulties during the contest, the organizer has found you to provide a reasonable scoring method. For each contestant, compute the minimum total number of edges they need to traverse to complete the collection task.

Implementation Details

You do not need to, and should not, implement the main function.

You must ensure that the submitted program includes the header file gems.h, i.e., add the following code at the beginning of the program:

#include "gems.h"

You need to implement the following two functions in the submission source file gems.cpp:

void gems(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> d);
  • c,n,mc, n, m represent the test point ID, the number of nodes in the tree, and the number of gems, respectively. c=0c = 0 means this test point is the sample.
  • For 0≤i<n−10 \le i < n - 1, ui,viu_i, v_i represent an edge of the tree.
  • For 0≤i<m0 \le i < m, ai,dia_i, d_i represent the two parameters of the (i+1)(i + 1)-th gem.
  • For each test point, this function will be called by the interaction library exactly once, and before any query function calls.
long long query(int x, int l, int r);
  • x,l,rx, l, r represent a contestant’s starting node and gem interval.
  • This function should return the minimum total number of edges the contestant needs to traverse when starting from node xx and collecting gems from the ll-th to the rr-th in order.
  • For each test point, this function will be called by the interaction library exactly qq times.

Testing Program Method

You can compile an executable in this problem directory using the following command:

g++ grader.cpp gems.cpp -o gems -O2 -std=c++14 -static

Input Format

For the compiled executable:

  • The executable will read data from standard input in the following format:
    • The first line contains three non-negative integers c,n,mc, n, m.
    • Line (1+i)(1 + i) (1≤i≤n−11 \le i \le n - 1) contains two positive integers ui,viu_i, v_i.
    • Line (n+1)(n + 1) contains mm positive integers a1,a2,…,ama_1, a_2, \dots, a_m.
    • Line (n+2)(n + 2) contains mm non-negative integers d1,d2,…,dmd_1, d_2, \dots, d_m.
    • Line (n+3)(n + 3) contains one positive integer qq.
    • Line (n+3+i)(n + 3 + i) (1≤i≤q1 \le i \le q) contains three positive integers x,l,rx, l, r.

Output Format

The executable will output data to standard output in the following format:

  • Output a total of qq lines, each containing one non-negative integer, which is the return value of the query function.
0 5 4
1 2
1 3
2 4
2 5
4 1 5 3
1 0 1 2
2
2 2 4
4 1 2
2
2

Hint

Sample 1 Explanation

For the first contestant, they need to start from node 22 and collect gems 2∼42 \sim 4. One possible collection path is 2→1→2→22 \to 1 \to 2 \to 2, passing through 22 edges in total.

For the second contestant, they need to start from node 44 and collect gems 1∼21 \sim 2. One possible collection path is 4→2→14 \to 2 \to 1, passing through 22 edges in total.

Constraints

For all testdata, we have:

  • $2 \le n \le 3 \times 10^5, 1 \le m \le 3 \times 10^5$.
  • For all 1≤i≤n−11 \le i \le n - 1, 1≤ui,vi≤n1 \le u_i, v_i \le n, and all edges form a tree.
  • For all 1≤i≤m1 \le i \le m, 1≤ai≤n1 \le a_i \le n and 0≤di≤n0 \le d_i \le n.
  • 1≤q≤5×1051 \le q \le 5 \times 10^5.
  • 1≤x≤n,1≤l≤r≤m1 \le x \le n, 1 \le l \le r \le m.

::cute-table{tuack} | Test Point ID | n,m≤n, m \le | q≤q \le | Special Property | |:---:|:---:|:---:|:---:| | 1∼31 \sim 3 | 10210^2 | 10210^2 | None | | 4∼74 \sim 7 | 10310^3 | 10310^3 | ^ | | 8∼108 \sim 10 | ^ | 3×1053 \times 10^5 | ^ | | 11,1211, 12 | 3×1053 \times 10^5 | 5×1055 \times 10^5 | A | | 13,1413, 14 | ^ | ^ | B | | 15∼1715 \sim 17 | ^ | ^ | C | | 18∼2118 \sim 21 | 10510^5 | 3×1053 \times 10^5 | None | | 22∼2522 \sim 25 | 3×1053 \times 10^5 | 5×1055 \times 10^5 | ^ |

  • Special Property A: The tree is a chain, i.e., there is no node with degree greater than 22.
  • Special Property B: For all 1≤i≤m1 \le i \le m, di≥n/2d_i \ge n/2.
  • Special Property C: l=1l = 1.

Translated by ChatGPT 5