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

    “在最艰苦的时候,就是你离成功最近的时候”,让暴风雨来得更猛烈些吧. 健身教练/学员,买的那本Java Web还是那么新,显然假期偷懒了,只能一点一点的补回来了.一个假期没有打开过自己的脑洞,真心醉了 ...

  2. struts2 jsp提交对象数据要这么干

    不要每个属性都 setter getter .. 这样页面很难看... 直接 把对象变成一个成员变量会比较好. Java code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks dp+矩阵加速

    题目链接: http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test2 se ...

  4. POJ 1239 Increasing Sequences 动态规划

    题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...

  5. 四则运算App--大总结(已完成)

    1. 贡献分分配(20分) 欧泽波:14分,Android的学习,代码的编写,等等 杨洁华:1分,提供学习资料,框架的设计等等 赵泽嘉:3分,提供学习资料,框架的设计等等 林扬滨:2分,提供学习资料, ...

  6. Redis4.0模块子系统实现简述

    一.模块加载方法 1.在配置文件或者启动参数里面通过<loadmodule /path/to/mymodule.so args>指令加载 2.Redis启动后,通过<module l ...

  7. 5G时代

    电信语音承载在CDMA2G网络--所以2G基本没有网络 网络走fdd4g 如果5G时代来临,4g网络可能就会像3G一样的慢

  8. selenium之数据驱动框架应用WPS个人中心自动签到

    wps在注册后,有个每日签到的功能,签到后有几率送wps的专属金币[稻米],为了免费获得,又不想每天都是人工去执行签到动作,所以用selenium写了个小脚本,准备用数据驱动框架来完成这个事情,数据驱 ...

  9. Mysql 定位执行效率低的sql 语句

    一.通过MySQL慢查询日志定位执行效率低的SQL语句. MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow-queries[=file_name]选项启动时,mysq ...

  10. 使用Fiddler后谷歌浏览器访问https不安全

    今天初次接触java爬虫,师兄给了一个软件加一个demo,软件是Fiddler,在网上找资料稍微学习了一下,自己一顿乱配...然后gg,谷歌浏览器访问https协议时都提示不安全,“您的链接不是一个私 ...