题意:

  m次查询。每次查询范围[L,R]中出现次数等于该数字的数字个数。

题解:

   由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n。对于r,每个块中因为同一块是按y排序,所以最多移动根号n;一共根号n个块。注意l和r要分开考虑。

   要注意的是这道题需要离散一下数据。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstring>
  6. using namespace std;
  7. const int maxn = 1e5+;
  8. int n, m;
  9. int l, r;
  10. int a[maxn], b[maxn], d[maxn];
  11. int blk;
  12. int blg[maxn];
  13. int ans;
  14. int sum[maxn];
  15. int out[maxn];
  16. struct node {
  17. int x, y, id;
  18. }c[maxn];
  19. bool cmp(node u, node v) {
  20. return (blg[u.x]==blg[v.x])?u.y<v.y:u.x<v.x;
  21. }
  22. void update(int x, int t) {
  23. sum[d[x]] += t;
  24. if(sum[d[x]] == a[x])
  25. ans++;
  26. else if(sum[d[x]] == a[x]+t)
  27. ans--;
  28.  
  29. }
  30. int main() {
  31. while(~scanf("%d%d", &n, &m)) {
  32. for(int i = ; i <= n; i++) {
  33. scanf("%d", &a[i]);
  34. b[i] = a[i];
  35. }
  36. sort(b+, b+n+);
  37. int num = unique(b+, b+n+)-b;
  38. for(int i = ; i <= n; i++) d[i] = lower_bound(b+, b+num+, a[i])-b;
  39. blk = sqrt(n);
  40. for(int i = ; i <= n; i++) blg[i] = (i-)/blk+;
  41. for(int i = ; i <= m; i++) {
  42. scanf("%d%d", &c[i].x, &c[i].y);
  43. c[i].id = i;
  44. }
  45. sort(c+, c+m+, cmp);
  46. l = ; r = ;
  47. ans = ;
  48. memset(sum, , sizeof(sum));
  49. for(int i = ; i <= m; i++) {
  50. while(l < c[i].x) update(l++, -);
  51. while(l > c[i].x) update(--l, );
  52. while(r < c[i].y) update(++r, );
  53. while(r > c[i].y) update(r--, -);
  54. out[c[i].id] = ans;
  55. }
  56. for(int i = ; i <= m; i++) printf("%d\n", out[i]);
  57. }
  58. return ;
  59. }

Codeforces - 220B Little Elephant and Array(莫队模板题)的更多相关文章

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

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

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

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

  3. CODEFORCES 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  4. 【洛谷2709】小B的询问(莫队模板题)

    点此看题面 大致题意: 有一个长度为\(N\)的序列,每个数字在\(1\sim K\)之间,有\(M\)个询问,每个询问给你一个区间,让你求出\(\sum_{i=1}^K c(i)^2\),其中\(c ...

  5. [SDOI2009][bzoj1878] HH的项链 [莫队模板题]

    题面: 传送门 思路: 就是一道莫队的模板题目...... 开一个1000000的数组记录每个数出现的次数,然后每次从1到0或者从0到1更新答案 莫队讲解看这里:莫队 Code: #include&l ...

  6. Codeforces 220B - Little Elephant and Array 离线树状数组

    This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...

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

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

  8. P2709 小B的询问——普通莫队&&模板

    普通莫队概念 莫队:莫涛队长发明的算法,尊称莫队.其实就是优化的暴力. 普通莫队只兹磁询问不支持修改,是离线的. 莫队的基本思想:就是假定我得到了一个询问区间[l,r]的答案,那么我可以在极短(通常是 ...

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

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

随机推荐

  1. ES6初识-Decorator

    开始先按照个插件 npm install babel-plugin-transform-decorators-lagacy --save-dev 1.扩充和修改类的行为 2.修改的行为@readonl ...

  2. Java中使用webSocket

    Java中使用webSocket package com.yaoqi.controller.message; import javax.websocket.*; import javax.websoc ...

  3. ethereum(以太坊)(十一)--字节数组(一)

    pragma solidity ^0.4.0; contract byte1{ /* 固定大小字节数组(Fixed-size byte arrays) 固定大小字节数组可以通过bytes1,bytes ...

  4. c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】

    引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. ---------- ...

  5. bash:/usr/bin/mogod/:connot execute binary:exec fotmat error

    前两天博主在安装mogodb的时候出现以下错误,很是郁闷,明明按照教程里面做的,怎么到最后 执行命令的时候出错了呢,以下为错误execute binary:exec fotmat error" ...

  6. 硬盘安装Windows Server 2008(解决系统盘符变成D盘)

    硬盘安装Windows 2008系统方法 操作系统最好用的无疑是server 2003,但是现在Server 2003支持的软件越来越少,很多是故意不支持Server 2003了, 像php5.5以上 ...

  7. cf978E Bus Video System

    The busses in Berland are equipped with a video surveillance system. The system records information ...

  8. url_maneger.py

    coding=UTF-8 # url管理器 class urlManeger: def __init__(self): self.new_urls = set() self.old_urls = se ...

  9. 使用Yeoman自动构建Ember项目

    Yeoman是什么? Yeoman按照官方说法,它不只是一个工具,还是一个工作流.它其实包括了三个部分yo.grunt.bower,分别用于项目的启动.文件操作.包管理. Yo: Yo是一个项目初始化 ...

  10. Myeclipse代码格式化的样式和保存自动格式化

    第一种方法:下载格式化代码样式文件,参考这位老兄的方法(包含了保存自动格式化):http://blog.csdn.net/u010028869/article/details/49780515   下 ...