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

时间:2022-09-09 10:45:31

给一段长为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 &amp&semi;&amp&semi; Little Elephant and Array &amp&semi;&amp&semi; Little Elephant and Array &amp&semi;&amp&semi; D-query &amp&semi;&amp&semi; Powerful array &amp&semi;&amp&semi; Fast Queries &lpar;莫队&rpar;

    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&period; Little Elephant and Array

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

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

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

  7. codeforces 220B &period; Little Elephant and Array 莫队&plus;离散化

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

  8. CodeForces - 220B Little Elephant and Array &lpar;莫队&plus;离散化 &sol; 离线树状数组)

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

  9. Codeforces 86D - Powerful array&lpar;莫队算法&rpar;

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

随机推荐

  1. 高级sql注入

    1. 避开输入过滤 输入过滤存在于外部和内部,外部属于web应用防火墙WAF,入侵防御系统IPS,入侵检测系统IDS,内部属于代码中对输入进行过滤 过滤select,insert等sql关键字和' | ...

  2. Ext&period;GridPanel 用法总结(一)—— Grid基本用法

    Ext.GridPanel 用法总结(一)—— Grid基本用法 摘自:http://www.cnblogs.com/luluping/archive/2009/08/01/1536645.html ...

  3. IPC&dollar;命令详解

    一 摘要二 什么是ipc$三 什么是空会话四 空会话可以做什么五 ipc$所使用的端口六 ipc管道在hack攻击中的意义七 ipc$连接失败的常见原因八 复制文件失败的原因九 关于at命令和xp对i ...

  4. typedef的使用2——定义函数

    #include <stdio.h> #include <string.h> #pragma warning(disable:4996) //闲言碎语都先不要讲了,直接上函数吧 ...

  5. yum安装软件时提示软件包没有签名

    yum install [XXX] -y --nogpgcheck

  6. poj2689:素数筛

    题目大意,给定l和u,求区间[l,u]内的素数中,相邻两个差最大和最小的素数其中 u的范围达到了2e9本质上需要找出n以内的所有素数,使用筛法.先保存50000(大于sqrt(2e9))内的所有素数, ...

  7. 局域网内IP冲突怎么办

      对于在Internet和Intranet网络上,使用TCP/IP协议时每台主机必须具有独立的IP地址,有了IP地址的主机才能与网络上的其它主机进行通讯.但IP地址冲突会造成网络客户不能正常工作,只 ...

  8. 超小Web手势库AlloyFinger原理&lpar;转载&rpar;

    目前AlloyFinger作为腾讯手机QQ web手势解决方案,在各大项目中都发挥着作用. 感兴趣的同学可以去Github看看: https://github.com/AlloyTeam/AlloyF ...

  9. C语言操作符

    C语言操作符的分类: 算术操作符 逻辑运算符 位操作符     赋值操作符 单目操作符 关系操作符 条件操作符 逗号表达式 数组下标引用 函数调用 结构体成员使用 大体上,C语言的操作符具体就这么些, ...

  10. js获取上传图片大小&comma;判断上传图片类型&comma;获取图片真实宽度和高度

    html部分 <div class="form-group col-md-12"> <label class="col-md-2 text-right& ...