CF1540E.Tasty Dishes
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Note that the memory limit is unusual.
There are n chefs numbered 1,2,…,n that must prepare dishes for a king. Chef i has skill i and initially has a dish of tastiness ai where ∣ai∣≤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 i 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.
- 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⋅ai ) (or doing nothing).
- After all chefs (who wanted) worked on their own dishes, each start observing the other chefs. In particular, for each chef j on chef i 's list, chef i can choose to copy (or not copy) j 's dish, thereby adding the tastiness of the j 's dish to i 's dish ( ai:=ai+aj ) (or doing nothing). It can be assumed that all copying occurs simultaneously. Namely, if chef i chooses to copy from chef j he will copy the tastiness of chef j 's dish at the end of stage 1 .
All chefs work to maximize the tastiness of their own dish in order to please the king.
Finally, you are given q queries. Each query is one of two types.
- 1 k l r — find the sum of tastiness al,al+1,…,ar after the k -th day. Because this value can be large, find it modulo 109+7 .
- 2 i x — the king adds x tastiness to the i -th chef's dish before the 1 -st day begins ( ai:=ai+x ). Note that, because the king wants to see tastier dishes, he only adds positive tastiness ( x>0 ).
Note that queries of type 1 are independent of each all other queries. Specifically, each query of type 1 is a scenario and does not change the initial tastiness ai of any dish for future queries. Note that queries of type 2 are cumulative and only change the initial tastiness ai of a dish. See notes for an example of queries.
输入格式
The first line contains a single integer n ( 1≤n≤300 ) — the number of chefs.
The second line contains n integers a1,a2,…,an ( −i≤ai≤i ).
The next n lines each begin with a integer ci ( 0≤ci<n ), denoting the number of chefs the i -th chef can copy from. This number is followed by ci distinct integers d ( i<d≤n ), signifying that chef i is allowed to copy from chef d during stage 2 of each day.
The next line contains a single integer q ( 1≤q≤2⋅105 ) — the number of queries.
Each of the next q lines contains a query of one of two types:
- 1 k l r ( 1≤l≤r≤n ; 1≤k≤1000 );
- 2 i x ( 1≤i≤n ; 1≤x≤1000 ).
It is guaranteed that there is at least one query of the first type.
输出格式
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:
- 1 : {2,3,4,5}
- 2 : {3}
- 3 : {4}
- 4 : {5}
- 5 : ∅ (no other chefs)
Following is a description of the sample.
For the first query of type 1 , the initial tastiness values are [1,0,−2,−2,4] .
The final result of the first day is shown below:
- [1,0,−2,−2,20] (chef 5 works on his dish).
- [21,0,−2,18,20] (chef 1 and chef 4 copy from chef 5 ).
So, the answer for the 1 -st query is 21+0−2+18+20=57 .
For the 5 -th query ( 3 -rd of type 1 ). The initial tastiness values are now [1,0,0,1,4] .
Day 1
- [1,0,0,4,20] (chefs 4 and 5 work on their dishes).
- [25,0,4,24,20] (chef 1 copies from chefs 4 and 5 , chef 3 copies from chef 4 , chef 4 copies from chef 5 ).
Day 2
- [25,0,12,96,100] (all chefs but chef 2 work on their dish).
- [233,12,108,196,100] (chef 1 copies from chefs 3 , 4 and 5 , chef 2 from 3 , chef 3 from 4 , chef 4 from chef 5 ).So, the answer for the 5 -th query is 12+108+196=316 .
It can be shown that, in each step we described, all chefs moved optimally.