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 ...
随机推荐
- GeneXus笔记本—获取当月的最后一天
首先获取当前日期 然后赋值为当前年月的第一天 然后加一个月 减去一天 就是当月最后一天 多用于筛选数据时的条件或者区间 我们先随便拉个页面 简单点就好 放入两个textblock 然后点击Even ...
- Linux面试基础(二)
Linux常用目录——存放 /bin 所有用户可以使用的可执行文件 /sbin 新管理员使用的执行文件 /boot Linux内核映像文件和与引导加载有关的文件 /dev 设备文件 /etc ...
- Redis的常用命令及数据类型
Redis支持的五种数据类型 字符串 (string) 字符串列表 (list) 散列 (hash) 字符串集合 (set) 有序字符串集合 (sorted-set) key(键) keys * 获取 ...
- 割点的tarjan算法模板
基本思路: 朴素的思想是删除每一个点,然后去dfs,这样无疑会爆炸 换一种思路,怎样判断是割点呢,如果是根节点的话毫无疑问只要看子树的数目,但是如果不是根节点呢,不知大牛是怎样想到的 利用两个数组df ...
- Django框架的学习
目前 Django 1.6.x 以上版本已经完全兼容 Python 3.x. 1. 指定django版本的安装 pip install django =1.11
- 【CSS】position(定位)属性
关于CSS position,来自MDN的描述: CSS position属性用于指定一个元素在文档中的定位方式.top.right.bottom.left 属性则决定了该元素的最终位置. 然后来看看 ...
- PHP filter_var_array() 函数
定义和用法 filter_var_array() 函数获取多个变量,并进行过滤. 该函数对过滤多个值很有用,无需重复调用 filter_var(). 如果成功,则以数组形式返回请求变量的值.如果失败, ...
- jmeter之-非GUI模式&登录实战
1.执行测试脚本 jmeter -n -t JMeter分布式测试示例.jmx 2.指定结果文件及日志路径 jmeter -n -t JMeter分布式测试示例.jmx -l report\01-re ...
- [bzoj3733]Iloczyn 题解(搜索剪枝)
3733: [Pa2013]Iloczyn Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 741 Solved: 217[Submit][Statu ...
- ELK问题处理
1.Logstash收集tomcat日志时报错warn: log4j:WARN No appenders could be found for logger (org.apache.http.clie ...