给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <cmath>
  12. #define MOD 2018
  13. #define LL long long
  14. #define ULL unsigned long long
  15. #define Pair pair<int, int>
  16. #define mem(a, b) memset(a, b, sizeof(a))
  17. #define _ ios_base::sync_with_stdio(0),cin.tie(0)
  18. //freopen("1.txt", "r", stdin);
  19. using namespace std;
  20. const int maxn = , INF = 0x7fffffff;
  21. LL n, m;
  22. LL pos[maxn], c[maxn], s[maxn], ans;
  23. LL pre[maxn], t[maxn];
  24. struct node
  25. {
  26. LL l, r, res;
  27. int id;
  28. }Node[maxn];
  29.  
  30. bool cmp(node x, node y)
  31. {
  32. if(pos[x.l] == pos[y.l]) return x.r < y.r;
  33. return x.l < y.l;
  34. }
  35.  
  36. bool cmp_id(node x, node y)
  37. {
  38. return x.id < y.id;
  39. }
  40.  
  41. void update(int p, int add)
  42. {
  43. if(add == )
  44. {
  45. s[c[p]]++;
  46. if(s[c[p]] == pre[p])
  47. ans++;
  48. else if(s[c[p]] == pre[p] + )
  49. ans--;
  50.  
  51. }
  52. else
  53. {
  54. s[c[p]]--;
  55. if(s[c[p]] == pre[p])
  56. ans++;
  57. else if(s[c[p]] + == pre[p])
  58. ans--;
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. ans = ;
  65. scanf("%lld%lld", &n, &m);
  66. for(int i=; i<=n; i++)
  67. {
  68. scanf("%lld", &c[i]);
  69. pre[i] = t[i] = c[i];
  70. }
  71. sort(t+, t+n+);
  72. int len = unique(t+, t+n+) - (t+);
  73. for(int i=; i<=n; i++)
  74. c[i] = lower_bound(t+, t+len+, c[i]) - t;
  75. int block = sqrt(n);
  76. for(int i=; i<=n; i++)
  77. pos[i] = (i-)/block + ;
  78. for(int i=; i<=m; i++)
  79. {
  80. scanf("%lld%lld", &Node[i].l, &Node[i].r);
  81. Node[i].id = i;
  82. }
  83. sort(Node+, Node+m+, cmp);
  84. for(int i=, l=, r=; i<=m; i++)
  85. {
  86. for(; r < Node[i].r; r++)
  87. update(r+, );
  88. for(; r > Node[i].r; r--)
  89. update(r, -);
  90. for(; l < Node[i].l; l++)
  91. update(l, -);
  92. for(; l > Node[i].l; l--)
  93. update(l-, );
  94.  
  95. Node[i].res = ans;
  96. }
  97. sort(Node+, Node+m+, cmp_id);
  98. for(int i=; i<=m; i++)
  99. cout<< Node[i].res <<endl;
  100.  
  101. return ;
  102. }

Little Elephant and Array CodeForces - 220B(莫队)的更多相关文章

  1. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  2. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  3. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  4. XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

    CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j) ...

  5. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  6. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  7. codeforces 220B . Little Elephant and Array 莫队+离散化

    传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...

  8. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  9. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

随机推荐

  1. futuba R70085SB 接收机 只有SBus端口有输出其他端口输出不变

    接收机能收到遥控器信号,且Sbus端口有输出,其他端口输出没有变化,这时你要看一下接收机的模式了,肯定是遥控器的通道跟输出端口的通道不匹配.参考Futuba T14SG遥控器的说明书,如下图

  2. 【TestNG测试】TestNG、Maven、testng.xml构建测试工程

    创建一个maven工程 使用Idea创建maven工程 ​ 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...

  3. linux文件种类及其扩展名

    文件种类 普通文件(ls -al出来第一列为-) 纯文本文件(ASCII):linux系统中最多的一种文件类型,可以使用cat直接读取: 二进制文件(binary):linux下面的可执行文件: 数据 ...

  4. vue 组件-父组件传值给子组件

    父组件通过属性,传值给子组件,子组件通过,props数组里的名称来接受父组件传过来的值. HTML部分: <div id="app"> <tmp1 :parent ...

  5. 在IIS中部署Asp.Net网站

    在IIS中部署Asp.Net网站 1.添加IIS或者删除IIS,在控制面板=>程序和功能=>打开或关闭功能 启动iis,右键计算机=>管理=>服务和应用程序=>Inter ...

  6. Python处理PDF和Word文档常用的方法

    Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...

  7. Spring的bean创建详解

           IoC容器,又名控制反转,全称为Inverse of Control,其是Spring最为核心的一个组件,其他的组件如AOP,Spring事务等都是直接或间接的依赖于IoC容器的.本文主 ...

  8. js备忘录6

  9. PSP总结

    一.回顾1 (1)回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 以前的回答:当初之所以选择计算机专业,是因为我比较喜欢数学,觉得计 ...

  10. Hibernate笔记④--一级二级缓存、N+1问题、saveorupdate、实例代码

    一级缓存及二级缓存 一级缓存也是Session 缓存     一个链接用户的多次查询使用缓存     跨用户 则无缓存     hibernate自带的 get和load都会填充并利用一级缓存 二级缓 ...