Description

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.

Input
The input consists of one or more test cases.



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.
Output
For each case, first output “Case #: “ in a single line, where # will be replaced by case number.

Then just output the answers in m lines.
Sample Input
10 10

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
Sample Output
Case 1:

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(莫队算法+分块实现)的更多相关文章

  1. 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...

  2. 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...

  3. XOR and Favorite Number(莫队算法+分块)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  4. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...

  5. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  6. HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)

    链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...

  7. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

  8. 【BZOJ3585/3339】mex 莫队算法+分块

    [BZOJ3585]mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. ...

  9. whu oj 1551 Pairs (莫队算法)

    problem_id=1551">题目链接 题目大意: 给出的询问,求出这个区间的里 差小于等于 2 的数字的对数. 思路分析: 莫队算法. 然后分析一下. 假设添加了一个数字.那么就 ...

随机推荐

  1. python类中__unicode__和__str__方法的妙用

    在python类中有个__str__的特殊方法,该方法可以使print打印出来的东西更美观,在类里就可以定义,如下代码: class Test: def __init__(self, name, jo ...

  2. 使用cURL和用户名和密码?

    问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...

  3. [HNOI2009]梦幻布丁(链表+启发式合并)

    洛谷传送门 开始一个O(n^2)思路,每次每句要改变颜色的点,改变完颜色后重新计算颜色的段数,显然拉闸. 然后呢..然后就不会了. 看了别人博客,才知道有个叫做启发式合并的东西,就是把小的合并到大的上 ...

  4. 【二分图匹配】E. 过山车

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/E [题意] 裸的最大匹配 [教训] 一开始边数开了k,建的是无向图,结果T了,改 ...

  5. docker改变镜像源

    sudo echo “DOCKER_OPTS=\”\$DOCKER_OPTS –registry-mirror=http://your-id.m.daocloud.io -d\”” >> ...

  6. [NOIP2001] 提高组 洛谷P1024 一元三次方程求解

    题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差 ...

  7. Python基础教程笔记——第7章:更加抽象(类)

    下面进入Python的面向对象: 对象的魔力: 多态:---可以对不同类的对象使用同样的操作 封装:---对外部隐藏对象内部的工作方式 继承:---以普通的类为基础建立专门的类对象 (1)多态: is ...

  8. Back弹出AlertDialog

    package com.pingyijinren.helloworld.activity; import android.content.DialogInterface; import android ...

  9. App竞品技术分析 (3)减小安装包的体积(转)

    http://blog.csdn.net/JspAndAsp/article/details/49339403 1 从几件小事说起 春节在家帮姐姐的iPhone手机安装市面上形形色色的App,忘记她是 ...

  10. SQL SERVER 2012 第三章 T-SQL 基本语句 having子句

    SELECT ManagerID AS Manager,COUNT(*) AS Reports FROM Human.Resources.Employee2 WHERE EmployeeID !=5 ...