C. Subset Sums
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1, a2, ..., an and m sets S1, S2, ..., Sm of indices of elements of this array. Let's denote Sk = {Sk, i} (1 ≤ i ≤ |Sk|). In other words, Sk, i is some element from set Sk.

In this problem you have to answer q queries of the two types:

  1. Find the sum of elements with indices from set Sk: . The query format is "? k".
  2. Add number x to all elements at indices from set Sk: aSk, i is replaced by aSk, i + x for all i (1 ≤ i ≤ |Sk|). The query format is "+ k x".

After each first type query print the required sum.

Input

The first line contains integers n, m, q (1 ≤ n, m, q ≤ 105). The second line contains n integers a1, a2, ..., an (|ai| ≤ 108) — elements of array a.

Each of the following m lines describes one set of indices. The k-th line first contains a positive integer, representing the number of elements in set (|Sk|), then follow |Sk| distinct integers Sk, 1, Sk, 2, ..., Sk, |Sk| (1 ≤ Sk, i ≤ n) — elements of set Sk.

The next q lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1 ≤ k ≤ m, |x| ≤ 108. The queries are given in order they need to be answered.

It is guaranteed that the sum of sizes of all sets Sk doesn't exceed 105.

Output

After each first type query print the required sum on a single line.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

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
【分析】一开始也想到了分块,但是一直以为是线段树,所以想歪了,然后看了网上的题解。。。太强了。

设B = sqrtn

先给集合分成两种, 一种是大小大于B的, 称为重集合, 小于的称为轻集合。

显然重集合的个数不会超过B个。

对每个集合, 求出它和每个重集合交集的大小, 即cnt[i][j]表示第i个集合和第j 个重集合交集的大小。 cnt数组可以O(NB)求出

对每个重集合, 维护add和sum, add表示加在这个集合上的值, sum表示这个集合没有加上其他重集合的附加值的和。

然后把操作分为4种:

1. 在轻集合上加值。 这样对每个轻集合里面的元素暴力加上v就行(O(B))。 同时要维护重集合的sum, 于是, 每个重集合的sum加上v*cnt[i][j],(O(B))。复杂度(O(B))。

2. 在重集合上加值。 直接在重集合的add上加上v。 (O(1))

3. 询问轻集合。 ans 加上轻集合里面的元素的值, (O(B))。 然后再加上重集合的add[j] * cnt[i][j], (O(B))。 O(B)

4. 询问重集合。 ans 加上重集合的sum, 然后再加上各个重集合的add[j] * cnt[i][j]。 O(B)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = ;
int n,m,q,k;
int cnt[][N],id[N],tot=;
int is[N];
ll a[N],add[N],sum[N];
vector<int>g[N],bl[N],big;
int main() {
int u,v;
scanf("%d%d%d",&n,&m,&q);
int b=sqrt(n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d",&k);
if(k>b)id[i]=++tot,big.pb(id[i]),is[i]=;;
for(int j=;j<k;j++){
scanf("%d",&u); g[i].pb(u);
if(k>b)bl[u].pb(tot),sum[tot]+=a[u];;
}
}
for(int i=;i<=m;i++){
for(int j=;j<g[i].size();j++){
int u=g[i][j];
for(int p=;p<bl[u].size();p++){
cnt[bl[u][p]][i]++;
}
}
}
char str[];
while(q--){
scanf("%s",str);
if(str[]=='+'){
scanf("%d%d",&u,&v);
if(is[u]){
add[id[u]]+=v;
}
else {
for(int i=;i<g[u].size();i++){
int c=g[u][i];
a[c]+=v;
}
for(int i=;i<big.size();i++){
int c=big[i];
sum[c]+=v*cnt[c][u];
}
}
}
else {
scanf("%d",&u);
if(is[u]){
ll ans=sum[id[u]];
for(int i=;i<big.size();i++){
int c=big[i];
ans+=add[c]*cnt[c][u];
}
printf("%lld\n",ans);
}
else {
ll ans=;
for(int i=;i<g[u].size();i++){
ans+=a[g[u][i]];
}
for(int i=;i<big.size();i++){
int c=big[i];
ans+=add[c]*cnt[c][u];
}
printf("%lld\n",ans);
}
}
}
return ;
}

CodeForces 348C Subset Sums(分块)(nsqrtn)的更多相关文章

  1. Codeforces 348C Subset Sums 分块思想

    题意思路:https://www.cnblogs.com/jianrenfang/p/6502858.html 第一次见这种思路,对于集合大小分为两种类型,一种是重集合,一种是轻集合,对于重集合,我们 ...

  2. Codeforces 348C - Subset Sums(根号分治)

    题面传送门 对于这类不好直接维护的数据结构,第一眼应该想到-- 根号分治! 我们考虑记[大集合]为大小 \(\geq\sqrt{n}\) 的集合,[小集合]为大小 \(<\sqrt{n}\) 的 ...

  3. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  4. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  5. Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验

    Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...

  6. Project Euler P105:Special subset sums: testing 特殊的子集和 检验

    Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...

  7. Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解

    Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...

  8. Codeforces348C - Subset Sums

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...

  9. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

随机推荐

  1. 【算法】CDQ分治 -- 三维偏序 & 动态逆序对

    初次接触CDQ分治,感觉真的挺厉害的.整体思路即分而治之,再用之前处理出来的答案统计之后的答案. 大概流程是(对于区间 l ~ r): 1.处理 l ~mid, mid + 1 ~ r 的答案: 2. ...

  2. Seajs的用法

    以前经常听到Seajs,但是没深入了解过,不清楚到底是用做哪个方面,后来调组到M站做开发,发现项目用到了Seajs,便去了解下 SeaJS是一个遵循CMD规范的JavaScript模块加载框架,可以实 ...

  3. [COGS 2089.] 平凡的测试数据 带权并查集

    差点就撸上LCT了....... 带权并查集就是在并查集的基础上稍作修改,我的用穿址实现的有人用记录原父亲来实现. #include<cstdio> #define N 300010 us ...

  4. POJ2502:Subway(最短路)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14634   Accepted: 4718 题目链接:http ...

  5. CSS3学习之radial-gradient(径向渐变)

    转自:http://www.cnblogs.com/rainman/p/5133685.html 1.语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“ ...

  6. GDOI2015的某道题目

    分析: 考试的时候由于一些神奇的原因(我就不说是什么了)...没有想$C$题,直接交了个暴力上去... 然后发现暴力的数组开的太大,由于矩阵乘法的需要做$m$次初始化,所以只拿到了10分... 我们一 ...

  7. 【poj3693-重复次数最多的连续重复子串】后缀数组

    题意:给定一个串,长度<=10^5,求它重复次数最多的连续重复子串(输出字典序最小的那个). 例如ccabcabc,答案就是abcabc 一开始没想清楚,结果调了好久. 原理: 按照L划分,因为 ...

  8. bzoj1574 [Usaco2009 Jan]地震损坏Damage

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1574 [题解] 贪心把report的点的旁边所有点破坏即可. # include <s ...

  9. 51nod 1020 逆序排列——dp

    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...

  10. alt+ F8 设置无效(转)

    原文转自 https://blog.csdn.net/m372897500/article/details/7310251 具体修改方法如下: 工具-选项-环境-键盘-应用以下其他键盘映射方案,选择v ...