Description

求区间内有多少对 \((i,j)\) 满足 \(|a_i - a_j| \leq k\)

Solution

可以莫队做(万能的莫队)

只需要考虑加入一个数会产生多少贡献即可

离散化的时候把 \(a_i,a_i - k, a_i+k\) 全部放进去。

加入一个数的时候只需要维护 \([a_i - k,a_i+k]\) 有多少个数,并且把 \(a_i\) 这个位置加上 1

删除亦然。这个可以用树状数组方便地维护。

具体实现的时候,因为树状数组是 sum(r) - sum(l-1) ,所以可以直接把 \(a_i,a_i - k-1, a_i+k\) 放进去离散化,求贡献就不用 -1 了

总复杂度 \(O(n \sqrt n \log n)\)

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
int n, k, q, blo, c[N * 2], now, ans[N], tmp[2 * N], MX;
struct Query {
int l, r, id;
inline bool operator < (const Query &x) const {
return l / blo == x.l / blo ? r < x.r : l / blo < x.l / blo;
}
} Q[N];
struct node {
int id, lk, rk, val, se;
} a[N];
inline int lb(int x) { return x & (-x); }
inline void add(int x, int d) {
for(int i = x; i <= MX; i += lb(i))
c[i] += d;
}
inline int sum(int x) {
int ret = 0;
for(int i = x; i; i -= lb(i))
ret += c[i];
return ret;
}
inline void ADD(int x) {
now += sum(a[x].rk) - sum(a[x].lk);
add(a[x].val, 1);
}
inline void DEL(int x) {
add(a[x].val, -1);
now -= sum(a[x].rk) - sum(a[x].lk);
}
int main() { int cnt = 0;
scanf("%d %d %d", &n, &k, &q); blo = sqrt(q);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i].val);
a[i].lk = a[i].val - k - 1, a[i].rk = a[i].val + k;
tmp[++cnt] = a[i].lk;
tmp[++cnt] = a[i].rk;
tmp[++cnt] = a[i].val;
} sort(tmp + 1, tmp + cnt + 1);
int len = unique(tmp + 1, tmp + cnt + 1) - tmp - 1;
for(int i = 1; i <= n; i++) {
a[i].val = lower_bound(tmp + 1, tmp + len + 1, a[i].val) - tmp;
a[i].lk = lower_bound(tmp + 1, tmp + len + 1, a[i].lk) - tmp;
a[i].rk = lower_bound(tmp + 1, tmp + len + 1, a[i].rk) - tmp;
MX = max(a[i].rk, MX);
}
for(int i = 1; i <= q; i++) {
scanf("%d %d", &Q[i].l, &Q[i].r); Q[i].id = i;
Q[i].l++, Q[i].r++;
} sort(Q + 1, Q + q + 1);
int L = 1, R = 0;
for(int i = 1; i <= q; i++) {
int l = Q[i].l, r = Q[i].r;
while(L > l) ADD(--L);
while(R < r) ADD(++R);
while(L < l) DEL(L++);
while(R > r) DEL(R--);
ans[Q[i].id] = now;
}
for(int i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}

题解【51nod 1290 Counting Diff Pairs】的更多相关文章

  1. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  2. 51nod 1290 Counting Diff Pairs 莫队 + bit

    一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[j]中,有多少对数,abs(A[i] - A[j]) <= K(abs表示绝对值) ...

  3. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  4. leetcode 题解 || Swap Nodes in Pairs 问题

    problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...

  5. LeetCode题解之Swap Nodes in Pairs

    1.题目描述 2.问题分析 对两个节点进行交换操作 3.代码 ListNode* swapPairs(ListNode* head) { if( !head || head->next == N ...

  6. leetcode个人题解——#24 Swap Nodes in Pairs

    因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...

  7. 【题解】CF#403 D-Beautiful Pairs of Numbers

    这题还挺对胃口的哈哈~是喜欢的画风!回家路上一边听歌一边想到的解法,写出来记录一下…… 首先,由于 \(b_{k} < a_{k + 1}\) ,所以我们可以看作是在一个长度为 n 的序列上选择 ...

  8. 题解 51nod 1597 有限背包计数问题

    题目传送门 题目大意 给出 \(n\),第 \(i\) 个数有 \(i\) 个,问凑出 \(n\) 的方案数. \(n\le 10^5\) 思路 呜呜呜,傻掉了... 首先想到根号分治,分别考虑 \( ...

  9. Counting The Important Pairs CodeChef - TAPAIR

    https://vjudge.net/problem/CodeChef-TAPAIR 合法的删除方法: 第一种:桥边与其余任意边(1)桥*(桥-1)/2(两条桥边)(2)桥*(m-桥)(桥边+其他边) ...

随机推荐

  1. 使用谷歌浏览器调试WEB前端的一些必备调试技巧

    转载:http://www.techug.com/post/chrome-debug-tips.html Chrome的开发者工具是个很强大的东西,相信程序员们都不会陌生,不过有些小功能可能并不为大众 ...

  2. 以符合人类阅读的方式打印php数组【转载】

    在程序开发过程中:打印数据进行查看调试是非常频繁的:如果没有一种易于阅读的样式那是相当痛苦的: 先定义一个数组: 1 2 3 4 5 6 7 8 9 $array=array(     't0'=&g ...

  3. 5233杨光--Linux第二次实验

    实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 若不小心登出后,直接刷新页面即可 2. 环境使用 完成实验后可以点击桌面上方的“实验截图”保存并分享实 ...

  4. BNUOJ 52305 Around the World 树形dp

    题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52305 Around the World Time Limit: 20000msMemory ...

  5. 2017 Summary

    几门课 基础电路与电子学 知道了一些二极管三极管的基本基本很基本的那种物理知识吧,但是毕竟我是从电信转专业过来的,所以说我内心就是逃避模电这样的课的.上课基本没听,后面只是死命复习了一周,考的还可以. ...

  6. PythonWeb 服务部署文档及迁移到Linux相关

    pythonWeb的部署(Django+Uwsgi): 1. 部署服务器上需要的Python3.6环境: 安装集成了python3.6 和pip ,virtualenv虚拟环境 的Anaconda(A ...

  7. 《软件工程和Python》第0周作业1

    写在前面的话 欢迎大家开始一段新的课程学习!从开博客开始吧.每次博客作业都会有评分,计入总成绩哦. 1.   截止日期 本次作业的提交截止时间:见老师要求 2.   作业要求 (1)建立个人技术博客和 ...

  8. 0506-Scrum 项目 2.0视频

    一.团队项目要求 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 二.NABCD模型 选题:约拍平台——家教平台 1) ...

  9. LeetCode题解:(221) Maximal Square

    题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  10. Cron表达式简单的介绍

    1.Cron是什么,用来做什么的 根据百度百科的解释:计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思.在Linux中,我们经常用到 cron 服务器来完成这项工作.cron服务器可以 ...