链接

bzoj

思路

cdq入门题,拆成4个矩阵,然后cdq。

代码

  1. /**************************************************************
  2. Problem: 1176
  3. User: gryz2016
  4. Language: C++
  5. Result: Accepted
  6. Time:2652 ms
  7. Memory:13012 kb
  8. ****************************************************************/
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11. #define lowbit(x) (x&-x)
  12. const int N = 2e5 + 7;
  13. int read() {
  14. int x = 0, f = 1; char s = getchar();
  15. for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
  16. for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
  17. return x * f;
  18. }
  19. int n, a[N], ans[N];
  20. struct ask {
  21. int opt, x, y, w, id;
  22. ask(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) {
  23. opt = a, x = b, y = c, w = d, id = e;
  24. }
  25. bool operator < (const ask &b) const {
  26. return x == b.x ? opt < b.opt : x < b.x;
  27. }
  28. } Q[N], tmp[N];
  29. int lsh_y[N << 1];
  30. namespace BIT {
  31. int sum[N], maxn;
  32. void add(int id, int w) {
  33. for (int i = id; i <= maxn; i += lowbit(i)) sum[i] += w;
  34. }
  35. int query(int x) {
  36. int ans = 0;
  37. for (int i = x; i >= 1; i -= lowbit(i)) ans += sum[i];
  38. return ans;
  39. }
  40. }
  41. void cdq(int l, int r) {
  42. if (l == r) return;
  43. int mid = (l + r) >> 1;
  44. cdq(l, mid), cdq(mid + 1, r);
  45. int p = l, q = mid + 1, js = l;
  46. while (p <= mid && q <= r) {
  47. if (Q[p] < Q[q]) {
  48. if (Q[p].opt == 1) BIT::add(Q[p].y, Q[p].w);
  49. tmp[js++] = Q[p++];
  50. } else {
  51. if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
  52. tmp[js++] = Q[q++];
  53. }
  54. }
  55. if (p <= mid) {
  56. for (int i = l; i < p; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
  57. while (p <= mid) tmp[js++] = Q[p++];
  58. } else {
  59. while (q <= r) {
  60. if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
  61. tmp[js++] = Q[q++];
  62. }
  63. for (int i = l; i <= mid; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
  64. }
  65. for (int i = l; i <= r; ++i) Q[i] = tmp[i];
  66. }
  67. int main() {
  68. // freopen("a.in", "r", stdin);
  69. int S = read(), W = read(), n = 0, DSR = 0;
  70. while (233) {
  71. int opt = read();
  72. if (opt == 3) break;
  73. if (opt == 1) {
  74. int x = read(), y = read(), w = read();
  75. Q[++n] = ask(opt, x, y, w), lsh_y[++lsh_y[0]] = Q[n].y;
  76. } else {
  77. ++DSR;
  78. int a = read(), b = read(), x = read(), y = read();
  79. if (x && y) Q[++n] = ask(opt, x, y, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
  80. if (a - 1 && b - 1) Q[++n] = ask(opt, a - 1, b - 1, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
  81. if (a - 1 && y) Q[++n] = ask(opt, a - 1, y, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
  82. if (x && b - 1) Q[++n] = ask(opt, x, b - 1, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
  83. }
  84. }
  85. sort(lsh_y + 1, lsh_y + 1 + lsh_y[0]);
  86. lsh_y[0] = unique(lsh_y + 1, lsh_y + 1 + lsh_y[0]) - lsh_y - 1;
  87. for (int i = 1; i <= n; ++i) Q[i].y = lower_bound(lsh_y + 1, lsh_y + 1 + lsh_y[0], Q[i].y) - lsh_y;
  88. BIT::maxn = lsh_y[0] + 1;
  89. cdq(1, n);
  90. for (int i = 1; i <= DSR; ++i) printf("%d\n", ans[i]);
  91. return 0;
  92. }

bzoj1176: [Balkan2007]Mokia cdq的更多相关文章

  1. [BZOJ1176][Balkan2007]Mokia cdq+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3134  Solved: 1395[Submit][S ...

  2. BZOJ1176: [Balkan2007]Mokia CDQ分治

    最近很不对啊=w= 写程序全是bug啊 ans数组开小了竟然一直不知道,小数据没问题大数据拍不过,交上去RE 蛋疼半天 这个主要把每次询问拆成3个询问. #include<cstdio> ...

  3. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  4. BZOJ 1176[Balkan2007]Mokia(CDQ分治)

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3381  Solved: 1520[Submit][S ...

  5. bzoj1176: [Balkan2007]Mokia【cdq分治】

    把询问搞成4个,cdq分治. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #d ...

  6. BZOJ1176 [Balkan2007]Mokia 【CDQ分治】

    题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...

  7. BZOJ1176 [Balkan2007]Mokia(CDQ)

    CDQ裸题,\(x\), \(y\), \(tim\)三维偏序 #include <cstdio> #include <iostream> #include <cstri ...

  8. 2018.09.16 bzoj1176: [Balkan2007]Mokia(cdq分治)

    传送门 调了半天发现是输出优化打错了求心理阴影体积233 这题很简单啊. 一个修改操作x如果对一个询问操作y有贡献那么有. tx<ty,Xx<=Xy,Yx<=Yy" rol ...

  9. cdq分治入门--BZOJ1176: [Balkan2007]Mokia

    对w*w,w<=2000000的矩形,一开始全是0(或一开始全是s),n<=170000个操作,每次操作:矩阵内某点加上一个数,查某一个子矩阵的和,保证修改数<=160000,询问数 ...

随机推荐

  1. 运行带有Activiti modeler时,出现这样的报错: java.rmi.AccessException: Cannot modify this registry

    最近在做整合Activiti Modeler工作流在线设计器的工作,运行IDEA时,出现了这样一个错误信息: 原因及解决办法:   Activiti默认打开了jmx功能,默认端口为1099,idea中 ...

  2. SpringBoot @EnableAutoConfiguration exclude属性失效

    本文链接:https://blog.csdn.net/yuan_ren_sheng/article/details/81516779 在学习SpringBoot的时候,入了不少的坑.今天学习@Spri ...

  3. POI SXSSF API 导出1000万数据示例

    SXSSF是XSSF API的兼容流式扩展,在必须生成非常大的电子表格.并且堆空间有限时使用. SXSSF通过限制对滑动窗口内数据的访问实现低内存占用,而XSSF允许访问文档中的所有行. 不在窗口中的 ...

  4. Java自学-接口与继承 UML图

    UML 图 步骤 1 : UML 图 -- 类之间的关系 UML-Unified Module Language 统一建模语言,可以很方便的用于描述类的属性,方法,以及类和类之间的关系 步骤 2 : ...

  5. web基础运用

    目录 web框架 web应用本质 Web应用程序的优点 Web应用程序的缺点 BS架构优点 web框架的分类 web框架包含了三部分 web框架分类 Http协议 路由系统 自定制的web框架案例 w ...

  6. 浏览网页隐藏服务器IP

    host文件修改 notepad %windir%\system32\drivers\etc\hosts 目标IP localhost.autumn.com 可能会导致HTTP Status Code ...

  7. .Net Core 遇到 “'windows-1252' is not a supported encoding name.”

    最近用 iTextSharp 拆分 Pdf 文档 加水印的时候遇到错误: 'windows-1252' is not a supported encoding name. For informatio ...

  8. SQLi_Labs通关文档【1-65关】

    SQLi_Labs通关文档[1-65关] 为了不干扰自己本机环境,SQL-LAB我就用的码头工人,跑起来的,搭建也非常简单,也就两条命令 docker pull acgpiano/sqli-labs ...

  9. BeanPostProcessor后置处理器原理以及ApplicationListener原理

    BeanPostProcessor:bean后置处理器,bean创建对象初始化前后进行拦截工作的 1.BeanFactoryPostProcessor:BeanFactory的后置处理器; 在Bean ...

  10. Node初识

    初识Nodejs Node.js的诞生 作者Ryan Dahl 瑞恩·达尔 2004 纽约 读数学博士 2006 退学到智利 转向开发 2009.5对外宣布node项目,年底js大会发表演讲 2010 ...