给一段长为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. 通过Jmeter对Dubbo接口进行接口及性能测试

    dubbo接口/性能测试 dubbo简介 zookeeper简介.安装及配置 dubbo服务端demo dubbo客户端调用 jmeter工程改造及接口调用 读取jmeter参数用于dubbo性能测试 ...

  2. Python学习之路:NumPy进阶

    import numpy as np; #创建数组的四种办法 ##可以传入任何类数组 a = np.array([0,1,2,3,4]); b = np.array((0,1,2,3,4)); c = ...

  3. python OptionParser模块使用

    OptionParser是python中用来处理命令行的模块,在我们使用python进行流程化开发中必要的工具 Optparse,它功能强大,而且易于使用,可以方便地生成标准的.符合Unix/Posi ...

  4. CentOS7上部署ASP.Net Core 2.2应用

    前言 在CentOS7上部署ASP.Net Core应用是我的技术路线验证的一部分,下一个产品计划采用ASP.Net Boilerplate Framework开发.因此需要求提前进行一下技术验证,在 ...

  5. 011 --Mysql中特定查询

    1.优化COUNT()查询 COUNT()可能是被大家误解最多的函数了,它有两种不同的作用,其一是统计某个列值的数量,其二是统计行数.统计列值时,要求列值是非空的,它不会统计NULL.如果确认括号中的 ...

  6. 如何利用京东云的对象存储(OSS)上传下载文件

    作者:刘冀 在公有云厂商里都有对象存储,京东云也不例外,而且也兼容S3的标准因此可以利用相关的工具去上传下载文件,本文主要记录一下利用CloudBerry Explorer for Amazon S3 ...

  7. 04-matplotlib-柱形图

    import numpy as np import matplotlib.pyplot as plt # 柱形图 # 例一 N =5 y = [15,28,10,30,25] index = np.a ...

  8. 【quickhybrid】Android端的项目实现

    前言 前文中就有提到,Hybrid模式的核心就是在原生,而本文就以此项目的Android部分为例介绍Android部分的实现. 提示,由于各种各样的原因,本项目中的Android容器确保核心交互以及部 ...

  9. url传多值问题

    使用url传值的特点是操作简单,虽然安全性低,但依然广泛运用. url传数据绑定的值: <a href='Default.aspx?id=<%#Eval("ID")%& ...

  10. Parallel学习

    Parallel给cpu的核有关系,在Parallel中,写入需要并行执行的方法,比如:方法1需要3秒:方法2需要6秒:方法3需要9秒: 并行情况下,加上任务分配,上下文切换需要1秒,执行方法总耗时只 ...