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

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
LL n, m;
LL pos[maxn], c[maxn], s[maxn], ans;
LL pre[maxn], t[maxn];
struct node
{
LL l, r, res;
int id;
}Node[maxn]; bool cmp(node x, node y)
{
if(pos[x.l] == pos[y.l]) return x.r < y.r;
return x.l < y.l;
} bool cmp_id(node x, node y)
{
return x.id < y.id;
} void update(int p, int add)
{
if(add == )
{
s[c[p]]++;
if(s[c[p]] == pre[p])
ans++;
else if(s[c[p]] == pre[p] + )
ans--; }
else
{
s[c[p]]--;
if(s[c[p]] == pre[p])
ans++;
else if(s[c[p]] + == pre[p])
ans--;
}
} int main()
{
ans = ;
scanf("%lld%lld", &n, &m);
for(int i=; i<=n; i++)
{
scanf("%lld", &c[i]);
pre[i] = t[i] = c[i];
}
sort(t+, t+n+);
int len = unique(t+, t+n+) - (t+);
for(int i=; i<=n; i++)
c[i] = lower_bound(t+, t+len+, c[i]) - t;
int block = sqrt(n);
for(int i=; i<=n; i++)
pos[i] = (i-)/block + ;
for(int i=; i<=m; i++)
{
scanf("%lld%lld", &Node[i].l, &Node[i].r);
Node[i].id = i;
}
sort(Node+, Node+m+, cmp);
for(int i=, l=, r=; i<=m; i++)
{
for(; r < Node[i].r; r++)
update(r+, );
for(; r > Node[i].r; r--)
update(r, -);
for(; l < Node[i].l; l++)
update(l, -);
for(; l > Node[i].l; l--)
update(l-, ); Node[i].res = ans;
}
sort(Node+, Node+m+, cmp_id);
for(int i=; i<=m; i++)
cout<< Node[i].res <<endl; return ;
}

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. Makefile详解

    原文链接:https://blog.csdn.net/qq_38646470/article/details/79917494 专栏链接:https://blog.csdn.net/column/de ...

  2. EasyUI系列学习笔记(一)——注册

    前面介绍过EasyUI是一个前段框架,开发之前需要导入底层包:我这里采用的是EasyUI 1.4版本~ 今天主要是搭建一个EasyUI的环境,同时做一个登陆页面... 环境搭建 导入需要的文件到项目中 ...

  3. 利用VS2015开发python版本的caffe应用

    打开VS2015,选择“新建项目”->“其它语言”->“python”,VS会提示你安装PTVS(Python Tools for Visual Studio)插件,安装完毕后即可开始py ...

  4. 脚本处理iOS的Crash日志

    背景 当我们打包app时,可以选择生成对应的符号表,其保存 16 进制函数地址映射信息,通过给定的函数起始地址和偏移量,可以对应函数具体信息以供分析. 所以我们拿到测试给的闪退日志(.crash)时, ...

  5. CsvReader和CsvWriter操作csv文件

    使用方法: 提供把实例数据输出到磁盘csv文件的功能 提供读取csv文件,并封装成指定实例的功能 小工具自己依赖了slf4j+logbak,以及fastJson,如果与系统冲突,可以在pom文件中去除 ...

  6. mysql添加一个字段(

    mysql添加一个字段(在指定的一个字段后面) 举个栗子:alter table inquiry add error_code varchar(3) after add_time; 说明:alter ...

  7. Tomcat部署与使用

    Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...

  8. java工程文件路径的问题

    String classpath = this.getClass().getResource("/").getPath().replaceFirst("/WEB-INF/ ...

  9. 10分钟入门git简易教程

    在注册了github账号之后,一度不知道该如何使用. 在仔细研究了github的官方说明文档.廖老师的教程.还有许多博主的文章之后,总算对github的操作和体系有了较为深刻的了解,还有这篇简单的入门 ...

  10. Visual Studio AI配置环境

    参考链接:http://www.cnblogs.com/ms-uap/p/9123033.html 背景: 1.能联网的电脑:Win7 64 SP1 2.鼠标.键盘.显示器好使 3.已安装VS2010 ...