「题解」Codeforces 348C Subset Sums
do_while_true · · 题解
默认
大小
可以提前
对于每个大集合,直接记录
对于大集合的修改:
对于小集合的修改:给原数组对应处的位置加上数,然后也是将
对于大集合的询问:大集合和小集合的修改都已经修改到了
对于小集合的询问:首先
综上所述,复杂度为
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<unordered_map>
#define pb emplace_back
#define mp std::make_pair
#define fi first
#define se second
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;
typedef std::vector<int> vi;
typedef std::vector<ll> vll;
const ll mod = 998244353;
ll Add(ll x, ll y) { return (x+y>=mod) ? (x+y-mod) : (x+y); }
ll Mul(ll x, ll y) { return x * y % mod; }
ll Mod(ll x) { return x < 0 ? (x + mod) : (x >= mod ? (x-mod) : x); }
ll cadd(ll &x, ll y) { return x = (x+y>=mod) ? (x+y-mod) : (x+y); }
template <typename T> T Max(T x, T y) { return x > y ? x : y; }
template <typename T> T Min(T x, T y) { return x < y ? x : y; }
template <typename T>
T &read(T &r) {
r = 0; bool w = 0; char ch = getchar();
while(ch < '0' || ch > '9') w = ch == '-' ? 1 : 0, ch = getchar();
while(ch >= '0' && ch <= '9') r = r * 10 + (ch ^ 48), ch = getchar();
return r = w ? -r : r;
}
const int N = 100010;
const int B = 316;
int n, m, q;
int siz[N];
vi s[N];
ll w[N], v[N], a[N];
int vis[N], cnt[B+10][N], pos[N], rk[N], pct;
void calc(int p) {
for(auto x : s[p]) vis[x] = 1, w[p] += a[x];
for(int i = 1; i <= m; ++i)
for(auto x : s[i])
if(vis[x])
++cnt[pos[p]][i];
for(auto x : s[p]) vis[x] = 0;
}
signed main() {
read(n); read(m); read(q);
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i <= m; ++i) {
read(siz[i]);
for(int j = 1, x; j <= siz[i]; ++j) read(x), s[i].pb(x);
}
for(int i = 1; i <= m; ++i)
if(siz[i] > B)
pos[i] = ++pct, rk[pct] = i, calc(i);
for(int o = 1; o <= q; ++o) {
char ch = getchar(); int k, x;
while(ch != '?' && ch != '+') ch = getchar();
if(ch == '+') {
read(k); read(x);
if(siz[k] > B) {
v[k] += x;
for(int i = 1; i <= pct; ++i)
w[rk[i]] += 1ll * cnt[i][k] * x;
}
else {
for(auto i : s[k]) a[i] += x;
for(int i = 1; i <= pct; ++i)
w[rk[i]] += 1ll * cnt[i][k] * x;
}
}
else {
read(k);
if(siz[k] > B) printf("%lld\n", w[k]);
else {
ll ans = 0;
for(auto i : s[k]) ans += a[i];
for(int i = 1; i <= pct; ++i)
ans += v[rk[i]] * cnt[i][k];
printf("%lld\n", ans);
}
}
}
return 0;
}