problem_id=1551">题目链接

题目大意:

给出的询问,求出这个区间的里 差小于等于 2 的数字的对数。

思路分析:

莫队算法。

然后分析一下。

假设添加了一个数字。那么就要加它旁边相差为2 的数字的和。

反之降低一个。就要降低相差为2 的数字的和。再减去自己这个1.。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #define maxn 100005
  7.  
  8. using namespace std;
  9.  
  10. int app[maxn];
  11. int save[maxn];
  12. int pos[maxn];
  13.  
  14. struct foo
  15. {
  16. int l,r,index;
  17. int ans;
  18. bool operator < (const foo &cmp)const
  19. {
  20. if(pos[l] == pos[cmp.l])
  21. return r<cmp.r;
  22. return pos[l]<pos[cmp.l];
  23. }
  24. }Q[maxn];
  25. bool cmp_id(const foo &a, const foo &b)
  26. {
  27. return a.index < b.index;
  28. }
  29. void debug()
  30. {
  31. for(int i=0;i<=7;i++)
  32. printf("%d ",app[i]);
  33. puts("");
  34. }
  35. void modify(int p,int &ans,int add)
  36. {
  37. int tot=0;
  38. for(int i=max(save[p]-2,0);i<=save[p]+2;i++)
  39. {
  40. tot+=app[i];
  41. }
  42.  
  43. if(add>0)ans+=tot;
  44. else ans-=tot-1;
  45. app[save[p]]+=add;
  46. }
  47. int main()
  48. {
  49. int n,m;
  50. int cas=1;
  51. while(scanf("%d%d",&n,&m)!=EOF)
  52. {
  53. int SIZE=(int)sqrt(1.0*n);
  54. memset(app,0,sizeof app);
  55.  
  56. for(int i=1;i<=n;i++)
  57. {
  58. scanf("%d",&save[i]);
  59. pos[i]=(i-1)/SIZE+1;
  60. }
  61.  
  62. for(int i=0;i<m;i++)
  63. {
  64. scanf("%d%d",&Q[i].l,&Q[i].r);
  65. Q[i].index=i;
  66. }
  67.  
  68. sort(Q,Q+m);
  69. int ans=0;
  70. for(int i=0,l=1,r=0;i<m;i++)
  71. {
  72. if(r<Q[i].r)
  73. {
  74. for(r=r+1;r<=Q[i].r;r++)
  75. modify(r,ans,1);
  76. r--;
  77. }
  78. if(r>Q[i].r)
  79. {
  80. for(;r>Q[i].r;r--)
  81. modify(r,ans,-1);
  82. }
  83. if(l<Q[i].l)
  84. {
  85. for(;l<Q[i].l;l++)
  86. modify(l,ans,-1);
  87. }
  88. if(l>Q[i].l)
  89. {
  90. for(l=l-1;l>=Q[i].l;l--)
  91. modify(l,ans,1);
  92. l++;
  93. }
  94. Q[i].ans=ans;
  95. }
  96.  
  97. sort(Q,Q+m,cmp_id);
  98. printf("Case %d:\n",cas++);
  99. for(int i=0;i<m;i++)
  100. printf("%d\n",Q[i].ans);
  101. }
  102. return 0;
  103. }

whu oj 1551 Pairs (莫队算法)的更多相关文章

  1. HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)

    链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...

  2. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  3. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  4. 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 ...

  5. XOR and Favorite Number(莫队算法+分块)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

  7. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  8. 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 ...

  9. kyeremal-bzoj2038-[2009国家集训队]-小z的袜子(hose)-莫队算法

    id=2038">bzoj2038-[2009国家集训队]-小z的袜子(hose) F.A.Qs Home Discuss ProblemSet Status Ranklist Con ...

随机推荐

  1. (视频)《高速创建站点》 4.2 完结篇 – 应用运营vs.发射卫星,遥測(Telemetry) 技术

    本文是<高速创建站点>系列的第10篇(完结篇),假设你还没有看过之前的内容,建议你点击下面文件夹中的章节先阅读其它内容再回到本文.訪问本系列文件夹.请点击:http://anb.io/bl ...

  2. 使用dom4j创建和解析xml

    之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码 package xml; import java.io.File; import java ...

  3. VMWare网络设置的3中方式(转)

    一 VMware网络设置的三种方式 1 Host-only连接方式  让虚机具有与宿主机不同的各自独立IP地址,但与宿主机位于不同网段,同时为宿主主机新增一个IP地址,且保证该IP地址与各虚机IP地址 ...

  4. 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 ...

  5. cocos2dx 3.2 定义自己使用rapidjson阅读json数据

    一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...

  6. SqlServer service broker 分布式系统(赵松桃)跳水 2005 数据库编程

    1.创建一个测试数据库 create database ServerbrokerTest on primary( name=ServerbrokerTest_data, filename='C:\Pr ...

  7. 问题:DataGrid该行并不总是很清楚验证错误(删除), 解决方案,如下面

    转载,收藏转载请注明出处http://blog.csdn.net/metal1/article/details/37568391 景象:于DataGrid进数据有误,行验证返回new Validati ...

  8. MySQL与逻辑模块

    启动MySQL 1.初始化模块运行&&存储引擎初始化运行 2.1中运行完毕后 ---->连接管理模块接手 3.连接管理模块启动处理client连接请求的监听程序(tcp/ip 网 ...

  9. WPF技术触屏上的应用系列(二): 嵌入百度地图、API调用及结合本地数据库在地图上进行自定义标点的实现

    原文:WPF技术触屏上的应用系列(二): 嵌入百度地图.API调用及结合本地数据库在地图上进行自定义标点的实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系 ...

  10. Java服务器下载速度的限制

    没有取之不尽,用之不竭的资源.server有限的带宽.运营商可以限制一点点.近期使用云存储openstack swift待办事项文件存储下载.如果第一个限速code: private Long wri ...