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. How to Create a Perl Based Custom Monitor on NetScaler

    How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...

  2. IHE PIX规范

    IHE(Integrating Healthcare Enterprise) 集成医疗企业 IHE概念是由医学专家和广大医护工作者.相关政府部门.信息技术专家和企业共同发起的,目的是提供一种更好的方法 ...

  3. [洛谷P3376]【模板】网络最大流(ISAP)

    C++ Code:(ISAP) #include <cstdio> #include <cstring> #define maxn 1210 #define maxm 1200 ...

  4. Mybatis缓存机制及mybatis的各个组成部分

    Mybatis 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 ...

  5. [Leetcode] Add two numbers 两数之和

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. gcc用法小记

    By francis_hao    Feb 13,2017 概要 这里只列出了最常用的选项   选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...

  7. pycharm激活(JetBrains IDEA 系列产品通用xx方法(license server))

    http://xclient.info/a/f0b9738a-36fd-8a97-a966-0d3db497092d.html .打开激活窗口 .选择 Activate new license wit ...

  8. lwIP RAW_API

    lwIP RAW TCP/IP接口 作者: Adam Dunkels, Leon Woestenberg, Christiaan Simons lwIP为使用TCP/IP协议通信的应用程序编程提供了两 ...

  9. nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器

    转载自:http://blog.csdn.net/cjsafty/article/details/7892392 目前,由于Flash的流行,网络上绝大多数的微视频网站都采用了Flv格式来播放视频. ...

  10. 初识 spl_autoload_register

    spl_autoload_register 一.首先我们看来自官网的定义 版本要求:php版本为5.1.2+ 说明:注册给定的函数作为__autoload的实现.即自动加载 函数参数说明: bool ...