题解【51nod 1290 Counting Diff Pairs】
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】的更多相关文章
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- 51nod 1290 Counting Diff Pairs 莫队 + bit
一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[j]中,有多少对数,abs(A[i] - A[j]) <= K(abs表示绝对值) ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 题解 || Swap Nodes in Pairs 问题
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- LeetCode题解之Swap Nodes in Pairs
1.题目描述 2.问题分析 对两个节点进行交换操作 3.代码 ListNode* swapPairs(ListNode* head) { if( !head || head->next == N ...
- leetcode个人题解——#24 Swap Nodes in Pairs
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...
- 【题解】CF#403 D-Beautiful Pairs of Numbers
这题还挺对胃口的哈哈~是喜欢的画风!回家路上一边听歌一边想到的解法,写出来记录一下…… 首先,由于 \(b_{k} < a_{k + 1}\) ,所以我们可以看作是在一个长度为 n 的序列上选择 ...
- 题解 51nod 1597 有限背包计数问题
题目传送门 题目大意 给出 \(n\),第 \(i\) 个数有 \(i\) 个,问凑出 \(n\) 的方案数. \(n\le 10^5\) 思路 呜呜呜,傻掉了... 首先想到根号分治,分别考虑 \( ...
- Counting The Important Pairs CodeChef - TAPAIR
https://vjudge.net/problem/CodeChef-TAPAIR 合法的删除方法: 第一种:桥边与其余任意边(1)桥*(桥-1)/2(两条桥边)(2)桥*(m-桥)(桥边+其他边) ...
随机推荐
- Scrum立会报告+燃尽图(Final阶段第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...
- Runtime 类的使用
package com.System.Runtime; import java.io.IOException; /* RunTime 该类类主要代表了应用程序运行的环境. getRuntime() 返 ...
- 福大软工1816 · 评分结果 · Alpha冲刺
作业地址:alpha冲刺1.alpha冲刺2.alpha冲刺3.alpha冲刺4.alpha冲刺5.alpha冲刺6.alpha冲刺7.alpha冲刺8.alpha冲刺9.alpha冲刺10 作业提交 ...
- week2-作业2
项目地址:https://git.coding.net/Rainoob/calculate.git ·1.需求分析:程序可以根据输入的参数n随机产生n道四则运算计算题,每个数字在0-100之间.运算符 ...
- 6/11 sprint2 看板和燃尽图的更新
- 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- yii 验证码 CCaptcha的总结(转)
今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &l ...
- 实现LinearLayout(垂直布局,Gravity内容排布)
首先上Gravity的代码,Android原版的Gravity搞得挺复杂的,太高端了.但基本思路是使用位运算来做常量,我就自己消化了一些,按自己的思路来实现. 先上代码,在做分析. package k ...
- Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...
- Task的运行原理和工作窃取
在net4.0以前,当调用ThreadPool.QueueUserWorkItem方法往线程池中插入作业时,会把作业内容(其实就是一个委托)放到线程池中的一个全局队列中,然后线程池中的线程按照先进先出 ...