HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接:
https://vjudge.net/contest/308446#problem/C
题意:
Chika gives you an integer sequence a1,a2,…,an and m tasks. For each task, you need to answer the number of " friendly pairs" in a given interval.
friendly pair: for two integers ai and aj, if i<j and the absolute value of ai−aj is no more than a given constant integer K, then (i,j) is called a "friendly pair".A friendly pair (i,j) in a interval [L,R] should satisfy L≤i<j≤R.
思路:
莫队算法,刚学的莫队,第一每次更改都查询位置,太蠢了.
用两个数组记录每个位置查询的下标.减少时间开销.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e4+10;
struct Node
{
int l, r;
int pos;
}node[MAXN];
int Tree[MAXN];
int a[MAXN], b[MAXN];
int L[MAXN], R[MAXN], P[MAXN];
int Res[MAXN];
int n, m, k;
int pos;
int l, r, res;
int unit;
int Lowbit(int x)
{
return (-x)&x;
}
void AddTree(int x, int v)
{
while (x <= n)
{
Tree[x] += v;
x += Lowbit(x);
}
}
int GetSum(int x)
{
int sum = 0;
while (x > 0)
{
sum += Tree[x];
x -= Lowbit(x);
}
return sum;
}
void Add(int x)
{
int rsum = GetSum(R[x]);
int lsum = GetSum(L[x]);
res += rsum-lsum;
AddTree(P[x], 1);
}
void Del(int x)
{
AddTree(P[x], -1);
int rsum = GetSum(R[x]);
int lsum = GetSum(L[x]);
res -= rsum - lsum;
}
void Query(int ql, int qr)
{
while (l < ql)
Del(l++);
while (l > ql)
Add(--l);
while (r < qr)
Add(++r);
while (r > qr)
Del(r--);
}
bool cmp(Node& a, Node& b)
{
if (a.l/unit != b.l/unit)
return a.l/unit < b.l/unit;
return a.r < b.r;
}
int main()
{
scanf("%d %d %d", &n, &m, &k);
unit = sqrt(n);
for (int i = 1;i <= n;i++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
for (int i = 1;i <= m;i++)
scanf("%d %d", &node[i].l, &node[i].r), node[i].pos = i;
sort(b+1, b+1+n);
pos = unique(b+1, b+1+n)-b;
for (int i = 1;i <= n;i++)
{
P[i] = lower_bound(b+1, b+pos, a[i])-b;
R[i] = lower_bound(b+1, b+pos, a[i]+k)-b;
if (b[R[i]] != a[i]+k)
R[i]--;
L[i] = lower_bound(b+1, b+pos, a[i]-k)-b-1;
}
sort(node+1, node+1+m, cmp);
l = 1, r = 0;
for (int i = 1;i <= m;i++)
{
Query(node[i].l, node[i].r);
Res[node[i].pos] = res;
}
for (int i = 1;i <= m;i++)
printf("%d\n", Res[i]);
return 0;
}
/*
7 2 3
2 5 7 5 1 5 6
6 6
1 3
4 6
2 4
3 4
7 2 3
2 5 7 5 1 5 6
1 3
4 6
*/
HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)的更多相关文章
- BZOJ3289【莫队算法+树状数组+离散化】
思路: 区间逆序数即是交换次数. 逆序数,可以用树状数组吧. 怎么处理区间变换的时候求逆序数啊.. 这里分成左边的增/删,右边的增/删 因为是按时序插入, 所以左边增,增一个数,计算:ans+=sun ...
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- BZOJ3289 Mato的文件管理(莫队算法+树状数组)
题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...
- 【BZOJ3289】Mato的文件管理 莫队算法+树状数组
[BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...
- BZOJ 3289:Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 题意:…… 思路:求交换次数即求逆序对数.确定了这个之后,先离散化数组.然后在后面插入元素的话 ...
- 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
随机推荐
- 2018-5 - 热经 - 北京中地时空数码科技有限公司 - 研发工程师(WEBGIS 方向)
一面: 登记,填写个人信息 笔试 选择题: HTML,CSS,JS 的选择题,都是基础题.其中有一道问哪个不是 document 的属性或方法,我在 bgColor 和 focus() 上面纠结了一下 ...
- MAC安装navcat
安装及破解,参照此文: https://blog.csdn.net/marswill/article/details/79808416
- db4o发布7.2,出现.NET 3.5版本,支持LINQ
db4o发布7.2,出现.NET 3.5版本,支持LINQ Db4Object刚刚发布了db4o的7.2beta,除了以前支持如下的平台:.NET 1.1,.NET 2.0,Mono外,现在还支持 ...
- Jedis源码浅析
1.概述 Jedis是redis官网推荐的redis java client,代码维护在github https://github.com/xetorthio/jedis. 本质上Jedis帮我们封装 ...
- Django-DRF组件学习-其他学习
1.认证Authentication 可以在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_ ...
- idea把java web项目打成war包
1.新建artifacts 2.设置你的目录内容路径 3.找到项目web或webapp的路径 4.可以直接添加已经跑通的项目 5.配置完成点OK 6.编译打成war包 7.点击编译结束打完收工 8.找 ...
- Web Services调用存储过程简单实例
转:http://www.cnblogs.com/jasenkin/archive/2010/03/02/1676634.html Web Services 主要利用 HTTP 和 SOAP 协议使商 ...
- vps国外节点ubuntu修改时区重启不失效
使用了tzselect方法,但是重启后时区又恢复到初始情况了,不得行. 使用下面的方法成功了: 1.将时区修改成上海时区 cp /usr/share/zoneinfo/Asia/Shanghai /e ...
- Linux rsync 远程同步部署篇
rsync官网: www.samba.org/ftp/rsync.html端口:873上机实战系列项目100台规模集群全网数据备份解决方案3.本项目提供免费实战讲解视频:Linux集群全网服务器数据备 ...
- SpringBoot、ActiveMQ整合阿里大鱼-----短信服务
3.短信微服务 3.1需求分析 构建一个通用的短信发送服务(独立于优乐选的单独工程),接收activeMQ的消息(MAP类型) 消息包括手机号(mobile).短信模板号(template_code ...