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 个数划分到 ...
随机推荐
- Struts2 (一)
1 三层架构 2 MVC框架的原理 3 什么是Struts2 Struts2是一个非常优秀的MVC框架,基于Model2设计模式. 是由传统的Struts1和WebWork两个经典的框架发展而来的. ...
- mysql 出现Duplicate entry ‘xxx’ for key ‘PRIMARY’,一个自增字段达到了上限,
mysql 出现Duplicate entry 'xxx' for key 'PRIMARY',一个自增字段达到了上限,
- sublime卡顿
sublime突然卡顿,输入字符要一两秒后才显示出来, 解决方法:首选项--插件控制--禁用插件 Git Gutter
- 【转】CString,string,char*综合比较
(一) 概述 1.string和CString均是字符串模a板类: 2.string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: 3.CString(typedef CString ...
- 取IP的几个方法
ifconfig eth0|grep " inet add"|cut -d":" -f2|cut -d " " -f1 ifconfig e ...
- javascript中的字符串对象和数组对象
1.javascript的对象的概念 在javascript中,除了null和undefined以处,其他的数据类型都被定义成了对象 也可以用创建对象的方法定义变量,string,math,array ...
- HTML5中的Web Notification桌面通知
大家在做一些浏览器端的聊天功能的时候,或者在一些网站跟在线客服咨询的时候,会看到一些消息通知的提示,常见的有浏览器标签页的闪烁和屏幕右侧的消息通知.本篇博客就在这里简单的介绍一下如何实现这样的功能. ...
- JMS基础篇
首先我们需要下载 ActiveMQ:http://activemq.apache.org/. 启动 ActiveMQ 服务:解包下载的 ActiveMQ >进去其bin 目录>双击 act ...
- oracle 字符串截取substr和instr
SUBSTR(string,start_position,[length]) 求子字符串,返回字符串解释:string 元字符串 start_position 开始位置(从0开始 ...
- Azure ARM (21) Azure订阅的两种管理模式
<Windows Azure Platform 系列文章目录> 熟悉Azure平台的读者都知道,Microsoft Azure服务管理,分为三个层次: 1.企业服务合同 (Enterpri ...