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 ...
- 使用dom4j创建和解析xml
之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码 package xml; import java.io.File; import java ...
- VMWare网络设置的3中方式(转)
一 VMware网络设置的三种方式 1 Host-only连接方式 让虚机具有与宿主机不同的各自独立IP地址,但与宿主机位于不同网段,同时为宿主主机新增一个IP地址,且保证该IP地址与各虚机IP地址 ...
- ORACLE Install (10g r2) FOR Red Hat Enterprise Linux Server release 5.5 (64 bit) (转)
OS Info----------# cat /etc/redhat-releaseRed Hat Enterprise Linux Server release 5.5 (Tikanga)# cat ...
- cocos2dx 3.2 定义自己使用rapidjson阅读json数据
一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...
- SqlServer service broker 分布式系统(赵松桃)跳水 2005 数据库编程
1.创建一个测试数据库 create database ServerbrokerTest on primary( name=ServerbrokerTest_data, filename='C:\Pr ...
- 问题:DataGrid该行并不总是很清楚验证错误(删除),
解决方案,如下面
转载,收藏转载请注明出处http://blog.csdn.net/metal1/article/details/37568391 景象:于DataGrid进数据有误,行验证返回new Validati ...
- MySQL与逻辑模块
启动MySQL 1.初始化模块运行&&存储引擎初始化运行 2.1中运行完毕后 ---->连接管理模块接手 3.连接管理模块启动处理client连接请求的监听程序(tcp/ip 网 ...
- WPF技术触屏上的应用系列(二): 嵌入百度地图、API调用及结合本地数据库在地图上进行自定义标点的实现
原文:WPF技术触屏上的应用系列(二): 嵌入百度地图.API调用及结合本地数据库在地图上进行自定义标点的实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系 ...
- Java服务器下载速度的限制
没有取之不尽,用之不竭的资源.server有限的带宽.运营商可以限制一点点.近期使用云存储openstack swift待办事项文件存储下载.如果第一个限速code: private Long wri ...