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. 2019天梯赛练习题(L2专项练习)

    7-2 列出连通集 (25 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序 ...

  2. UVa-401-Palindromes(回文)

    这一题的话我们可以把映像字符的内容给放入一个字符串常量里面,然后开辟一个二维的字符串常量数组,里面放置答案. 对于回文实际上是很好求的,对于镜像的话,我们写一个返回char的函数,让它接收一个char ...

  3. ubuntu中执行docker info出现警告信息WARNING: No memory limit support 或 WARNING: No swap limit support

    docker info 指令报若下错误:WARNING: No memory limit support 或WARNING: No swap limit support 解决方法: 1.打开/etc/ ...

  4. 条款30:透彻了解inline的里里外外(understand the ins and outs of inlining)

    NOTE: 1.将大多数inline限制在小型 被频繁调用的函数身上.这可使日后的调试过程和二进制升级(binary upgradability)更容易,也可使潜在的代码膨胀问题最小化, 使程序的速度 ...

  5. 第二天,学习if,变量,注释

    zz_age = 31guss_age=int(input("input your answer:"))if guss_age == zz_age: print ("Yo ...

  6. Root of AVL Tree

    04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...

  7. Android记录2013年10月20日

    1. ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connect ...

  8. Django 的信号 & Flask 的信号

    信号:框架内部已帮助开发者预留的可扩展的位置 一.Django 的信号 项目目录结构: django_signal |--- app01 |--- models.py |--- views.py .. ...

  9. POJ 1797 【一种叫做最大生成树的很有趣的贪心】【也可以用dij的变形思想~】

    题意: 给一个无向图,找1到n所有的路中每条路最小权值的最大值! 屌丝一开始的思想是利用dij的变形~ 但是==屌丝忘记了更新dis数组~结果TLE无数次... 说正经的~dij的变形思想是这样的if ...

  10. JVM内存区域(运行时数据区)划分

    前言: 我们每天都在编写Java代码,编译,执行.很多人已经知道Java源代码文件(.java后缀)会被Java编译器编译为字节码文件(.class后缀),然后由JVM中的类加载器加载各个类的字节码文 ...