#P8262. [CTS2022] 隆

    ID: 9341 远端评测题 10000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2022交互题Special JudgeO2优化CTSC/CTS

[CTS2022] 隆

Background

This is an interactive problem.

For local testing, you need #include <long.h>. When submitting, please use the following template instead of including long.h. The method is to put this template directly into your code, and then complete the functions required by the problem.

struct sethash{
	int val;
	void encode();
	void decode();
	void operator=(int x);
};
struct infoset{
	sethash sum;
	int sz;
	int size()const{
		return sz;
	}
};
extern const infoset emptyset;
infoset merge(const infoset&a,const infoset&b);
void report(infoset p);
void init(int id,int n,int q,vector<int>dad,vector<infoset>ve);
void modify(int x,int y);
void ask(int x,int y);

The great scientist Xin plans to build the powerful AI Long to rule the world. After feeding it some Vietnamese competitive programming problems as a training set, she found that Long automatically generated a new problem and provided a solution. At the same time, Long also reduced 30%30\% of the inputs in the test set to this problem.

Xin quickly copied down the problem as follows:

Given a rooted tree, you need to support two operations:

  • Query the sum of elements on a tree path.
  • Change the parent of a node to another node.

“This problem is not that hard. Why can it solve 30%30\% of the problems in the test set?” Xin thought.

Before closing the training results page, Xin suddenly noticed that, in the problem given by Long, the cost of merging information is not O(1)O(1).

Xin fell into deep thought and realized she could not solve this problem.

To find out how hard it is, Xin presents it to you.

Problem Description

You need to maintain a rooted tree with root 11. This tree has nn nodes. Initially, for all 2in2 \le i \le n, node ii has a parent pip_i, and it is guaranteed that pi<ip_i \lt i.

At the beginning, you have nn sets {1},{2},,{n}\{1\}, \{2\}, \cdots, \{n\}. For any two sets AA and BB, if AB=A \cap B = \empty, then you can, in one operation, pay a cost of A+B|A| + |B| to obtain the set ABA \cup B.

Then there are qq operations. Each operation is one of the following two types:

  • 0 a b: Let SS be the set of nodes on the path from aa to bb in the tree. You need to represent SS as i=1kAi\cup_{i=1}^{k} A_i, where each AiA_i is a set you have already obtained, and ij,AiAj=\forall i \neq j, A_i \cap A_j = \empty. The cost of answering one query using (A1,A2,,Ak)(A_1, A_2, \cdots, A_k) is kk.
  • 1 a b: Change the parent of node aa to bb. It is guaranteed that a>1a \gt 1, and after the modification, these nn nodes still form a tree, but it is not guaranteed that a>ba \gt b. In this operation, you may generate some new sets to handle future queries.

Let the total cost consumed during the entire execution of your program be C1C_1, and the maximum cost consumed by a single operation be C2C_2. Each subtask will be scored in some way based on the values of C1C_1 and C2C_2.

Input Format

Implementation Details

The header file defines a data type infoset, used to store the sets you have obtained.

This type includes a member function size(), which indicates the size of the set. The header file also provides a constant emptyset representing the empty set.

In addition, you can call the following functions:

infoset merge(const infoset&A,const infoset&B);

  • Generate a new set ABA \cup B at a certain cost.
  • Note that you may generate the same set multiple times, and the cost must be counted multiple times accordingly.

void report(infoset A);

  • You may call this function only during a query operation, indicating that you need to pay a cost of 11 to add a set AA into the final union.

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

void init(int id,int n,int q,vector<int>dad,vector<infoset>ve);

  • You may preprocess some information before the qq operations start.
  • The cost consumed in this function will not be counted into the maximum cost of a single operation, but it will be counted into the total cost during the entire execution.
  • id indicates the subtask number, n indicates the total number of nodes, and q indicates the total number of operations.
  • The size of dad is n1n-1, and dad[i] indicates the parent of node i+2i+2 at the initial moment.
  • The size of ve is nn, and ve[i] indicates the initial set {i+1}\{i+1\}.

void modify(int x,int y);

  • Execute one modification operation, setting the parent of node xx to node yy.

void ask(int x,int y);

  • Execute one query operation, querying the tree-path information from xx to yy.
  • You need to call report some number of times to answer the query. At the end of this query, the interaction library will check whether all sets AA you reported satisfy the conditions.

During evaluation, the interaction library will call init exactly once, and then call modify and ask a total of qq times.

This problem guarantees that the tree shape and the sequence of operations are fully determined before the interaction starts, and will not be constructed dynamically based on the interaction process with your program. Therefore, the interactive operations in this problem are deterministic, and you do not need to care about the specific implementation of these operations in the interaction library.

During evaluation, the interaction library will use a special implementation: a single variable of type infoset will always consume 88 bytes of memory. Please ensure that there are not too many variables of type infoset existing at the same time during the execution of your program.

It is guaranteed that, under the call limits, the time required by the interaction library will not exceed 2s2\text{s}; the memory consumed by the interaction library itself will not exceed 16MB16\text{MB}.

Output Format

Test Program Mode

The grader.cpp in the provided files is a reference implementation of the interaction library. The interaction library used in the final testing is different from this reference implementation, so contestants’ solutions should not depend on the interaction library implementation.

You need to compile to an executable program using the following command: g++ grader.cpp long.cpp -o long -static -O2 -std=c++14

For the compiled executable:

  • The executable will read input data from standard input in the following format:
    • The first line contains an integer idid, indicating the subtask number.
    • The second line contains two integers n,qn, q, indicating the total number of nodes and the total number of operations.
    • The third line contains n1n-1 integers, indicating the parent node indices of nodes 22 to nn.
    • The next qq lines each contain three integers op,x,yop, x, y, describing one operation. op=0op = 0 means this is a query operation, and op=1op = 1 means this is a modification operation.
  • After reading is completed, the interaction library will call the function init exactly once and then call modify or ask qq times, using the input data to test your functions. After the program runs normally, it will output several lines. Each line contains several numbers representing your answer to each query operation. The last line will include two integers W1,W2W_1, W_2, representing the total cost consumed by your program and the maximum cost of a single operation, respectively.

The provided files include four sets of sample files, where the fourth set satisfies the special property of Subtask 2. When testing samples, please ignore the comparison of the last line in the output file, because the last line output by the interaction library is the running cost of the program, not the query results of the query operations.

Hint

Scoring

This problem is first subject to the same constraints as traditional problems. For example, a compilation error will cause you to get 00 points for the entire problem; runtime errors, exceeding the time limit, exceeding the memory limit, etc. will cause you to get 00 points for the corresponding test points. You can only access the variables you define and those provided by the interaction library, as well as their corresponding memory space. Attempting to access other space may lead to compilation errors or runtime errors.

On the basis of the above conditions, for a test point, if your program performs illegal function calls, fails to terminate normally, or provides an incorrect answer to a query operation, that test point will receive 00 points. Otherwise, scoring will be based on the cost consumed by your program, with two scoring methods:

  • Scoring method 1: If W1>6108W_1 \gt 6 \cdot 10^8, then the score for that test point is 00. Otherwise, the score for that test point is $\frac{\frac{1.5 \cdot 10^8}{max(W_1, 1.5 \cdot 10^8)} \cdot 7 + 3}{10} \times$ the subtask score value of that test point.
  • Scoring method 2: If W2>2104W_2 \gt 2 \cdot 10^4, then the score for that test point is 00. Otherwise, the score for that test point is 5000max(W2,5000)×\frac{5000}{max(W_2, 5000)} \times the subtask score value of that test point.

Different subtasks use different scoring methods. The score of a subtask is the minimum of the scores of all test points in that subtask.

Constraints and Agreements

It is guaranteed that for all test points, 1n,q1051 \le n, q \le 10^5.

Subtask ID n,qn, q \le Special Property Scoring Method Score
11 100100 None 1 1010
22 10510^5 Yes 2020
33 2
44 None 1 3030
55 2 2020

Special property: It is guaranteed that at any time, except for node 11, each node has at most one child.

Afterword

Xin found that you still did not solve this problem after five hours, and she was very satisfied with Long’s abilities. However, the next morning, Xin suddenly found that the complexity proof of Long’s solution was wrong.

Translated by ChatGPT 5