#P10437. [JOIST 2024] JOI 之旅 / JOI Tour

    ID: 13475 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树2024交互题O2优化树链剖分JOI(日本)动态 DP

[JOIST 2024] JOI 之旅 / JOI Tour

Background

When submitting, please do not include joitour.h.

Please do not submit using C++14 (GCC 9).

Problem Description

In the country of IOI, there are NN cities, numbered from 00 to N1N-1, and N1N-1 roads, numbered from 00 to N2N-2. Road j (0jN2)j\ (0\le j\le N-2) connects city UjU_j and city VjV_j bidirectionally. You can travel from one city to any other city via roads.

Each city in the country of IOI has a restaurant. The restaurant type in city i (0iN1)i\ (0\le i\le N-1) is denoted by FiF_i. Specifically:

  • Fi=0F_i=0: juice shop.
  • Fi=1F_i=1: Japanese omelette shop.
  • Fi=2F_i=2: ice cream shop.

Ms. Rie is a tour guide in the country of IOI. She is planning a sightseeing program called the JOI Tour. In the JOI Tour, the plan is to visit the following 33 types of restaurants in this order:

  1. Choose a city i0 (0i0N1)i_0\ (0\le i_0\le N-1) that has a juice shop, and start the trip from city i0i_0.
  2. Visit the juice shop in city i0i_0.
  3. Choose a city i1 (0i1N1)i_1\ (0\le i_1\le N-1) that has a Japanese omelette shop, and take a bus from city i0i_0 to city i1i_1 along a shortest path.
  4. Visit the Japanese omelette shop in city i1i_1.
  5. Choose a city i2 (0i2N1)i_2\ (0\le i_2\le N-1) that has an ice cream shop, and take a bus from city i1i_1 to city i2i_2 along a shortest path.
  6. Visit the ice cream shop in city i2i_2.
  7. End the trip in city i2i_2.

To avoid boring the tourists, Ms. Rie decides to choose three cities i0,i1,i2i_0,i_1,i_2 such that they do not pass through the same road twice. We call such a JOI Tour a good one. To help her find an ideal plan, you are asked to compute the number of good JOI Tours. In other words, you need to find the number of triples (i0,i1,i2)(i_0,i_1,i_2) that satisfy all of the following conditions:

  • The restaurant in city i0i_0 is a juice shop.
  • The restaurant in city i1i_1 is a Japanese omelette shop.
  • The restaurant in city i2i_2 is an ice cream shop.
  • While moving along shortest paths from city i0i_0 to i1i_1, and then from i1i_1 to i2i_2, you do not pass through the same road twice.

In the country of IOI, there will be QQ events where restaurant types change. When the (k+1) (0kQ1)(k+1)\ (0\le k\le Q-1)-th event happens, two integers XkX_k and YkY_k are given, satisfying 0XkN10\le X_k\le N-1 and 0Yk20\le Y_k\le 2. Then, the restaurant type in city XkX_k becomes YkY_k. That is, when Yk=0,1,2Y_k=0,1,2, the restaurant type becomes a juice shop, a Japanese omelette shop, and an ice cream shop, respectively. After each event, you should immediately compute the latest number of good JOI Tours and tell Ms. Rie.

Given the roads and restaurant information, compute the number of good JOI Tours after each type-change event.

Implementation Details

You need to implement the following functions.

  • void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V, int Q)
    • Use this function to provide the road and restaurant information.
    • This function is called only once at the beginning of the program.
    • The parameter N is the number of cities NN.
    • The parameter F is an array of length NN. F[i] (0iN10\le i\le N-1) represents the restaurant type in city ii, i.e. FiF_i.
    • The parameters U and V are arrays of length N1N-1. U[j] and V[j] (0jN20\le j\le N-2) are the two cities UjU_j and VjV_j connected by road jj.
    • The parameter Q is the number of restaurant type-change events QQ.
  • void change(int X, int Y)
    • Use this function to provide a restaurant type-change event.
    • This function is called QQ times.
    • In the (k+1) (0kQ1)(k+1)\ (0\le k\le Q-1)-th call, the parameter X is the city index where the change happens, i.e. XkX_k.
    • In the (k+1) (0kQ1)(k+1)\ (0\le k\le Q-1)-th call, the parameter Y is the new restaurant type, i.e. YkY_k. It is guaranteed that the new type is different from the old type.
  • long long num_tours()
    • This function is called in the following situations, for a total of Q+1Q+1 times.
      • After executing the function init.
      • After executing the function change.
    • This function should return the latest number of good JOI Tours.

Input Format

The following is the input format of the provided grader. You should not read any information from standard input.

The first line contains an integer NN.

The second line contains NN integers F0,,FN1F_0,\ldots,F_{N-1}.

The next N1N-1 lines each contain two integers Uj,VjU_j,V_j.

The next line contains an integer QQ.

The next QQ lines each contain two integers Xk,YkX_k,Y_k.

Output Format

The following is the output format of the provided grader. You should not print any information to standard output.

After each call to the function num_tours, the provided grader outputs one line containing an integer, which is the return value of this function.

3
0 1 2
0 1
1 2
0
1
3
0 1 2
0 1
1 2
2
2 0
0 2
1
0
1
7
1 0 2 2 0 1 0
0 1
0 2
1 3
1 4
2 5
2 6
7
0 0
1 1
2 0
3 0
4 2
5 2
6 2
3
0
4
4
0
4
5
5

Hint

Sample Explanation 1

Function Call Return Value
init(3, [0, 1, 2], [0, 1], [1, 2], 0)
num_tours() 11

There is exactly one good JOI Tour, which is (i0,i1,i2)=(0,1,2)(i_0,i_1,i_2)=(0,1,2). Below is an explanation of why it satisfies the conditions of a good JOI Tour.

  • F0=0F_0=0, so the restaurant in city 00 is a juice shop.
  • F1=1F_1=1, so the restaurant in city 11 is a Japanese omelette shop.
  • F2=2F_2=2, so the restaurant in city 22 is an ice cream shop.
  • When we move along shortest paths from city 00 to 11, and then from 11 to 22, we do not pass through the same road twice.

Therefore, the return value of the first call to num_tours() is 11.

This sample satisfies the constraints of subtasks 1,2,3,4,6,71,2,3,4,6,7.

Sample Explanation 2

Function Call Return Value
init(3, [0, 1, 2], [0, 1], [1, 2], 2)
num_tours() 11
change(2, 0)
num_tours() 00
change(0, 2)
num_tours() 11

Initially, there is one good JOI Tour, which is (i0,i1,i2)=(0,1,2)(i_0,i_1,i_2)=(0,1,2). Therefore, the return value of the first call to num_tours() is 11.

In the first event, the restaurant in city 22 changes from an ice cream shop to a juice shop. After this change, ice cream shops disappear from the country of IOI, so there are no good JOI Tours anymore. Therefore, the return value of the second call to num_tours() is 00.

In the second event, the restaurant in city 00 changes from a juice shop to an ice cream shop. After this change, there is one good JOI Tour, which is (i0,i1,i2)=(2,1,0)(i_0,i_1,i_2)=(2,1,0). Therefore, the return value of the third call to num_tours() is 11.

This sample satisfies the constraints of subtasks 1,2,4,6,71,2,4,6,7.

Sample Explanation 3

This sample satisfies the constraints of subtasks 1,2,5,6,71,2,5,6,7.

Important Notes

  • Your program may implement other functions for internal use, or use global variables.
  • Your program is not allowed to use standard input/output. Your program is not allowed to interact with other files in any way. However, your program may output debugging information to standard error output.

Compilation and Test Run

You can download the sample interactor in “File - Additional Files” to test your program. The additional files also include a sample program.

The sample interactor is the file grader.cpp. To test your program, put grader.cpp, joitour.cpp, and joitour.h in the same directory, and compile with the following command. You may also run compile.sh to compile your program.

g++ -std=gnu++20 -O2 -o grader grader.cpp joitour.cpp

If compilation succeeds, an executable file grader will be generated.

Note that the interactor used for judging is different from the sample interactor. The sample interactor runs as a single process: it reads input from standard input and outputs results to standard output.

Constraints

  • 3N2×1053\le N\le 2\times 10^5.
  • 0Fi2 (0iN1)0\le F_i\le 2\ (0\le i\le N-1).
  • 0Uj<VjN1 (0jN2)0\le U_j<V_j\le N-1\ (0\le j\le N-2).
  • It is possible to travel from any city to any other city via roads.
  • 0Q5×1040\le Q\le 5\times 10^4
  • 0XkN1 (0kQ1)0\le X_k\le N-1\ (0\le k\le Q-1)
  • 0Yk2 (0kQ1)0\le Y_k\le 2\ (0\le k\le Q-1)
  • For each call to the function change, the new type is different from the old type.

Subtasks

  • (6 points) N400N\le 400, Q100Q\le 100.
  • (8 points) N4000N\le 4\,000, Q1000Q\le 1\,000.
  • (6 points) Q=0Q=0.
  • (16 points) Uj=j,Vj=j+1 (0jN2)U_j=j,V_j=j+1\ (0\le j\le N-2).
  • (16 points) $U_j=\lfloor\frac{j}{2}\rfloor,V_j=j+1\ (0\le j\le N-2)$.
  • (34 points) N105N\le 10^5, Q2.5×104Q\le 2.5\times 10^4.
  • (14 points) No additional constraints.

Translated by ChatGPT 5