题意思路:https://www.cnblogs.com/jianrenfang/p/6502858.html

第一次见这种思路,对于集合大小分为两种类型,一种是重集合,一种是轻集合,对于重集合,我们维护这个集合加上的和,已经集合的和。对于轻集合,我们直接暴力在序列上加上和,以及把这种加和对重集合的影响加上。

代码:

  1. #include <bits/stdc++.h>
  2. #define LL long long
  3. using namespace std;
  4. const int maxn = 100010;
  5. int cnt[maxn][350];
  6. LL sum[maxn], add[maxn], a[maxn];
  7. int mp[350], tot;
  8. vector<int> s[maxn];
  9. bool is_big[maxn];
  10. int main() {
  11. int n, m, x, y, T;
  12. scanf("%d%d%d", &n, &m, &T);
  13. int block = sqrt(n);
  14. for (int i = 1; i <= n; i++) {
  15. scanf("%lld", &a[i]);
  16. }
  17. for (int i = 1; i <= m; i++) {
  18. scanf("%d", &x);
  19. while(x--) {
  20. scanf("%d", &y);
  21. s[i].push_back(y);
  22. }
  23. sort(s[i].begin(), s[i].end());
  24. if(s[i].size() >= block) {
  25. mp[++tot] = i;
  26. is_big[i] = 1;
  27. }
  28. }
  29. for (int i = 1; i <= n; i++) {
  30. for (int j = 1; j <= tot; j++) {
  31. int now = mp[j], l = 0, r = 0;
  32. for (; l < s[i].size(); l++) {
  33. while(r < s[now].size() && s[now][r] < s[i][l]) r++;
  34. if(s[now][r] == s[i][l]) cnt[i][j]++;
  35. }
  36. }
  37. }
  38. for (int i = 1; i <= tot; i++)
  39. for (int j = 0; j < s[mp[i]].size(); j++) {
  40. sum[mp[i]] += a[s[mp[i]][j]];
  41. }
  42. char str[3];
  43. while(T--) {
  44. scanf("%s", str + 1);
  45. if(str[1] == '+') {
  46. scanf("%d %d", &x, &y);
  47. if(is_big[x]) add[x] += y;
  48. else {
  49. for (int i = 0; i < s[x].size(); i++)
  50. a[s[x][i]] += y;
  51. for (int i = 1; i <= tot; i++)
  52. sum[mp[i]] += (LL)cnt[x][i] * y;
  53. }
  54. } else {
  55. LL ans = 0;
  56. scanf("%d", &x);
  57. if(is_big[x]) {
  58. ans += sum[x];
  59. for (int i = 1; i <= tot; i++) {
  60. ans += add[mp[i]] * (LL)cnt[x][i];
  61. }
  62. printf("%lld\n", ans);
  63. } else {
  64. for (int i = 0; i < s[x].size(); i++) {
  65. ans += a[s[x][i]];
  66. }
  67. for (int i = 1; i <= tot; i++)
  68. ans += add[mp[i]] * (LL)cnt[x][i];
  69. printf("%lld\n", ans);
  70. }
  71. }
  72. }
  73. }

  

Codeforces 348C Subset Sums 分块思想的更多相关文章

  1. CodeForces 348C Subset Sums(分块)(nsqrtn)

    C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Codeforces 348C - Subset Sums(根号分治)

    题面传送门 对于这类不好直接维护的数据结构,第一眼应该想到-- 根号分治! 我们考虑记[大集合]为大小 \(\geq\sqrt{n}\) 的集合,[小集合]为大小 \(<\sqrt{n}\) 的 ...

  3. Codeforces Round #319 (Div. 1)C. Points on Plane 分块思想

                                                                              C. Points on Plane On a pl ...

  4. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  5. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces348C - Subset Sums

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...

随机推荐

  1. java.nio.Buffer 中的 flip()方法

    在Java NIO编程中,对缓冲区操作常常需要使用  java.nio.Buffer中的 flip()方法. Buffer 中的 flip() 方法涉及到 Buffer 中的capacity.posi ...

  2. Linux学习笔记0-CentOS7关闭防火墙

    关闭防火墙 systemctl stop firewalld.service //停止firewall systemctl disable firewalld.service //禁止firewall ...

  3. line vty 0 4的意义

    VTY是路由器的远程登陆的虚拟端口,0 4表示可以同时打开5个会话,line vty 0 4是进入VTY端口,对VTY端口进行配置,比如说配置密码,或者ACL. 进入VTY 0 0 好象没什么实际操作 ...

  4. UOJ197 线性规划

    传送门 由于这道题标程GG了所以必不可能AC嘛2333 单纯形法是一个很玄学的东西qwq 就是 非标准型 -> 标准型 -> 规范型 -> 松弛型 一个玄学操作——转轴操作(priv ...

  5. List Comprehension ()(一)

    >>> L = [1,2,3,4,5] >>> L = [x+10 for x in L] >>> L [11, 12, 13, 14, 15] ...

  6. php操作redis--生存时间篇

    常用函数:expireAt,expire,ttl 应用场景:登陆生效时间,验证码有效时间等 设置某个键的有效时间,如当天有效(以时间戳的方式设置) $expireTime = mktime('23', ...

  7. springCloud配置(microServiceProvider)

    server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 typ ...

  8. yum工具入门

    一yum介绍 注意学完了yum之后,rpm的使用频率就少了.有些功能yum用起来不如rpm更方便. CentOS: yum, dnfYUM: Yellowdog Update Modifier,rpm ...

  9. mysql数据库优化学习

    目的避免出现页面访问错误慢查询造成页面无法加载阻塞造成数据无法提交优化从sql及索引,数据库结构,系统配置,硬件 日志慢查询日志:   show variables like 'slow_query_ ...

  10. mysql学习-join的使用

    sql的执行顺序是从from 开始 join图