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 ...
随机推荐
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1
以下是一些设计略显繁琐,有必要清除思维. 下一个主要的成就,当我们点击Gobutton后,得到一个随机数骰子,是走了几步,它是基于以下步骤行走路径的数目,然后移动位置的基于角色的路径. 流程如图普遍认 ...
- 变化App.config其中值,并保存
using System; using System.Collections.Generic; using System.Configuration; using System.IO; using S ...
- DrectX11学习笔记Texture2D有关
///////////////////////////////////////////////////////////////////////////////////// 有时候....有时候.... ...
- Saltstack 安装使用
Saltstack是一个具备puppet与func功能为一身的集中化,轻量级的自动化运维管理工具,使用python编写,功能非常强大,可以使用EPEL快速安装.相比较puppet,安装和配置更加容易和 ...
- 邮箱password复位图
blog宗旨:用图说话 辅助文字说明: 长处:用户系统完毕接口部分.界面留给业务自行设计. 缺点:邮箱发送邮件定制模板,没有完毕松耦合. 改进点:邮箱的准备发送邮件模板. 版权声明:本文博客原创文章, ...
- 从原理角度解析Android (Java) http 文件上传
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23781773 文件上传是我们项目中经常使用的功能,一般我们的服务器可能都是web ...
- Oracle性能优化顺序表名称来选择最有效的学习笔记
选择最有效的顺序表名(只有有效的基于规则的优化) ORACLE分析器按照订单处理从右到左FROM在FROM子句中的表名,故FROM写在最后的表(基础表 driving table)将被最先处理. 在 ...
- 【Android进阶】Android程序与JavaScript之间的简单调用
本篇将讲解一个简单的Android与JavaScript之间的简单调用的小程序 效果图 工程结构 HTMLActivity.java代码 package com.example.javatojs; i ...
- UIStepper使用的具体解释的控制
UIStepper控件类似于UISlider控件,但它有"+"和"-"两个button,单击当中一个可使属性value值递增或递减. 如声音.速度.图片等的大小 ...
- Linux经常使用的命令(十) - nl
nl命令在linux系统中用来计算文件里行号. nl 能够将输出的文件内容自己主动的加上行号.其默认的结果与 cat -n 有点不太一样. nl 能够将行号做比較多的显示设计,包含位数与是否自己主动补 ...