WHU-1551-Pairs(莫队算法+分块实现)
Give you a sequence consisted of n numbers. You are required to answer how many pairs of numbers (ai, aj) satisfy that | ai - aj | <= 2 and L <= i < j <= R.
For each case, the first line contains two numbers n and m, which represent the length of the number sequence and the number of queries. ( 1 <= n <= 10^5 and 1 <= m <= 10^5 )
The second line consists of n numbers separated by n - 1 spaces.( 0 <= ai <= 10^5 )
Then the m lines followed each contain two values Li and Ri.
Then just output the answers in m lines.
5 5 1 3 6 3 5 7 1 7
3 4
6 8
8 9
2 8
5 7
6 7
1 9
3 10
3 10
5 6
1
2
0
13
2
1
22
14
14
0
思路:把n个数分成 sqrt(n)块。用pos数组保存i所在的块。将全部查询按(pos(l),r)双keyword排序(这样子是为了降低后面的移动次数),然后模拟l向左向右移,r向左向右移,求出全部区间的结果。详见代码。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; struct Q{
int l,r,index,ans;
}query[100001]; int pos[100001],f[100001],w[100001]; bool cmplr(struct Q a,struct Q b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;//先按l所在的块排。假设相等就按r排
else return pos[a.l]<pos[b.l];
} bool cmpid(struct Q a,struct Q b)
{
return a.index<b.index;
} int update(int p,int ans,int add)//每移一步就更新一次结果
{
int i; if(add==1)//1代表区间长度加1
{
for(i=w[p]-2;i<=w[p]+2;i++)
{
if(i>=0) ans+=f[i];
}
f[w[p]]++;//f[i]表示当前区间里面包括了f[i]个i
}
else//-1代表区间长度减1
{
f[w[p]]--;//f[i]表示当前区间里面包括了f[i]个i
for(i=w[p]-2;i<=w[p]+2;i++)
{
if(i>=0) ans-=f[i];
}
} return ans;
} int main()
{
int n,m,cnt,i,ans,l,r,casenum=1; while(~scanf("%d%d",&n,&m))
{
memset(f,0,sizeof(f));//f[i]表示当前区间里面包括了f[i]个i cnt=(int)sqrt(n);//分成sqrt(n)块 for(i=1;i<=n;i++)
{
scanf("%d",&w[i]);
pos[i]=i/cnt;
} for(i=1;i<=m;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
query[i].index=i;
} sort(query+1,query+m+1,cmplr); ans=0;
l=1,r=0;
for(i=1;i<=m;i++)//每一次都使l和r指向查询区间的l和r,这样子区间长度添加时就不用把自己加进去了,区间长度减小时要把自己减去
{
if(query[i].l==query[i].r)
{
query[i].ans=0;
continue;
} if(r<query[i].r)
{
for(r=r+1;r<=query[i].r;r++)
{
ans=update(r,ans,1);
}
r--;
}
if(r>query[i].r)
{
for(;r>query[i].r;r--)
{
ans=update(r,ans,-1);
}
}
if(l<query[i].l)
{
for(;l<query[i].l;l++)
{
ans=update(l,ans,-1);
}
}
if(l>query[i].l)
{
for(l=l-1;l>=query[i].l;l--)
{
ans=update(l,ans,1);
}
l++;
} query[i].ans=ans;
//printf("index=%d,l=%d,r=%d,ans=%d\n",query[i].index,query[i].l,query[i].r,query[i].ans);
} sort(query+1,query+m+1,cmpid); printf("Case %d:\n",casenum++); for(i=1;i<=m;i++)
{
printf("%d\n",query[i].ans);
}
} }
WHU-1551-Pairs(莫队算法+分块实现)的更多相关文章
- 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- 【BZOJ3585/3339】mex 莫队算法+分块
[BZOJ3585]mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. ...
- whu oj 1551 Pairs (莫队算法)
problem_id=1551">题目链接 题目大意: 给出的询问,求出这个区间的里 差小于等于 2 的数字的对数. 思路分析: 莫队算法. 然后分析一下. 假设添加了一个数字.那么就 ...
随机推荐
- python中的参数、全局变量及局部变量
1.位置参数.关键字参数.默认参数的使用 位置参数.关键字参数 def test(x,y,z): print(x) print(y) print(z) test(1,2,3) #位置参数,必须一一对应 ...
- kvm使用kickstart文件自动安装系统
假定kvm已经准备好 1.创建磁盘 qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G 2.上传或下载安装镜像 mkdir -p /kvm/iso cd ...
- docker快速搭建
curl -sSL https://get.docker.com|sh docker --version systemctl start docker.service ps -ef|grep doc ...
- crontab 和 supervisor
crontab linux系统自带的工具,可以做定时任务,最小间隔是1分钟 配置crontab 命令 如果是以root用户编辑的,那么最后运行也是以root用户运行脚本文件 crontab -e 命令 ...
- LeetCode(12)Integer to Roman
题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 开门人和关门人(结构体+sort)
每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行给出记录的总天数N ( > ...
- C# Updating
闪开,这篇博文仅作笔记C#处理. 用来记录在学习/使用C#的过程中的Point,持续更新或成文后大幅删减 1,定义类,当声明类的对象后,类中的方法并不会被执行,构造函数会在声明类的对象时,被触发,没有 ...
- ES6(Iterator 和 for...of 循环)
Iterator 和 for...of 循环 1.什么是 Iterator 接口 Iterator 接口功能:用一种相同办法的接口让不同的数据结构得到统一的读取命令的方式 2.Iterator的基本用 ...
- 转载:better-scroll的相关api
格式:var obj = new BScroll(object,{[option1,],.,.}); 注意:1.要确保object元素的高度比其父元素高 2.使用时,一定要确保object所在的dom ...
- 65.什么是IOC?【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 这个小节吧,是无意当中看了一篇文章,觉得介绍的特别好,引用到我的博客中,让大家也乐下.那么他是怎么解说IOC的呢?看如下: 套用好莱坞的一句名言就 ...