51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组
题面
一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[j]中,有多少对数,abs(A[i] - A[j]) <= K(abs表示绝对值)。
题解
莫队!//其实我就是搜索“51nod + 莫队”找到的这道题……
七级算法题!
一道320分!
你值得拥有!
题解就是……用个普通的莫队,加上树状数组来统计符合条件的数个数,就好啦。
当增加/删除一个数的时候,统计能和它组成合法数对的数的个数,然后对答案进行相应的增/减。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
#define space putchar(' ')
#define enter putchar('\n')
const int N = 50005, B = 233;
int n, d, m, a[N], lst[N], idx, tol[N], tor[N], tr[N], pl = 1, pr;
ll res, ans[N];
#define bel(x) (((x) - 1) / B + 1)
struct query {
int id, l, r;
bool operator < (const query &b) const {
return bel(l) == bel(b.l) ? r < b.r : l < b.l;
}
} q[N];
void init(){
sort(lst + 1, lst + n + 1);
idx = unique(lst + 1, lst + n + 1) - lst - 1;
for(int i = 1; i <= n; i++)
a[i] = lower_bound(lst + 1, lst + idx + 1, a[i]) - lst;
int l = 1, r = 1;
for(int i = 1; i <= idx; i++){
while(l < i && lst[i] - lst[l] > d) l++;
while(r < idx && lst[r + 1] - lst[i] <= d) r++;
tol[i] = l, tor[i] = r;
}
}
void add(int p, int x){
while(p <= idx) tr[p] += x, p += p & -p;
}
int ask(int p){
int res = 0;
while(p) res += tr[p], p -= p & -p;
return res;
}
int getres(int x){
return ask(tor[x]) - ask(tol[x] - 1);
}
int main(){
read(n), read(d), read(m);
for(int i = 1; i <= n; i++)
read(a[i]), lst[i] = a[i];
init();
for(int i = 1; i <= m; i++)
q[i].id = i, read(q[i].l), q[i].l++, read(q[i].r), q[i].r++;
sort(q + 1, q + m + 1);
for(int i = 1; i <= m; i++){
while(pl > q[i].l) res += getres(a[--pl]), add(a[pl], 1);
while(pr < q[i].r) res += getres(a[++pr]), add(a[pr], 1);
while(pl < q[i].l) add(a[pl], -1), res -= getres(a[pl++]);
while(pr > q[i].r) add(a[pr], -1), res -= getres(a[pr--]);
ans[q[i].id] = res;
}
for(int i = 1; i <= m; i++)
write(ans[i]), enter;
return 0;
}
51nod 1290 Counting Diff Pairs | 莫队 树状数组的更多相关文章
- 51nod 1290 Counting Diff Pairs 莫队 + bit
一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[j]中,有多少对数,abs(A[i] - A[j]) <= K(abs表示绝对值) ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- HihoCoder 1488 : 排队接水(莫队+树状数组)
描述 有n个小朋友需要接水,其中第i个小朋友接水需要ai分钟. 由于水龙头有限,小Hi需要知道如果为第l个到第r个小朋友分配一个水龙头,如何安排他们的接水顺序才能使得他们等待加接水的时间总和最小. 小 ...
- BZOJ 3236 莫队+树状数组
思路: 莫队+树状数组 (据说此题卡常数) yzy写了一天(偷笑) 复杂度有点儿爆炸 O(msqrt(n)logn) //By SiriusRen #include <cmath> #in ...
随机推荐
- Unity学习笔记(5):动态加载Prefab
第一种方法,从Resources文件夹读取Prefab Assets/Resources文件夹是Unity中的一个特殊文件夹,在博主当前的认知里,放在这个文件夹里的Prefab可以被代码动态加载 直接 ...
- 笨办法学Python - 习题3: Numbers and Math
目录 习题 3: 数字和数学计算 算术运算符 加分习题: 我的答案: 总结: 扩展: Python比较运算符 Python赋值运算符 Python位运算符 Python逻辑运算符 Python成员运算 ...
- centos6.9+lnmp1.5环境部署swoole记录
hiredis下载地址:https://github.com/redis/hiredis/releasesunzip hiredis-v0.13.3.zipmake -jsudo make insta ...
- bootstrape3 模态
version 2 的模态 不能在 version 3 上面用. http://schy-hqh.iteye.com/blog/1995690
- Python20-Day04
##########迭代器.生成器和面向过程编程########## 一.迭代器 迭代器是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值: l = [1,2,3] cou ...
- HotSpot JVM 常用配置设置
本文讨论的选项是针对HotSpot虚拟机的. 1.选项分类及语法 HotspotJVM提供以下三大类选项: 1.1.标准选项 这类选项的功能是很稳定的,在后续版本中也不太会发生变化. 运行java或者 ...
- Set up classpath in Eclipse
On Eclipse main window Right click on .java file-> Run As -> Run Configurations... On Run Conf ...
- 关于httpServlet.service()步骤
关于httpServlet.service()步骤 关于()方法 1.HTTP Servlet 使用一个 HTML 表格来发送和接收数据.要创建一个 HTTP Servlet,就需要扩展 HttpSe ...
- my everying
evering -> everything 一.请回望暑假时的第一次作业,你对于软件工程课程的想象 1)对比开篇博客你对课程目标和期待,"希望通过实践锻炼,增强计算机专业的能力和就业竞 ...
- MAVEN ERROR maven-resources-plugin
maven新建项目时报错 Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin: ...