#P15061. 琥峪枫

    ID: 16985 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>交互题Special JudgeAd-hoc

琥峪枫

Background

Do not use #include "tree.h"\texttt{\#include "tree.h"}.

You need to add the following content at the top of your file, and submit using C++17 or a higher language standard:

long long ask(int u, int d);

Problem Description

This is an interactive problem.

Given a positive integer hh. Let n=2h−1n = 2^h - 1.

The interactive library hides a permutation pp of 1∼n1 \sim n and a sequence ff of length nn, where each element is a positive integer not greater than 10910^9.

Now there is a full binary tree GG of depth hh with nn nodes, and the root is node 11. Also, for any node uu satisfying 2≤u≤n2 \le u \le n, its parent node is ⌊u2⌋\left\lfloor\dfrac u 2\right\rfloor.

In each query, you can choose two integers u,du, d satisfying 1≤u≤n1 \le u \le n and 1≤d≤1091 \le d \le 10^9. The interactive library will return the sum of fvf_v over all nodes vv such that dis⁡(pu,v)=d\operatorname{dis}(p_u, v) = d. In particular, if there is no such node vv, the interactive library will return 00.

Here, dis⁡(u,v)\operatorname{dis}(u, v) is the number of edges on the simple path between node uu and node vv. In particular, dis⁡(u,u)=0\operatorname{dis}(u, u) = 0.

You need to determine ∑i=1nfi\sum\limits_{i=1}^n f_i using no more than 2hn2hn queries. The contestant’s score depends on the number of queries for a single testdata and, for any integer uu, the maximum number of times you query the integer u\boldsymbol u.

It is not guaranteed that the permutation pp and the sequence ff are fixed, i.e., the interactive library may be adaptive.

Implementation Details

You need to implement the following function:

long long solve(int subtask, int h);
  • subtask indicates the test point index;
  • hh is the height of the binary tree;
  • This function should return the value of ∑i=1nfi\sum\limits_{i=1}^n f_i;
  • For each test point, this function may be called multiple times by the interactive library.

You can send a query to the interactive library by calling:

long long ask(int u, int d);
  • uu is the center node of the query, and you must ensure 1≤u≤n1 \leq u \leq n;
  • dd is the distance limit, and you must ensure 1≤d≤1091 \leq d \leq 10^9;
  • This function will return the sum of ff values of nodes whose distance to node pup_u is exactly dd.

The problem guarantees that within the allowed operation limit, the interactive library will run in no more than 11 second; the memory usage of the interactive library is fixed and does not exceed 32 MiB32 \text{ MiB}.

Interaction Example

Suppose h=2h = 2, n=3n = 3, the hidden permutation is p=[2,1,3]p = [2, 1, 3], and the node weights are f=[11,45,14]f = [11, 45, 14]. The following is a valid interaction:

Contestant Program Interactive Library Explanation
Call tree(1, 2) Start testing
Call ask(1, 1) Return 1111 The only node at distance 11 from p1=2p_1 = 2 is node 11, sum is 1111
Call ask(2, 1) Return 5959 Nodes at distance 11 from p2=1p_2 = 1 are nodes 2,32, 3, sum is 45+14=5945 + 14 = 59
Call ask(3, 1) Return 1111 The only node at distance 11 from p3=3p_3 = 3 is node 11, sum is 1111
End and return 7070 Print the interaction result Interaction ends, correct result

Hint

Constraints

For all testdata, it is guaranteed that: 2≤h≤152 \leq h \leq 15, number of test cases 1≤T≤1 5001 \leq T \leq 1\,500, and across all data the sum of nn satisfies ∑n≤106\sum n \leq 10^6.

This problem has 22 test points. The score and constraints for each test point are shown below.

Test Point Index Score Special Property
11 1010 Guaranteed h=2h = 2, T=100T = 100
22 9090 No special restrictions

Scoring

This problem will first be subject to the same restrictions as usual, e.g., a compilation error will cause the whole problem to score 00 points; runtime errors, time limit exceeded, and memory limit exceeded will cause the corresponding test point to score 00 points. Contestants can only access variables or data defined by themselves and those provided by the interactive library, and the corresponding memory space. Attempts to access other memory locations may cause compilation errors or runtime errors.

In each call to solve, the number of operations used by the program, qq, must satisfy q≤2hnq \leq 2hn, otherwise it will receive 00 points.

Based on the above conditions:

  • In test point 11, the program gets full marks if and only if the return value of solve is correct.

  • In test point 22, the score is computed as follows:

    • If the return value of solve is incorrect, the score is 00.
    • If all return values of solve are correct, then each testdata in this test point is scored separately: let qq be the number of operations used, and for any integer uu, let xx be the maximum number of times the integer uu is queried. Then the program gets f(q)−g(x)f(q) - g(x) points, where f(q)f(q) is the maximum score among all satisfied conditions in the table below:
    Condition Score
    q≤2n+3q \leq 2n + 3 9090
    q≤2n+4q \leq 2n + 4 8282
    q≤2n+5q \leq 2n + 5 7676
    q≤2n+h+2q \leq 2n + h + 2 7272
    q≤2n+h+4q \leq 2n + h + 4 6969
    q≤2n+2h+2q \leq 2n + 2h + 2 6666
    q≤2n+2h+4q \leq 2n + 2h + 4 6363
    q≤3n+3q \leq 3n + 3 5959
    q≤3n+5q \leq 3n + 5 5656
    q≤3n+h+4q \leq 3n + h + 4 5353
    q≤3n+2h+4q \leq 3n + 2h + 4 5050
    q≤4n+3q \leq 4n + 3 4343
    q≤4n+5q \leq 4n + 5 4040
    q≤4n+h+4q \leq 4n + h + 4 3737
    q≤4n+2h+4q \leq 4n + 2h + 4 3434
    q≤2hnq \leq 2hn 3030

    Here, g(x)g(x) is computed as follows:

    xx g(x)g(x)
    ≤4\leq 4 00
    =5= 5 1010
    =6= 6 1515
    >6> 6 2020

Translated by ChatGPT 5