CF1540E.Tasty Dishes

NOI/NOI+/CTSC

通过率:0%

时间限制:10.00s

内存限制:64MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Note that the memory limit is unusual.

There are nn chefs numbered 1,2,…,n1, 2, \ldots, n that must prepare dishes for a king. Chef ii has skill ii and initially has a dish of tastiness aia_i where ∣ai∣≤i|a_i| \leq i. Each chef has a list of other chefs that he is allowed to copy from. To stop chefs from learning bad habits, the king makes sure that chef ii can only copy from chefs of larger skill.

There are a sequence of days that pass during which the chefs can work on their dish. During each day, there are two stages during which a chef can change the tastiness of their dish.

  1. At the beginning of each day, each chef can choose to work (or not work) on their own dish, thereby multiplying the tastiness of their dish of their skill (ai:=i⋅aia_i := i \cdot a_i) (or doing nothing).
  2. After all chefs (who wanted) worked on their own dishes, each start observing the other chefs. In particular, for each chef jj on chef ii's list, chef ii can choose to copy (or not copy) jj's dish, thereby adding the tastiness of the jj's dish to ii's dish (ai:=ai+aja_i := a_i + a_j) (or doing nothing). It can be assumed that all copying occurs simultaneously. Namely, if chef ii chooses to copy from chef jj he will copy the tastiness of chef jj's dish at the end of stage 11.

All chefs work to maximize the tastiness of their own dish in order to please the king.

Finally, you are given qq queries. Each query is one of two types.

  1. 11 kk ll rr — find the sum of tastiness al,al+1,…,ara_l, a_{l+1}, \ldots, a_{r} after the kk-th day. Because this value can be large, find it modulo 109+710^9 + 7.
  2. 22 ii xx — the king adds xx tastiness to the ii-th chef's dish before the 11-st day begins (ai:=ai+xa_i := a_i + x). Note that, because the king wants to see tastier dishes, he only adds positive tastiness (x>0x \gt 0).

Note that queries of type 11 are independent of each all other queries. Specifically, each query of type 11 is a scenario and does not change the initial tastiness aia_i of any dish for future queries. Note that queries of type 22 are cumulative and only change the initial tastiness aia_i of a dish. See notes for an example of queries.

注意:本题的内存限制较为特殊。

有 nn 位厨师,编号为 1,2,…,n1, 2, \ldots, n,他们需为国王准备菜肴。厨师 ii 的技能值为 ii,初始时其菜肴的美味度为 aia_i,满足 ∣ai∣≤i|a_i| \leq i。每位厨师均拥有一张允许其模仿的其他厨师列表。为防止厨师沾染不良习惯,国王规定:厨师 ii 只能向技能值严格更大的厨师学习(即只能从技能值 >i> i 的厨师处模仿)。

在若干天中,厨师们可不断改进自己的菜肴。每天分为两个阶段,厨师可在其中改变自己菜肴的美味度:

  1. 每天开始时,每位厨师可选择是否对自己的菜肴进行加工(也可选择不操作),若加工,则将其菜肴美味度乘以其自身技能值(即执行 ai:=i⋅aia_i := i \cdot a_i);
  2. 所有厨师完成第 1 阶段操作后,每位厨师开始观察列表中的其他厨师。具体而言,对厨师 ii 列表中的每位厨师 jj,厨师 ii 可选择是否模仿 jj 的菜肴(也可选择不模仿),若模仿,则将其菜肴美味度加上厨师 jj 当前的美味度(即执行 ai:=ai+aja_i := a_i + a_j);所有模仿操作是同时进行的,即厨师 ii 若选择模仿厨师 jj,则所加的值为厨师 jj 在第 1 阶段结束后的美味度。

所有厨师的目标均为最大化自己菜肴的美味度,以取悦国王。

最后,你将收到 qq 个查询,每个查询为以下两种类型之一:

  1. 1 k l r —— 求第 kk 天结束后,美味度之和 al+al+1+⋯+ara_l + a_{l+1} + \cdots + a_r 的值;由于该值可能极大,请对 109+710^9 + 7 取模输出。
  2. 2 i x —— 国王在第 11 天开始前,向第 ii 位厨师的菜肴额外添加 xx 美味度(即执行 ai:=ai+xa_i := a_i + x)。注意:因国王希望看到更美味的菜肴,故只添加正数美味度(即 x>0x > 0)。

注意:类型 11 的查询彼此相互独立。具体而言,每个类型 11 的查询仅对应一个独立场景,不会影响后续查询中任何菜肴的初始美味度 aia_i。而类型 22 的查询是累加的,仅修改某菜肴的初始美味度 aia_i。关于查询的示例,请参见题目末尾的“说明”部分。

输入格式

The first line contains a single integer nn (1≤n≤3001 \le n \le 300) — the number of chefs.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−i≤ai≤i-i \le a_i \le i).

The next nn lines each begin with a integer cic_i (0≤ci<n0 \le c_i \lt n), denoting the number of chefs the ii-th chef can copy from. This number is followed by cic_i distinct integers dd (i<d≤ni \lt d \le n), signifying that chef ii is allowed to copy from chef dd during stage 22 of each day.

The next line contains a single integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5) — the number of queries.

Each of the next qq lines contains a query of one of two types:

  • 11 kk ll rr (1≤l≤r≤n1 \le l \le r \le n; 1≤k≤10001 \le k \le 1000);
  • 22 ii xx (1≤i≤n1 \le i \le n; 1≤x≤10001 \le x \le 1000).

It is guaranteed that there is at least one query of the first type.

第一行包含一个整数 nn(1≤n≤3001 \le n \le 300)—— 厨师的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−i≤ai≤i-i \le a_i \le i)。

接下来的 nn 行,每行以一个整数 cic_i(0≤ci<n0 \le c_i < n)开头,表示第 ii 位厨师在每天第二阶段中可以模仿的厨师数量。该整数后跟 cic_i 个互不相同的整数 dd(i<d≤ni < d \le n),表示第 ii 位厨师在每天第二阶段中允许模仿第 dd 位厨师。

下一行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)—— 查询的数量。

接下来的 qq 行中,每行包含以下两种类型之一的查询:

  • 11 kk ll rr(1≤l≤r≤n1 \le l \le r \le n;1≤k≤10001 \le k \le 1000);
  • 22 ii xx(1≤i≤n1 \le i \le n;1≤x≤10001 \le x \le 1000)。

保证至少存在一个第一类查询。

输出格式

For each query of the first type, print a single integer — the answer to the query.

对于每个第一类查询,输出一个整数——该查询的答案。

输入输出样例

  • 输入#1

    5
    1 0 -2 -2 4
    4 2 3 4 5
    1 3
    1 4
    1 5
    0
    7
    1 1 1 5
    2 4 3
    1 1 1 5
    2 3 2
    1 2 2 4
    2 5 1
    1 981 4 5

    输出#1

    57
    71
    316
    278497818

说明/提示

Below is the set of chefs that each chef is allowed to copy from:

  • 11: 2,3,4,5{2, 3, 4, 5}
  • 22: 3{3}
  • 33: 4{4}
  • 44: 5{5}
  • 55: ∅\emptyset (no other chefs)

Following is a description of the sample.

For the first query of type 11, the initial tastiness values are [1,0,−2,−2,4][1, 0, -2, -2, 4].

The final result of the first day is shown below:

  1. [1,0,−2,−2,20][1, 0, -2, -2, 20] (chef 55 works on his dish).
  2. [21,0,−2,18,20][21, 0, -2, 18, 20] (chef 11 and chef 44 copy from chef 55).

So, the answer for the 11-st query is 21+0−2+18+20=5721 + 0 - 2 + 18 + 20 = 57.

For the 55-th query (33-rd of type 11). The initial tastiness values are now [1,0,0,1,4][1, 0, 0, 1, 4].

Day 1

  1. [1,0,0,4,20][1, 0, 0, 4, 20] (chefs 44 and 55 work on their dishes).
  2. [25,0,4,24,20][25,0, 4, 24, 20] (chef 11 copies from chefs 44 and 55, chef 33 copies from chef 44, chef 44 copies from chef 55).

Day 2

  1. [25,0,12,96,100][25, 0, 12, 96, 100] (all chefs but chef 22 work on their dish).

  2. [233,12,108,196,100][233, 12, 108, 196, 100] (chef 11 copies from chefs 33, 44 and 55, chef 22 from 33, chef 33 from 44, chef 44 from chef 55).

    So, the answer for the 55-th query is 12+108+196=31612+108+196=316.

It can be shown that, in each step we described, all chefs moved optimally.

以下是每位厨师被允许抄袭的厨师集合:

  • 11: 2,3,4,5{2, 3, 4, 5}
  • 22: 3{3}
  • 33: 4{4}
  • 44: 5{5}
  • 55: ∅\emptyset(无其他厨师)

以下是样例说明。

对于第一个类型为 11 的查询,初始美味度值为 [1,0,−2,−2,4][1, 0, -2, -2, 4]。

第一天的最终结果如下所示:

  1. [1,0,−2,−2,20][1, 0, -2, -2, 20](厨师 55 对其菜品进行操作)。
  2. [21,0,−2,18,20][21, 0, -2, 18, 20](厨师 11 和厨师 44 向厨师 55 抄袭)。

因此,第 11 个查询的答案为 21+0−2+18+20=5721 + 0 - 2 + 18 + 20 = 57。

对于第 55 个查询(即第 33 个类型为 11 的查询),当前初始美味度值为 [1,0,0,1,4][1, 0, 0, 1, 4]。

第一天:

  1. [1,0,0,4,20][1, 0, 0, 4, 20](厨师 44 和厨师 55 对其菜品进行操作)。
  2. [25,0,4,24,20][25, 0, 4, 24, 20](厨师 11 向厨师 44 和厨师 55 抄袭,厨师 33 向厨师 44 抄袭,厨师 44 向厨师 55 抄袭)。

第二天:

  1. [25,0,12,96,100][25, 0, 12, 96, 100](除厨师 22 外,其余所有厨师均对其菜品进行操作)。
  2. [233,12,108,196,100][233, 12, 108, 196, 100](厨师 11 向厨师 33、44 和 55 抄袭,厨师 22 向厨师 33 抄袭,厨师 33 向厨师 44 抄袭,厨师 44 向厨师 55 抄袭)。

因此,第 55 个查询的答案为 12+108+196=31612+108+196=316。

可以证明,在我们所描述的每一步中,所有厨师均采取了最优行动。

输入解题思路,AI测评打分。不知道怎么写?

首页