题意:

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

题解:

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

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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e5+;
int n, m;
int l, r;
int a[maxn], b[maxn], d[maxn];
int blk;
int blg[maxn];
int ans;
int sum[maxn];
int out[maxn];
struct node {
int x, y, id;
}c[maxn];
bool cmp(node u, node v) {
return (blg[u.x]==blg[v.x])?u.y<v.y:u.x<v.x;
}
void update(int x, int t) {
sum[d[x]] += t;
if(sum[d[x]] == a[x])
ans++;
else if(sum[d[x]] == a[x]+t)
ans--; }
int main() {
while(~scanf("%d%d", &n, &m)) {
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b+, b+n+);
int num = unique(b+, b+n+)-b;
for(int i = ; i <= n; i++) d[i] = lower_bound(b+, b+num+, a[i])-b;
blk = sqrt(n);
for(int i = ; i <= n; i++) blg[i] = (i-)/blk+;
for(int i = ; i <= m; i++) {
scanf("%d%d", &c[i].x, &c[i].y);
c[i].id = i;
}
sort(c+, c+m+, cmp);
l = ; r = ;
ans = ;
memset(sum, , sizeof(sum));
for(int i = ; i <= m; i++) {
while(l < c[i].x) update(l++, -);
while(l > c[i].x) update(--l, );
while(r < c[i].y) update(++r, );
while(r > c[i].y) update(r--, -);
out[c[i].id] = ans;
}
for(int i = ; i <= m; i++) printf("%d\n", out[i]);
}
return ;
}

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. MySQL 5.7传统复制到GTID在线切换(一主一从)

    Preface       Classic replication is commonly used in previous version of MySQL.It's really tough in ...

  2. django+xadmin在线教育平台(十三)

    这个6-8对应对应6-11,6-12 拷入forgetpassword页面 书写处理忘记密码的view users/views.py # 用户忘记密码的处理view class ForgetPwdVi ...

  3. java 动态生成SQL

    代码如下: /** * 动态生成SQ及SQL参数L * @param ve 接收到的消息的CHGLIST * @param paramList MQ消息中的SQL参数 * @param t 泛型对象 ...

  4. html页面 加载完成后再刷新 一次

    主要用于第一次加载页面有部分加载bug,再刷新一次即可正常运行. 简单粗暴直接上代码,不带参数,0影响 <Script>function refresh(){ url = location ...

  5. python之doctest的用法

    doctest是python自带的一个模块,你可以把它叫做“文档测试”(doctest)模块. doctest的使用有两种方式:一个是嵌入到python源中.另一个是放到一个独立文件. doctest ...

  6. python 函数复习

    # 函数 # 可读性强 复用性强 # def 函数名(): # 函数体 #return 返回值 # 所有的函数 只定义不调用就一定不执行 #先定义后调用 #函数名() #不接收返回值 #返回值 = 函 ...

  7. 菜鸟教程perl总结

    数据类型有:  标量$, 数组@,哈希% 数组声明 :  @hits = (25, 30, 40);  或者  @sites = qw/google taobao runoob/; 数组操作 pop, ...

  8. 流量操控之SSH隧道与端口转发

    目  录 第1章            概述... 3 1.1.         实现命令... 3 1.2.         SSH隧道类型... 3 第2章            SSH隧道... ...

  9. 7 Django的模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...

  10. Spring---单例模式(Singleton)的6种实现

    1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...