题意:给一个序列a,以及K,有Q个询问,每个询问四个数,L,R,U,V, 求L<=i<=R,U<=j<=V,a[i]+a[j]=K的(i, j)对数(题目保证了L <= R < U <= V)。

思路:首先用容斥原理把询问变为i,j同区间,记f(A,B)为答案,'+'为区间的并,A=[L,R],B=[U,V],C=[u+1,v-1],则f(A,B) = f(A+B+C,A+B+C)+f(C,C)-f(A+C,A+C)-f(B+C,B+C)。令g(L,R) = f([L,R],[L,R]) = f(A,A)。对于g函数区间变化为1时,可以做到o(1)的维护。用莫队算法的知识把询问排个序,按顺序维护当前区间的答案,同时更新最后答案。

总结:莫队算法并不是某一具体问题的解法,而是一种通用算法,对于任意无修改的区间统计问题,都可以用莫队算法试试。对于所有询问(L,R),复杂度等于sigma(|Li - Li-1| + |Ri - Ri-1|)*o(x),o(x)是区间变化为1时的维护代价,而前面的sigma用莫队算法的知识可以降低到o(m*sqrt(n))。

  1. #pragma comment(linker, "/STACK:10240000,10240000")
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <map>
  9. #include <queue>
  10. #include <deque>
  11. #include <cmath>
  12. #include <vector>
  13. #include <ctime>
  14. #include <cctype>
  15. #include <set>
  16. #include <bitset>
  17. #include <functional>
  18. #include <numeric>
  19. #include <stdexcept>
  20. #include <utility>
  21.  
  22. using namespace std;
  23.  
  24. #define mem0(a) memset(a, 0, sizeof(a))
  25. #define mem_1(a) memset(a, -1, sizeof(a))
  26. #define lson l, m, rt << 1
  27. #define rson m + 1, r, rt << 1 | 1
  28. #define define_m int m = (l + r) >> 1
  29. #define rep_up0(a, b) for (int a = 0; a < (b); a++)
  30. #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
  31. #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
  32. #define rep_down1(a, b) for (int a = b; a > 0; a--)
  33. #define all(a) (a).begin(), (a).end()
  34. #define lowbit(x) ((x) & (-(x)))
  35. #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
  36. #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
  37. #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
  38. #define pchr(a) putchar(a)
  39. #define pstr(a) printf("%s", a)
  40. #define sstr(a) scanf("%s", a)
  41. #define sint(a) scanf("%d", &a)
  42. #define sint2(a, b) scanf("%d%d", &a, &b)
  43. #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
  44. #define pint(a) printf("%d\n", a)
  45. #define test_print1(a) cout << "var1 = " << a << endl
  46. #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
  47. #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
  48. #define mp(a, b) make_pair(a, b)
  49. #define pb(a) push_back(a)
  50.  
  51. typedef long long LL;
  52. typedef pair<int, int> pii;
  53. typedef vector<int> vi;
  54.  
  55. const int dx[] = {, , -, , , , -, -};
  56. const int dy[] = {-, , , , , -, , - };
  57. const int maxn = 3e4 + ;
  58. const int md = ;
  59. const int inf = 1e9 + ;
  60. const LL inf_L = 1e18 + ;
  61. const double pi = acos(-1.0);
  62. const double eps = 1e-;
  63.  
  64. template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
  65. template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
  66. template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
  67. template<class T>T condition(bool f, T a, T b){return f?a:b;}
  68. template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
  69. int make_id(int x, int y, int n) { return x * n + y; }
  70.  
  71. struct Node {
  72. int L, R, id, kth;
  73. constructInt4(Node, L, R, id, kth);
  74. };
  75. Node node[maxn * ];
  76.  
  77. int block;
  78. int a[maxn], ans[maxn], cnt[maxn];
  79.  
  80. bool cmp(const Node a, const Node b) {
  81. int lb = a.L / block, rb = b.L / block;
  82. return lb < rb || lb == rb && a.R < b.R;
  83. }
  84.  
  85. int main() {
  86. //freopen("in.txt", "r", stdin);
  87. int n, m, k;
  88. while (cin >> n) {
  89. cin >> k;
  90. rep_up0(i, n) {
  91. sint(a[i]);
  92. }
  93. cin >> m;
  94. rep_up0(i, m) {
  95. int L, R, U, V;
  96. scanf("%d %d %d %d", &L, &R, &U, &V);
  97. L --; R --; U --; V --;
  98. node[i * ] = Node(L, V, i, );
  99. node[i * + ] = Node(R + , U - , i, );
  100. node[i * + ] = Node(L, U - , i, );
  101. node[i * + ] = Node(R + , V, i, );
  102. }
  103.  
  104. block = (int)sqrt(n + 0.5);
  105. sort(node, node + * m, cmp);
  106.  
  107. int cur_ans = , L = , R = -;
  108. mem0(cnt);
  109. mem0(ans);
  110.  
  111. rep_up0(i, * m) {
  112. Node query = node[i];
  113. while (L < query.L) {
  114. cnt[a[L]] --;
  115. if (k > a[L] && k - a[L] <= n) cur_ans -= cnt[k - a[L]];
  116. L ++;
  117. }
  118. while (R > query.R) {
  119. cnt[a[R]] --;
  120. if (k > a[R] && k - a[R] <= n) cur_ans -= cnt[k - a[R]];
  121. R --;
  122. }
  123. while (L > query.L) {
  124. L --;
  125. if (k > a[L] && k - a[L] <= n) cur_ans += cnt[k - a[L]];
  126. cnt[a[L]] ++;
  127. }
  128. while (R < query.R) {
  129. R ++;
  130. if (k > a[R] && k - a[R] <= n) cur_ans += cnt[k - a[R]];
  131. cnt[a[R]] ++;
  132. }
  133. if (query.kth < ) ans[query.id] += cur_ans;
  134. else ans[query.id] -= cur_ans;
  135. }
  136.  
  137. rep_up0(i, m) {
  138. printf("%d\n", ans[i]);
  139. }
  140. }
  141. return ;
  142. }

[hdu5213]容斥原理+莫队算法的更多相关文章

  1. HDU5213(容斥定理+莫队算法)

    传送门 题意 给出n个数和幸运数k,m次询问,每次询问[l1,r1]和[l2,r2]有多少对数满足x+y=k,x∈[l1,r1],y∈[l2,r2] 分析 看到m只有3e4,可以考虑\(m\sqrt{ ...

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  4. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  5. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  6. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  7. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  8. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  9. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

随机推荐

  1. 前端学习笔记-JavaScript

    js引入方式: 1.嵌入js的方式:直接在页内的script标签内书写js功能代码. <script type="text/javascript">alert('hel ...

  2. xshell下使用vim的编辑一个文件Ctrl+S和Ctrl+Q

    xshell下使用vim的编辑一个文件,保存的时候习惯性的按了Ctrl+S 结构悲剧了.屏幕锁死了.按其他键都没有反应,exc也不行. 经过问度娘才知道. 原来Ctrl+S在Linux里,是锁定屏幕的 ...

  3. python3如何不生成pyc文件

    使用-B参数 即 python3 -B test.py 设置环境变量 export PYTHONDONTWRITEBYTECODE=1 在导入的地方增加 import sys sys.dont_wri ...

  4. Python 实用冷门知识整理

    1.print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设置 ...

  5. 理解java容器底层原理--手动实现ArrayList

    为了照顾初学者,我分几分版本发出来 版本一:基础版本 实现对象创建.元素添加.重新toString() 方法 package com.xzlf.collection; /** * 自定义一个Array ...

  6. opencv-5-图像遍历与图像改变

    opencv-5-图像遍历与图像改变 opencvc++qt 目录 目录 开始 图像的像素点访问与遍历 opencv 座标定义 下标访问 指针访问 迭代器法访问 遍历访问时间对比 图像操作 图像叠加 ...

  7. java中FutureTask的使用

    文章目录 FutureTask简介 Callable和Runnable的转换 以Runnable运行 java中FutureTask的使用 FutureTask简介 FutureTask是java 5 ...

  8. SpringBoot 集成Swagger2自动生成文档和导出成静态文件

    目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...

  9. iOS逆向之一 工具的安装和使用

    iOS逆向之一-工具的安装和使用 最近在学习iOS安全方面的技术,有些东西就记录下来了,所有有了这篇文章.顺便也上传了DEMO,可以再这里找到这些DEMO的源码:dhar/iOSReProject 越 ...

  10. 面试题总结-Java部分

    1 集合 1.1 hashmap原理 HashMap是基于哈希表实现的,每一个元素是一个key-value对,实现了Serializable.Cloneable接口,允许使用null值和null键.不 ...