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 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
随机推荐
- kafka多线程消费
建立kafka消费类ConsumerRunnable ,实现Runnable接口: import com.alibaba.fastjson.JSON; import com.alibaba.fastj ...
- window.screenLeft&&window.screenTop&&window.screenX&&window.screenY
http://blog.sina.com.cn/s/blog_14e2a237b0102w4i0.html window.screenLeft&&window.screenTop&am ...
- c++ 调用 sqlcipher
#include <iostream> #include <string.h> #include "sqlite3.h" using namespace s ...
- linux中从一台机器复制文件或目录到另一台机器上linux机器上
本机IP:x.x.x.1需要拷贝的机器IP:x.x.x.2用户名:ssh_user 目的:将本机中source_path路径下的文件或目录拷贝到另一台机器的destination_path/路径下 复 ...
- 应用安全 - 无文件攻击 - Office漏洞 - 汇总
CVE-2017-0199 Date: -1 类型: 弹窗|内网穿透导致远程代码执行 影响范围: Microsoft Office 2007 Service Pack 3 Microsoft Offi ...
- 数据契约[DataContract]
数据契约(DataContract)服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型.一旦声明一个类型为DataContract,那么该类型就可以被序列 ...
- eclipse 或 STS 卸载SVN 插件
help菜单 ==> about eclipse ==>install details按钮 ==> installed software选项卡 选中下面的这几项,点击 uni ...
- Mybatis—动态sql拼接问题
背景:使用Mybatis的最近半年,经常发现一些小坑,现在总结回顾下,记个小本本,不让它再来欺负我! 百度了许久,才留心到官网文档,比我的全,我很菜的! *************<if> ...
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
出现错误 Client does not support authentication protocol requested by server; consider upgrading MySQL c ...
- CentOS服务器开放端口
拿到服务器之后接着之前的通信步骤进行,却发现怎么也连接不上.最后发现是因为服务器端的端口5000没有开放.下面记录一下开放端口的过程. 使用命令 netstat -anp 查看端口开放情况.如果显示命 ...