Codeforces 348C Subset Sums 分块思想
题意思路:https://www.cnblogs.com/jianrenfang/p/6502858.html
第一次见这种思路,对于集合大小分为两种类型,一种是重集合,一种是轻集合,对于重集合,我们维护这个集合加上的和,已经集合的和。对于轻集合,我们直接暴力在序列上加上和,以及把这种加和对重集合的影响加上。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100010;
int cnt[maxn][350];
LL sum[maxn], add[maxn], a[maxn];
int mp[350], tot;
vector<int> s[maxn];
bool is_big[maxn];
int main() {
int n, m, x, y, T;
scanf("%d%d%d", &n, &m, &T);
int block = sqrt(n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
while(x--) {
scanf("%d", &y);
s[i].push_back(y);
}
sort(s[i].begin(), s[i].end());
if(s[i].size() >= block) {
mp[++tot] = i;
is_big[i] = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= tot; j++) {
int now = mp[j], l = 0, r = 0;
for (; l < s[i].size(); l++) {
while(r < s[now].size() && s[now][r] < s[i][l]) r++;
if(s[now][r] == s[i][l]) cnt[i][j]++;
}
}
}
for (int i = 1; i <= tot; i++)
for (int j = 0; j < s[mp[i]].size(); j++) {
sum[mp[i]] += a[s[mp[i]][j]];
}
char str[3];
while(T--) {
scanf("%s", str + 1);
if(str[1] == '+') {
scanf("%d %d", &x, &y);
if(is_big[x]) add[x] += y;
else {
for (int i = 0; i < s[x].size(); i++)
a[s[x][i]] += y;
for (int i = 1; i <= tot; i++)
sum[mp[i]] += (LL)cnt[x][i] * y;
}
} else {
LL ans = 0;
scanf("%d", &x);
if(is_big[x]) {
ans += sum[x];
for (int i = 1; i <= tot; i++) {
ans += add[mp[i]] * (LL)cnt[x][i];
}
printf("%lld\n", ans);
} else {
for (int i = 0; i < s[x].size(); i++) {
ans += a[s[x][i]];
}
for (int i = 1; i <= tot; i++)
ans += add[mp[i]] * (LL)cnt[x][i];
printf("%lld\n", ans);
}
}
}
}
Codeforces 348C Subset Sums 分块思想的更多相关文章
- CodeForces 348C Subset Sums(分块)(nsqrtn)
C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 348C - Subset Sums(根号分治)
题面传送门 对于这类不好直接维护的数据结构,第一眼应该想到-- 根号分治! 我们考虑记[大集合]为大小 \(\geq\sqrt{n}\) 的集合,[小集合]为大小 \(<\sqrt{n}\) 的 ...
- Codeforces Round #319 (Div. 1)C. Points on Plane 分块思想
C. Points on Plane On a pl ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 洛谷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 ...
- Codeforces348C - Subset Sums
Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...
随机推荐
- Web前端基础学习-2
盒子模型 在页面中,我们将所有的元素全部看做是一个盒子,页面布局就是将大大小小不同的盒子堆砌在一起,而一个盒子由以下几部分组成: padding:内边距,内容到边框的距离: margin:外边距,其他 ...
- Linux: 给右键菜单加一个“转换图片为jpg格式”
Linux上通常都会安装imagemagick这个小巧但又异常强大的工具.这个软件提供了一系列很好用的功能.这里说一说如何使用它的convert命令转换图片为jpg格式,以及如何把它添加到Thunar ...
- 10.ThreadLocal
/** * - void set(Object value)设置当前线程的线程局部变量的值. * - public Object get()该方法返回当前线程所对应的线程局部变量. * - publi ...
- vue 路由动态传参 (多个)
动态传参 传值页面 客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面 客户详情CustomerDetails.vue 通过this.$router.para ...
- 阿里云HBase推出普惠性高可用服务,独家支持用户的自建、混合云环境集群
HBase可以支持百TB数据规模.数百万QPS压力下的毫秒响应,适用于大数据背景下的风控和推荐等在线场景.阿里云HBase服务了多家金融.广告.媒体类业务中的风控和推荐,持续的在高可用.低延迟.低成本 ...
- shell脚本学习(2)查找
1 grep 用法: grep -F fa 找含有fa字符的字符串 yuyuyu@ubuntu:~$ grep -F fa < do.txt grep -i fa 忽略大 ...
- 函数————count
count和count_if函数是计数函数,先来看一下count函数:count函数的功能是:统计容器中等于value元素的个数. count(first,last,value); first是容器的 ...
- ThinkPHP示例:图片上传
ThinkPHP示例之图片上传,包括图片上传.数据库保存.缩略图生成以及图片水印功能演示.首先需要下载框架核心,然后把示例解压到Web根目录下面,并修改入口文件中的框架入口文件的位置.导入示例目录下面 ...
- 2018icpc南京/gym101981 I Magic Potion
题意: 若干个勇士,每个勇士只能杀特定的怪物.每个勇士只能杀1个怪,但是有一些药,喝了药之后能再杀一个,每个勇士只能喝一瓶药.问你最多杀多少怪. 题解: 按照如下建图套网络流板即可. 网上有题解说套D ...
- 深入理解dijkstra+堆优化
深入理解dijkstra+堆优化 其实就这几种代码几种结构,记住了完全就可以举一反三,所以多记多练多优化多思考. Dijkstra 对于一个有向图或无向图,所有边权为正(边用邻接矩阵的形式给出), ...