codeforces 348 C. Subset Sums (暴力+技巧)
2017-01-18 17:09
387 查看
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 denoteSk = {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 %I64dspecifier.
Examples
input
output
题解:暴力+技巧
两种操作:(1)将集合中每个元素对应位置的权值+x
(2)查询集合中每个元素对应位置的权值和。
设集合大小的和为n
我们将集合分成两类,定义大集合为集合中元素个数大于sqrt(n),小集合为集合中元素个数小于等于sqrt(n)
然后预处理出每个集合与每个大集合交集得到大小,因为大集合的数量不会超过sqrt(n),所以时间复杂度是O(nsqrt(n))
对于每个集合的和我们分成三部分计算:集合中元素初始值的和+小集合对该集合的影响+大集合对该集合的影响。
修改:
(1)小集合:我们将每个位置的val增加x,然后考虑这个小集合对于每个大集合的影响+num[i][j]*x (num[i][j]表示两者的交集大小)
(2)大集合:我们直接将大集合的影响标记+x
查询:
(1)小集合:将每个位置的权值累加,然后枚举每个大集合,计算大集合的delta对小集合的影响 +num[i][j]*delta[j]
(2)大集合:初始权值和+小集合对大集合的影响+大集合对该集合的影响。
这样均摊的时间辅助度是O(m sqrt(n))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100003
#define LL long long
using namespace std;
int n,m,q;
int point
,v
,next
,tot,size
;
LL sum
,delta
,tag
,val
;
int num
[400],mark
,a
,opt
,belong
;
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
}
int main()
{
freopen("a.in","r",stdin);
scanf("%d%d%d",&n,&m,&q);
for (int i=1;i<=n;i++) scanf("%I64d",&val[i]);
for (int i=1;i<=m;i++) {
scanf("%d",&size[i]); size[0]+=size[i];
for (int j=1;j<=size[i];j++){
int x; scanf("%d",&x);
add(i,x); sum[i]+=val[x];
}
}
int cnt=0;
for (int i=1;i<=m;i++)
if (size[i]>sqrt(size[0]*1.0)) a[++cnt]=i,opt[i]=1,belong[i]=cnt;
else opt[i]=0;
for (int i=1;i<=cnt;i++) {
memset(mark,0,sizeof(mark));
for (int j=point[a[i]];j;j=next[j])
mark[v[j]]=1;
for (int j=1;j<=m;j++) {
for (int k=point[j];k;k=next[k]) num[j][i]+=mark[v[k]];
}
}
/*for (int i=1;i<=m;i++) {
for (int j=1;j<=cnt;j++) cout<<num[i][j]<<" ";
cout<<endl;
}*/
for (int i=1;i<=q;i++) {
char s[10]; int x,y;
scanf("%s",s);
if (s[0]=='?') {
scanf("%d",&x);
LL ans=0;
if (opt[x]==0) {
for (int j=point[x];j;j=next[j]) ans+=val[v[j]];
for (int j=1;j<=cnt;j++)
ans+=delta[j]*num[x][j];
printf("%I64d\n",ans);
}
else {
for (int j=1;j<=cnt;j++) ans+=delta[j]*num[x][j];
printf("%I64d\n",ans+sum[x]+tag[belong[x]]);
}
}
else{
scanf("%d%d",&x,&y);
if (opt[x]==0) {
for (int j=point[x];j;j=next[j])
val[v[j]]+=y;
for (int j=1;j<=cnt;j++)
tag[j]+=y*num[x][j];
}
else {
delta[belong[x]]+=y;
}
}
}
}
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 denoteSk = {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 %I64dspecifier.
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
题解:暴力+技巧
两种操作:(1)将集合中每个元素对应位置的权值+x
(2)查询集合中每个元素对应位置的权值和。
设集合大小的和为n
我们将集合分成两类,定义大集合为集合中元素个数大于sqrt(n),小集合为集合中元素个数小于等于sqrt(n)
然后预处理出每个集合与每个大集合交集得到大小,因为大集合的数量不会超过sqrt(n),所以时间复杂度是O(nsqrt(n))
对于每个集合的和我们分成三部分计算:集合中元素初始值的和+小集合对该集合的影响+大集合对该集合的影响。
修改:
(1)小集合:我们将每个位置的val增加x,然后考虑这个小集合对于每个大集合的影响+num[i][j]*x (num[i][j]表示两者的交集大小)
(2)大集合:我们直接将大集合的影响标记+x
查询:
(1)小集合:将每个位置的权值累加,然后枚举每个大集合,计算大集合的delta对小集合的影响 +num[i][j]*delta[j]
(2)大集合:初始权值和+小集合对大集合的影响+大集合对该集合的影响。
这样均摊的时间辅助度是O(m sqrt(n))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100003
#define LL long long
using namespace std;
int n,m,q;
int point
,v
,next
,tot,size
;
LL sum
,delta
,tag
,val
;
int num
[400],mark
,a
,opt
,belong
;
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
}
int main()
{
freopen("a.in","r",stdin);
scanf("%d%d%d",&n,&m,&q);
for (int i=1;i<=n;i++) scanf("%I64d",&val[i]);
for (int i=1;i<=m;i++) {
scanf("%d",&size[i]); size[0]+=size[i];
for (int j=1;j<=size[i];j++){
int x; scanf("%d",&x);
add(i,x); sum[i]+=val[x];
}
}
int cnt=0;
for (int i=1;i<=m;i++)
if (size[i]>sqrt(size[0]*1.0)) a[++cnt]=i,opt[i]=1,belong[i]=cnt;
else opt[i]=0;
for (int i=1;i<=cnt;i++) {
memset(mark,0,sizeof(mark));
for (int j=point[a[i]];j;j=next[j])
mark[v[j]]=1;
for (int j=1;j<=m;j++) {
for (int k=point[j];k;k=next[k]) num[j][i]+=mark[v[k]];
}
}
/*for (int i=1;i<=m;i++) {
for (int j=1;j<=cnt;j++) cout<<num[i][j]<<" ";
cout<<endl;
}*/
for (int i=1;i<=q;i++) {
char s[10]; int x,y;
scanf("%s",s);
if (s[0]=='?') {
scanf("%d",&x);
LL ans=0;
if (opt[x]==0) {
for (int j=point[x];j;j=next[j]) ans+=val[v[j]];
for (int j=1;j<=cnt;j++)
ans+=delta[j]*num[x][j];
printf("%I64d\n",ans);
}
else {
for (int j=1;j<=cnt;j++) ans+=delta[j]*num[x][j];
printf("%I64d\n",ans+sum[x]+tag[belong[x]]);
}
}
else{
scanf("%d%d",&x,&y);
if (opt[x]==0) {
for (int j=point[x];j;j=next[j])
val[v[j]]+=y;
for (int j=1;j<=cnt;j++)
tag[j]+=y*num[x][j];
}
else {
delta[belong[x]]+=y;
}
}
}
}
相关文章推荐
- CodeForces 348 C.Subset Sums(分块)
- Codeforces 677E Vanya and Balloons【断点前缀和+暴力维护+数学技巧】
- CodeForces 348C Subset Sums nsqrtn的姿势。。
- codeforces 348C Subset Sums
- Codeforces 349E - Subset Sums
- 乱搞 [CodeForces 348C] Subset Sums nsqrtn
- CodeForces 348C Subset Sums(分块)(nsqrtn)
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
- Project Euler P105:Special subset sums: testing 特殊的子集和 检验
- CF348C Subset Sums
- USACO 2.2 Subset Sums (DP动态规划)
- 洛谷P1466 集合 Subset Sums(dp)
- 动规-Subset Sums
- 【CODEFORCES】 A. Dreamoon and Sums
- CodeForces - 538A暴力
- Codeforces 625B War of the Corporations 【暴力】
- CodeForces 597A Divisibility(技巧)
- 【CodeForces - 758C Unfair Poll】 暴力 + 模拟
- Codeforces 474C Captain Marmot 暴力+几何(绕定点旋转)
- USACO section2.2 Subset Sums题解&代码