whu oj 1551 Pairs (莫队算法)
题目大意:
给出的询问,求出这个区间的里 差小于等于 2 的数字的对数。
思路分析:
莫队算法。
然后分析一下。
假设添加了一个数字。那么就要加它旁边相差为2 的数字的和。
反之降低一个。就要降低相差为2 的数字的和。再减去自己这个1.。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 100005 using namespace std; int app[maxn];
int save[maxn];
int pos[maxn]; struct foo
{
int l,r,index;
int ans;
bool operator < (const foo &cmp)const
{
if(pos[l] == pos[cmp.l])
return r<cmp.r;
return pos[l]<pos[cmp.l];
}
}Q[maxn];
bool cmp_id(const foo &a, const foo &b)
{
return a.index < b.index;
}
void debug()
{
for(int i=0;i<=7;i++)
printf("%d ",app[i]);
puts("");
}
void modify(int p,int &ans,int add)
{
int tot=0;
for(int i=max(save[p]-2,0);i<=save[p]+2;i++)
{
tot+=app[i];
} if(add>0)ans+=tot;
else ans-=tot-1;
app[save[p]]+=add;
}
int main()
{
int n,m;
int cas=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
int SIZE=(int)sqrt(1.0*n);
memset(app,0,sizeof app); for(int i=1;i<=n;i++)
{
scanf("%d",&save[i]);
pos[i]=(i-1)/SIZE+1;
} for(int i=0;i<m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].index=i;
} sort(Q,Q+m);
int ans=0;
for(int i=0,l=1,r=0;i<m;i++)
{
if(r<Q[i].r)
{
for(r=r+1;r<=Q[i].r;r++)
modify(r,ans,1);
r--;
}
if(r>Q[i].r)
{
for(;r>Q[i].r;r--)
modify(r,ans,-1);
}
if(l<Q[i].l)
{
for(;l<Q[i].l;l++)
modify(l,ans,-1);
}
if(l>Q[i].l)
{
for(l=l-1;l>=Q[i].l;l--)
modify(l,ans,1);
l++;
}
Q[i].ans=ans;
} sort(Q,Q+m,cmp_id);
printf("Case %d:\n",cas++);
for(int i=0;i<m;i++)
printf("%d\n",Q[i].ans);
}
return 0;
}
whu oj 1551 Pairs (莫队算法)的更多相关文章
- HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述 Given a sequence of integers a1, a2, ..., an a ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- kyeremal-bzoj2038-[2009国家集训队]-小z的袜子(hose)-莫队算法
id=2038">bzoj2038-[2009国家集训队]-小z的袜子(hose) F.A.Qs Home Discuss ProblemSet Status Ranklist Con ...
随机推荐
- (视频)《高速创建站点》 4.2 完结篇 – 应用运营vs.发射卫星,遥測(Telemetry) 技术
本文是<高速创建站点>系列的第10篇(完结篇),假设你还没有看过之前的内容,建议你点击下面文件夹中的章节先阅读其它内容再回到本文.訪问本系列文件夹.请点击:http://anb.io/bl ...
- 使用LabVIEWPC的制备
使用LabVIEWPC的制备 1.下载.安装LabVIEW 如今,互联网搜索,你可以搜索出一大推LabVIEW下载并安装破解教程.因此,这里没有具体描述的.请自行百度~~.另外.提醒一下,下载LabV ...
- 凝视条件推断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
<!--[if !IE]><!--> 除IE外可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![e ...
- 【转】Qt事件循环与线程 二
转自:http://blog.csdn.net/changsheng230/article/details/6153449 续上文:http://blog.csdn.net/changsheng230 ...
- Blend4精选案例图解教程(五):可视数据管理
原文:Blend4精选案例图解教程(五):可视数据管理 应用程序中我们会经常需要操作数据,在程序设计之初示例数据一般都是手工添加,Blend4提供了非常方便的数据管理能力,包括丰富的数据类型和内置示例 ...
- hive的非交互模式
在linux的终端运行:$HIVE_HOME/bin/hive 会进入交互模式: $HIVE_HOME/bin/hive -e或者-f 是非交互模式 1.非交互模式运行HQL语句 $HIVE_HOM ...
- effective c++ 条款9 do not call virtual function in constructor or deconstructor
在构造函数中不要调用virtual函数,调用了也不会有预期的效果. 举个例子 class Transaction { public: Transaction() { log(); } ; } clas ...
- js“分享到”侧边框伸缩实现
思路: 1,临界值是 -150 和 0 如果大于临界值,就要隐藏 2,隐藏:速度为负 显示:速度为正 3,如果与临界值相等,就清空定时器 否则,就运动 ------------------------ ...
- mysql监视器MONyog的使用
MONyog是个商业收费软件,可是能够找一下破解版.我用的是4.72破解版 1. 图1.1 在server设置中,如图1.1. 在Sniffer Settings里Enable sniff ...
- C#如何获得 WINDOWS
版本号
using System; using System.Runtime.InteropServices; namespace GetWindowsVersion { [ StructLayout( La ...