codeforces 348C Subset Sums

本文介绍了一个关于数组操作的问题解决方法,包括查询子集元素之和及更新特定集合中元素值的功能实现。通过区分轻块与重块优化处理流程,以提高算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/2018/03/02/codeforces-348c-subset-sums/
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.

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

Copy
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
题意简述

首先可以知道假设我把数量大于sqrt(n) 的块叫重块 剩下 的叫轻块 那么显然我重块的个数不会超过sqrt(n)个 那么我可以首先预处理一下 所有块和哪些重块有几个的交集

修改的时候如果是重块直接打修改标记 如果是轻块暴力修改 并且将带来的影响加到所影响到的重块的身上 查询的时候首先需要把重块的标记下放到我当前查询的这个块上 然后回答询问即可

#include<cmath>
#include<vector>
#include<cstdio>
#include<algorithm>
#define N 100010
#define ll long long
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
vector<int> s[N];
ll add[N],sum[N],a[N];
int tot,nm[N][400],id[400],n,m,ss;bool ta[400][N];
inline void change(int x,int w){
    for (int i=0;i<s[x].size();++i) a[s[x][i]]+=w;
    for (int i=1;i<=tot;++i) sum[id[i]]+=(ll)w*nm[x][i];
}
inline ll query(int idn){
    ll tmp=0;
    if(s[idn].size()<=ss)
    for (int i=0;i<s[idn].size();++i) tmp+=a[s[idn][i]];else tmp+=sum[idn];
    for (int i=1;i<=tot;++i) tmp+=add[id[i]]*nm[idn][i];return tmp;
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();int q=read();ss=0;
    for (int i=1;i<=n;++i) a[i]=read();
    for (int i=1;i<=m;++i){
        int x=0;
        int len=read();ss+=len;
        while(len--) sum[i]+=a[x=read()],s[i].push_back(x);
    }ss=sqrt(ss);
    for (int i=1;i<=m;++i){
        if (s[i].size()<=ss) continue;
        id[++tot]=i;
        for (int j=0;j<s[i].size();++j) ta[tot][s[i][j]]=1;
    }
    for (int i=1;i<=m;++i){
        for (int j=0;j<s[i].size();++j)
            for (int z=1;z<=tot;++z)
                if (ta[z][s[i][j]]) nm[i][z]++;
    }
    while(q--){
        char ch=gc();while(ch!='?'&&ch!='+') ch=gc();
        if (ch=='?'){
            int x=read();printf("%I64d\n",query(x));
        }
        if (ch=='+'){
            int x=read(),w=read();
            if (s[x].size()<=ss) change(x,w);else add[x]+=w;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值