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 ...
随机推荐
- Why 使用TLS记录封装IP层VPN IS A Bad Idea
一个很自然的想法,使用TLS套餐一IP数据报实现第三层VPN.这种想法必须经过深思熟虑的,但不幸的是,.这是一个错误的想法.有文章<Why TCP Over TCP Is A Bad Idea& ...
- wxWidgets刚開始学习的人导引(1)——前言
wxWidgets刚開始学习的人导引全文件夹 PDF版及附件下载 1 前言2 下载.安装wxWidgets3 wxWidgets应用程序初体验4 wxWidgets学习资料及利用方法指导5 用wx ...
- Just like normal variables,
Just like normal variables, pointers can be declared constant. There are two different ways that poi ...
- [Elasticsearch] 控制相关性 (一) - 后面的相关度分值理论计算
从第一章翻译Elasticsearch官方指南Controlling Relevance一章. 控制相关度(Controlling Relevance) 对于仅处理结构化数据(比方日期.数值和字符枚举 ...
- STL顺序容器【vector】【deque】【list】
我们都知道,stl在集装箱船分为两类,订购集装箱和相关的容器. 顺序容器有三种即动态数组vector,双端队列deque,以及链表list (对csdn的文字排版严重吐槽.写好的版发表了就变了) 一: ...
- 新书《iOS8 Swift编程指南》货架
颐和园的新书出版. 链接:http://www.amazon.cn/dp/B00YOQSYAO 这本书从去年开始7可能开始写.今年1完成这个月的第一稿,经过多次修改,今天,最后的正式出版,欢迎大家指正 ...
- Windows 8 应用开发 - 挂起与恢复
原文:Windows 8 应用开发 - 挂起与恢复 Windows 8 应用通常涉及到两种数据类型:应用数据与会话数据.在上一篇提到的本地数据存储就是应用层面的数据,包括应用参数设置.用户重 ...
- oracle 优化or 更换in、exists、union all几个字眼,测试没有问题!
oracle 优化or 更换in.exists.union几个字眼.测试没有问题! 根据实际情况选择相应的语句是.假设指数,or全表扫描,in 和not in 应慎用.否则会导致全表扫描. sele ...
- 冒泡排序java
先对冒泡排序做一个简单的解释,然后是代码的实现.解释出资<java的数据结构和算法>,大家可以看看. 排序类: package com.dxx.order; public class Bu ...
- 重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果)
原文:重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果) [源码下载] 重新想象 Windows 8 Store Apps (21) ...