PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000105. Tổng trên các tập chỉ số (Subset Sums)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng trên các tập chỉ số (Subset Sums)

Subset Sums

Source: Codeforces Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n and mm sets of indices S1,S2,…,SmS_1,S_2,\ldots,S_m. Each set contains distinct positions between 11 and nn. Different sets may overlap.

Process qq operations in the given order:

  • ? k: Print the sum ∑i∈Skai\displaystyle\sum_{i\in S_k}a_i using the current array values.
  • + k x: For every position i∈Ski\in S_k, replace aia_i by ai+xa_i+x. All other positions remain unchanged. The integer xx may be negative or zero.

All updates persist. If a position belongs to several sets, an update to a given set changes that position exactly once.

Input

  • Line 1: integers n m q.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n.
  • Each of the next mm lines begins with a positive integer sk=∣Sk∣s_k=|S_k|, followed by sks_k distinct indices belonging to set SkS_k.
  • The final qq lines contain either ? k or + k x.

Output

For each ? k operation, output the requested sum on its own line in query order. The answer may be negative and may exceed the 32-bit signed integer range.

Subtasks

  • Subtask 1 (15 points): 1≤n≤301\le n\le 30, 1≤m≤301\le m\le 30, 1≤q≤601\le q\le 60, sum of set sizes at most 100100; 1≤∣Sk∣≤n1\le |S_k|\le n, distinct indices within each set; ∣ai∣,∣x∣≤108|a_i|,|x|\le10^8.
  • Subtask 2 (20 points): 1≤n≤4001\le n\le 400, 1≤m≤4001\le m\le 400, 1≤q≤20001\le q\le 2000, sum of set sizes at most 40004000; 1≤∣Sk∣≤n1\le |S_k|\le n, distinct indices within each set; ∣ai∣,∣x∣≤108|a_i|,|x|\le10^8.
  • Subtask 3 (25 points): 1≤n≤50001\le n\le 5000, 1≤m≤50001\le m\le 5000, 1≤q≤200001\le q\le 20000, sum of set sizes at most 3000030000; 1≤∣Sk∣≤n1\le |S_k|\le n, distinct indices within each set; ∣ai∣,∣x∣≤108|a_i|,|x|\le10^8.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000, 1≤m≤1000001\le m\le 100000, 1≤q≤1000001\le q\le 100000, sum of set sizes at most 100000100000; 1≤∣Sk∣≤n1\le |S_k|\le n, distinct indices within each set; ∣ai∣,∣x∣≤108|a_i|,|x|\le10^8.

Examples

Input

5 3 5
5 -5 5 1 -4
2 1 2
4 2 1 4 5
2 2 5
? 2
+ 3 4
? 1
+ 2 1
? 2

Output

-3
4
9

Explanation

Initially, S2={2,1,4,5}S_2=\{2,1,4,5\} has sum −5+5+1−4=−3-5+5+1-4=-3. Operation + 3 4 increases a2a_2 and a5a_5 to −1-1 and 00, so the sum of S1={1,2}S_1=\{1,2\} becomes 5+(−1)=45+(-1)=4. Finally, + 2 1 increments the four positions in S2S_2, giving the sum 0+6+2+1=90+6+2+1=9.