#P2146. [NOI2015] 软件包管理器

    ID: 1124 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树形结构2015线段树NOI 系列深度优先搜索,DFS树链剖分,树剖

[NOI2015] 软件包管理器

Description

You decide to design your own package manager. Inevitably, you must handle dependencies between packages. If package aa depends on package bb, then before installing package aa, you must first install package bb. Likewise, if you want to uninstall package bb, you must uninstall package aa.

Now you have obtained all dependency relations among packages. Moreover, due to your previous design, every package in your manager except package 00 depends on exactly one package, and package 00 depends on none. The dependency relations contain no cycles (i.e., there do not exist mm packages a1,a2,,ama_1, a_2, \dots, a_m such that for i<mi < m, aia_i depends on ai+1a_{i+1}, and ama_m depends on a1a_1).

You now need to write a dependency resolver for your package manager. According to user feedback, when installing or uninstalling a package, users want to quickly know how many packages will actually change their installation state as a result of the operation (that is, how many currently uninstalled packages will be installed by an install operation, or how many currently installed packages will be uninstalled by an uninstall operation). Your task is to implement this part.

Note that installing an already installed package, or uninstalling a package that is not installed, will not change any installation state. In this case, the number of packages whose installation state changes is 00.

Input Format

The first line contains a positive integer nn, the number of packages, numbered starting from 00.
The second line contains n1n - 1 integers; the ii-th integer denotes the package ID that package ii depends on.
Then a line with a positive integer qq follows, denoting the number of operations, in the following format:

  • install x means install package xx.
  • uninstall x means uninstall package xx.

Initially, all packages are not installed.

For each operation, you need to output how many packages will change their installation state because of this step, and then apply the operation (i.e., update the installation states you maintain).

Output Format

Output qq lines, each containing an integer, which is the answer for each query.

7
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
3
1
3
2
3
10
0 1 2 1 3 0 0 3 2
10
install 0
install 3
uninstall 2
install 7
install 5
install 9
uninstall 9
install 4
install 1
install 9
1
3
2
1
3
1
1
1
0
1

Hint


Initially, all packages are not installed.

To install package 55, you need to install packages 0,1,50, 1, 5.

After that, installing package 66 only requires installing package 66. At this point, packages 0,1,5,60, 1, 5, 6 are installed.

Uninstalling package 11 requires uninstalling packages 1,5,61, 5, 6. Now only package 00 remains installed.

Next, installing package 44 requires installing packages 1,41, 4. Now packages 0,1,40, 1, 4 are installed. Finally, uninstalling package 00 will uninstall all packages.

Constraints

::cute-table{tuack}

Test point ID $$n$$ size $$q$$ size Notes
11 n=5,000{n = 5{,}000} q=5,000{q = 5{,}000}
22 ^
33 n=100,000{n = 100{,}000} q=100,000{q = 100{,}000} testdata does not include uninstall operations
44 ^
55 n=100,000{n = 100{,}000} q=100,000{q = 100{,}000} For package ii, the ID of the package it depends on is uniformly random in [0,i1][0, i - 1], and the package ID in each operation is uniformly random in [0,n1][0, n - 1].
66 ^
77
88
99 n=100,000{n = 100{,}000} q=100,000{q = 100{,}000}
1010 ^
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020

Translated by ChatGPT 5