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 ...
随机推荐
- Js event对象offsetX,pageX,screenX,clientX详解
平时在测量元素位置时难以确定,下面给出具体的event对象中的各种属性,以便日后使用. 检测相对于浏览器的位置:clientX和clientY 当鼠标事件发生时,鼠标相对于浏览器左上 ...
- 关于UITableViewAutomaticDimension的产生的bug
一.下面这句代码要想有作用 在iOS11之前需要适配,两个代理都需要实现 - (CGFloat)tableView:(UITableView *)tableView heightForHeader ...
- 2018-2-22-在-windows-安装-Jekyll
title author date CreateTime categories 在 windows 安装 Jekyll lindexi 2018-02-22 17:47:39 +0800 2018-2 ...
- Win32 API 多线程编程——一个简单实例(含消息参数传递)
Win32 API进行程序设计具有很多优点:应用程序执行代码小,运行效率高,但是他要求程序员编写的代码较多,且需要管理所有系统提供给程序的资源,要求程序员对Windows系统内核有一定的了解,会占用程 ...
- ECUST_Algorithm_2019_4
简要题意及解析 1001 第三次作业原题,略. 1002 把一个数转换为二进制. 不断除以\(2\)取余就好了.写递归代码会非常短. 时间复杂度\(O(Tlogn)\) \(T\)是数据组数,\(n\ ...
- OpenLayers API整理
整理的Openlayers 的知识笔记,随着运用不断加深理解,也会不断更新. 本文链接:Openlayers API整理 作者:狐狸家的鱼 GitHub:八至 一.创建地图 1.地图Map 创建地图底 ...
- nohup及crontab定时任务
一.关于nohup: 1.假设有一 tash.sh 启动是:nohup /home/spex/bin_prodDemo/task.sh & 一定 ...
- activemq消息中间件的依赖
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artif ...
- 企业实践 | 如何更好地使用 Apache Flink 解决数据计算问题?
业务数据的指数级扩张,数据处理的速度可不能跟不上业务发展的步伐.基于 Flink 的数据平台构建.运用 Flink 解决业务场景中的具体问题等随着 Flink 被更广泛的应用于广告.金融风控.实时 B ...
- MTD系统架构和yaffs2使用、Nandflash驱动设计
一.MTD系统架构 1.MTD设备体验 FLASH在嵌入式系统中是必不可少的,它是bootloader.linux内核和文件系统的最佳载体. 在Linux内核中引入了MTD子系统为NORFLASH和N ...