Codeforces348C - Subset Sums
Description
给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq10^5)\),进行\(q(q\leq10^5)\)次操作:
- 询问下标属于集合\(S_k\)的所有数之和。
- 将下标属于集合\(S_k\)的所有数加\(x\)。
Solution
记\(N_0=\sqrt{\sum|S_i|}\)。
我们把集合划分成轻集合与重集合,大小超过\(N_0\)的集合就是重集合。容易知道重集合的个数不超过\(N_0\)。对于每个重集合,记录sum表示该集合的和,add表示该集合总体被加了的值,cnt[i]表示该集合与集合\(i\)的交集大小。
询问时,如果是重集合则输出sum;否则暴力求\(\{a\}\)上的和,再加上每个重集合对该轻集合的贡献add*cnt[k]。
修改时,如果是重集合则add+=x;否则暴力修改\(\{a\}\)。然后更新每个重集合的sum,也就是加上x*cnt[k]。
时间复杂度\(O(q\sqrt{\sum|S_i|})\)。
Code
//Subset Sums
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long lint;
inline int read()
{
int x=0,f=1; char ch=getchar();
while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=getchar();}
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int const N=1e5+1e3;
int const N0=400;
int n,m,q,n0; lint a[N];
int ori[N];
struct _set{int siz,id; vector<int> x;} s[N];
bool cmpSiz(_set x,_set y) {return x.siz>y.siz;}
bool tmp[N]; int cnt[N0][N]; lint add[N0],sum[N0];
int main()
{
n=read(),m=read(),q=read();
for(int i=1;i<=n;i++) a[i]=read();
int sumK=0;
for(int i=1;i<=m;i++)
{
s[i].siz=read(),s[i].id=i,s[i].x.push_back(0); sumK+=s[i].siz;
for(int p=1;p<=s[i].siz;p++) s[i].x.push_back(read());
}
sort(s+1,s+m+1,cmpSiz);
for(int i=1;i<=m;i++) ori[s[i].id]=i;
n0=sqrt(sumK); int cnt1=0;
for(int i=1;s[i].siz>=n0;i++) cnt1=i;
for(int i=1;i<=cnt1;i++)
{
memset(tmp,false,sizeof tmp);
for(int p=1;p<=s[i].siz;p++) sum[i]+=a[s[i].x[p]],tmp[s[i].x[p]]=true;
for(int j=1;j<=m;j++)
for(int p=1;p<=s[j].siz;p++) if(tmp[s[j].x[p]]) cnt[i][j]++;
}
for(int owo=1;owo<=q;owo++)
{
char opt; scanf("%c",&opt);
if(opt=='?')
{
int k=ori[read()];
if(k<=cnt1) printf("%lld\n",sum[k]);
else
{
lint res=0;
for(int p=1;p<=s[k].siz;p++) res+=a[s[k].x[p]];
for(int i=1;i<=cnt1;i++) res+=add[i]*cnt[i][k];
printf("%lld\n",res);
}
}
if(opt=='+')
{
int k=ori[read()]; lint v=read();
if(k<=cnt1) add[k]+=v;
else for(int p=1;p<=s[k].siz;p++) a[s[k].x[p]]+=v;
for(int i=1;i<=cnt1;i++) sum[i]+=v*cnt[i][k];
}
}
return 0;
}
P.S.
可以用vector<int>存储集合元素,直接开数组内存太大。
要开long long啊啊啊啊啊!
Codeforces348C - Subset Sums的更多相关文章
- 洛谷P1466 集合 Subset Sums
P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 348C Subset Sums(分块)(nsqrtn)
C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- DP | Luogu P1466 集合 Subset Sums
题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...
- spoj-SUBSUMS - Subset Sums
SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...
- 洛谷 P1466 集合 Subset Sums Label:DP
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- 【USACO 2.2】Subset Sums (DP)
N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...
随机推荐
- JS题目
1.请你谈谈Cookie的弊端 cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的. 第一:每个特定的域名下最多生成20个cookie 1.IE6或更低版本最 ...
- jquery判断数据类型和相同字符串不相等
typeof object返回object对象数据类型 encodeURIComponent(str)//可把字符串作为URI 组件进行编码. 若str1和str2字符串数值相同,encodeURIC ...
- maven核心概念--插件和目标
maven的核心概念有很多,我们这里先来看Plugins和Goals. 在上一篇博客中,我们用两种类型的命令行参数运行了Maven. 第一条命令,create.这是一条单个的插件目标,Archetyp ...
- WebSphere--安装与配置
对于任何软件,都需要一些计划和具体步骤以确保成功安装.对于安装与配制 WebSphere应用服务器及其组件也是如此.下面介绍在Windows NT 上安装与配置WebSphere应用服务器 1 ...
- MySQL中group_concat()函数的排序方法
group_concat()函数的参数是可以直接使用order by排序的.666..下面通过例子来说明,首先看下面的t1表. 比如,我们要查看每个人的多个分数,将该人对应的多个分数显示在一起,分数要 ...
- Navicat for MySQL导出表结构脚本的方法
使用MySQL可视化工具Navicat导出MySQL的表结构脚本的方法. 1.右键Navicat中的数据库→数据传输(Data Transfer). 2.左边数据库对象(Database Object ...
- spring使用redis做缓存
缓存 什么是缓存? 在高并发下,为了提高访问的性能,需要将数据库中 一些经常展现和不会频繁变更的数据,存放在存取速率更快的内存中.这样可以 降低数据的获取时间,带来更好的体验 减轻数据库的压力 缓存适 ...
- javascript中的BOM对象
1.window对象 所有的浏览器都支持window对象 概念上讲,一个html文档对应一个window对象 功能上讲,控制浏览器窗口 使用上讲,window对象不需要创建对象,直接使用 2.wind ...
- cdh版本的hue安装配置部署以及集成hadoop hbase hive mysql等权威指南
hue下载地址:https://github.com/cloudera/hue hue学习文档地址:http://archive.cloudera.com/cdh5/cdh/5/hue-3.7.0-c ...
- 使用react-router实现单页面应用路由
这是Webpack+React系列配置过程记录的第二篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...