#P10783. 【MX-J1-T3】『FLA - III』Anxiety

    ID: 12120 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP图论O2优化枚举树形 DP树论梦熊比赛

【MX-J1-T3】『FLA - III』Anxiety

Background

Original link: https://oier.team/problems/J1C


I came. I saw. I had anxiety. I left.

Problem Description

Given a binary tree with 2n12^n-1 nodes, the weight of node ii is wiw_i, and node 11 is the root. For every non-root node ii, there is an undirected edge connecting node ii and node i2\left\lfloor \frac{i}{2} \right\rfloor. Note that X\left\lfloor X \right\rfloor denotes the greatest integer not exceeding XX.

Define the distance between nodes u,vu,v as the minimum number of edges that must be traversed to go from node uu to node vv. Given mm queries, the ii-th query provides three positive integers xi,yi,kix_i,y_i,k_i. You need to output the sum of weights of all nodes in the tree whose distances to both nodes xix_i and yiy_i are no more than kik_i.

Input Format

The first line contains two positive integers n,mn,m.

The second line contains 2n12^n-1 positive integers; the ii-th integer is wiw_i.

The next mm lines each contain three positive integers xi,yi,kix_i,y_i,k_i on the ii-th line.

Output Format

Output mm lines, each containing one integer. The integer on the ii-th line is the answer to the ii-th query.

3 3
1 1 1 1 1 1 1
3 4 2
5 4 6
3 2 2
2
7
3
4 5
3 4 10 7 1 6 10 6 16 5 3 16 6 2 9
1 4 6
4 2 1
1 14 5
6 13 3
11 15 2

104
11
74
51
0

Hint

"Sample Explanation #1"

example

For the first query, the nodes that satisfy the condition are 1,21,2, and the sum of weights is 22.

For the second query, the nodes that satisfy the condition are 1,2,3,4,5,6,71,2,3,4,5,6,7, and the sum of weights is 77.

For the third query, the nodes that satisfy the condition are 1,2,31,2,3, and the sum of weights is 33.

"Constraints"

Test Point ID nn \leq mm \leq kik_i \leq wiw_i \leq
11 22 55 1010
232 \sim 3 1010 10001000
454 \sim 5 1818 2×1052 \times 10^5 55 10910^9
676 \sim 7 10910^9 11
8108 \sim 10 10910^9

For 100%100\% of the testdata, 2n182 \leq n \leq 18, 1m2×1051 \leq m \leq 2 \times 10^5, 1xi,yi2n11 \leq x_i,y_i \leq 2^n-1, 1ki1091 \leq k_i \leq 10^9, 1wi1091 \leq w_i \leq 10^9, and xiyix_i \neq y_i. Node indices are integers from 11 to 2n12^n-1.

Translated by ChatGPT 5