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:
- Find the sum of elements with indices from set Sk:
. The query format is "? k".
- 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.
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.
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 %I64dspecifier.
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
-3 4 9
又一次让我领略了div1的可怕...
题目的意思是说一个长度为n的序列a[i]和m个集合Si,每个集合都用一些编号表示,这些编号对应的是序列中的元素,有两种操作:
1.查询第k个集合中元素之和
2.把第k个集合中对应a序列中的元素全部加上x
我是除了暴力没有想到什么好的方法,搜了博客,大牛的题解让我感叹就是大牛,分桶法,我以前都不知道还有这种套路。
http://blog.csdn.net/u013654696/article/details/41578337
一直想不到把时间复杂度怎么降下来,其实大牛的那句显然我还是不太懂是怎么出来的,显然重集合的个数不会超过B个,我显然了好一会儿没显然出来。不过方法学习到了,但是还是觉得好难(为什么想到了线段树的add数组),还要回来熟练这个题。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=111111;
int n,m,q;
LL a[maxn];
int h[maxn];//h[i]=1:第i个集合是重集合,h[i]=0:第i个集合是轻集合
int cnt[maxn][360];//cnt[i][j]是第i个集合和第j个重集合的交集元素个数
int res,id[maxn];//res表示重集合的数量,id[i]表示第i个重集合的编号
LL sum[maxn];//sum[i]表示第i个集合(重)中元素总和
LL add[maxn];//add[i]表示第i个集合(重)中元素更新值
vector<int>g[maxn],gg[maxn];//g[i]表示第i个集合,gg[i]表示第i个元素所在集合
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
}
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
memset(add,0,sizeof(add));
int nn=sqrt(n+0.5),res=0;
for(int i=1;i<=m;i++){
int num,temp;
scanf("%d",&num);
if(num>=nn)
h[i]=1,id[++res]=i;
else
h[i]=0;
for(int j=0;j<num;j++){
scanf("%d",&temp);
g[i].push_back(temp);
if(h[i]){
sum[i]+=a[temp];
gg[temp].push_back(res);
}
}
}
for(int i=1;i<=m;i++){
for(int j=0;j<(int)g[i].size();j++){
int u=g[i][j];
for(int k=0;k<(int)gg[u].size();k++){
int v=gg[u][k];
cnt[i][v]++;
}
}
}
while(q--){
int k,x;
char op[10];
scanf("%s%d",op,&k);
if(op[0]=='?'){
LL ans=0;
if(h[k]){
ans=sum[k];
for(int i=1;i<=res;i++){
ans+=add[id[i]]*cnt[k][i];
}
}
else{
for(int i=0;i<(int)g[k].size();i++){
ans+=a[g[k][i]];
}
for(int i=1;i<=res;i++){
ans+=add[id[i]]*cnt[k][i];
}
}
printf("%I64d\n",ans);
}
else{
scanf("%d",&x);
if(h[k])
add[k]+=x;
else{
for(int i=0;i<(int)g[k].size();i++){
a[g[k][i]]+=x;
}
for(int i=1;i<=res;i++){
sum[id[i]]+=x*cnt[k][i];
}
}
}
}
return 0;
}