[hdu5213]容斥原理+莫队算法
题意:给一个序列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))。
- #pragma comment(linker, "/STACK:10240000,10240000")
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- #include <map>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <cctype>
- #include <set>
- #include <bitset>
- #include <functional>
- #include <numeric>
- #include <stdexcept>
- #include <utility>
- using namespace std;
- #define mem0(a) memset(a, 0, sizeof(a))
- #define mem_1(a) memset(a, -1, sizeof(a))
- #define lson l, m, rt << 1
- #define rson m + 1, r, rt << 1 | 1
- #define define_m int m = (l + r) >> 1
- #define rep_up0(a, b) for (int a = 0; a < (b); a++)
- #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
- #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
- #define rep_down1(a, b) for (int a = b; a > 0; a--)
- #define all(a) (a).begin(), (a).end()
- #define lowbit(x) ((x) & (-(x)))
- #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) {}
- #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
- #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
- #define pchr(a) putchar(a)
- #define pstr(a) printf("%s", a)
- #define sstr(a) scanf("%s", a)
- #define sint(a) scanf("%d", &a)
- #define sint2(a, b) scanf("%d%d", &a, &b)
- #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
- #define pint(a) printf("%d\n", a)
- #define test_print1(a) cout << "var1 = " << a << endl
- #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
- #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
- #define mp(a, b) make_pair(a, b)
- #define pb(a) push_back(a)
- typedef long long LL;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- const int dx[] = {, , -, , , , -, -};
- const int dy[] = {-, , , , , -, , - };
- const int maxn = 3e4 + ;
- const int md = ;
- const int inf = 1e9 + ;
- const LL inf_L = 1e18 + ;
- const double pi = acos(-1.0);
- const double eps = 1e-;
- template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
- template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
- template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
- template<class T>T condition(bool f, T a, T b){return f?a:b;}
- template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
- int make_id(int x, int y, int n) { return x * n + y; }
- struct Node {
- int L, R, id, kth;
- constructInt4(Node, L, R, id, kth);
- };
- Node node[maxn * ];
- int block;
- int a[maxn], ans[maxn], cnt[maxn];
- bool cmp(const Node a, const Node b) {
- int lb = a.L / block, rb = b.L / block;
- return lb < rb || lb == rb && a.R < b.R;
- }
- int main() {
- //freopen("in.txt", "r", stdin);
- int n, m, k;
- while (cin >> n) {
- cin >> k;
- rep_up0(i, n) {
- sint(a[i]);
- }
- cin >> m;
- rep_up0(i, m) {
- int L, R, U, V;
- scanf("%d %d %d %d", &L, &R, &U, &V);
- L --; R --; U --; V --;
- node[i * ] = Node(L, V, i, );
- node[i * + ] = Node(R + , U - , i, );
- node[i * + ] = Node(L, U - , i, );
- node[i * + ] = Node(R + , V, i, );
- }
- block = (int)sqrt(n + 0.5);
- sort(node, node + * m, cmp);
- int cur_ans = , L = , R = -;
- mem0(cnt);
- mem0(ans);
- rep_up0(i, * m) {
- Node query = node[i];
- while (L < query.L) {
- cnt[a[L]] --;
- if (k > a[L] && k - a[L] <= n) cur_ans -= cnt[k - a[L]];
- L ++;
- }
- while (R > query.R) {
- cnt[a[R]] --;
- if (k > a[R] && k - a[R] <= n) cur_ans -= cnt[k - a[R]];
- R --;
- }
- while (L > query.L) {
- L --;
- if (k > a[L] && k - a[L] <= n) cur_ans += cnt[k - a[L]];
- cnt[a[L]] ++;
- }
- while (R < query.R) {
- R ++;
- if (k > a[R] && k - a[R] <= n) cur_ans += cnt[k - a[R]];
- cnt[a[R]] ++;
- }
- if (query.kth < ) ans[query.id] += cur_ans;
- else ans[query.id] -= cur_ans;
- }
- rep_up0(i, m) {
- printf("%d\n", ans[i]);
- }
- }
- return ;
- }
[hdu5213]容斥原理+莫队算法的更多相关文章
- HDU5213(容斥定理+莫队算法)
传送门 题意 给出n个数和幸运数k,m次询问,每次询问[l1,r1]和[l2,r2]有多少对数满足x+y=k,x∈[l1,r1],y∈[l2,r2] 分析 看到m只有3e4,可以考虑\(m\sqrt{ ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
随机推荐
- 前端学习笔记-JavaScript
js引入方式: 1.嵌入js的方式:直接在页内的script标签内书写js功能代码. <script type="text/javascript">alert('hel ...
- xshell下使用vim的编辑一个文件Ctrl+S和Ctrl+Q
xshell下使用vim的编辑一个文件,保存的时候习惯性的按了Ctrl+S 结构悲剧了.屏幕锁死了.按其他键都没有反应,exc也不行. 经过问度娘才知道. 原来Ctrl+S在Linux里,是锁定屏幕的 ...
- python3如何不生成pyc文件
使用-B参数 即 python3 -B test.py 设置环境变量 export PYTHONDONTWRITEBYTECODE=1 在导入的地方增加 import sys sys.dont_wri ...
- Python 实用冷门知识整理
1.print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设置 ...
- 理解java容器底层原理--手动实现ArrayList
为了照顾初学者,我分几分版本发出来 版本一:基础版本 实现对象创建.元素添加.重新toString() 方法 package com.xzlf.collection; /** * 自定义一个Array ...
- opencv-5-图像遍历与图像改变
opencv-5-图像遍历与图像改变 opencvc++qt 目录 目录 开始 图像的像素点访问与遍历 opencv 座标定义 下标访问 指针访问 迭代器法访问 遍历访问时间对比 图像操作 图像叠加 ...
- java中FutureTask的使用
文章目录 FutureTask简介 Callable和Runnable的转换 以Runnable运行 java中FutureTask的使用 FutureTask简介 FutureTask是java 5 ...
- SpringBoot 集成Swagger2自动生成文档和导出成静态文件
目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...
- iOS逆向之一 工具的安装和使用
iOS逆向之一-工具的安装和使用 最近在学习iOS安全方面的技术,有些东西就记录下来了,所有有了这篇文章.顺便也上传了DEMO,可以再这里找到这些DEMO的源码:dhar/iOSReProject 越 ...
- 面试题总结-Java部分
1 集合 1.1 hashmap原理 HashMap是基于哈希表实现的,每一个元素是一个key-value对,实现了Serializable.Cloneable接口,允许使用null值和null键.不 ...