Little Elephant and Array CodeForces - 220B(莫队)
给一段长为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(莫队)的更多相关文章
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- 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)) 在 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和
CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! = j) ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
随机推荐
- futuba R70085SB 接收机 只有SBus端口有输出其他端口输出不变
接收机能收到遥控器信号,且Sbus端口有输出,其他端口输出没有变化,这时你要看一下接收机的模式了,肯定是遥控器的通道跟输出端口的通道不匹配.参考Futuba T14SG遥控器的说明书,如下图
- 【TestNG测试】TestNG、Maven、testng.xml构建测试工程
创建一个maven工程 使用Idea创建maven工程 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...
- linux文件种类及其扩展名
文件种类 普通文件(ls -al出来第一列为-) 纯文本文件(ASCII):linux系统中最多的一种文件类型,可以使用cat直接读取: 二进制文件(binary):linux下面的可执行文件: 数据 ...
- vue 组件-父组件传值给子组件
父组件通过属性,传值给子组件,子组件通过,props数组里的名称来接受父组件传过来的值. HTML部分: <div id="app"> <tmp1 :parent ...
- 在IIS中部署Asp.Net网站
在IIS中部署Asp.Net网站 1.添加IIS或者删除IIS,在控制面板=>程序和功能=>打开或关闭功能 启动iis,右键计算机=>管理=>服务和应用程序=>Inter ...
- Python处理PDF和Word文档常用的方法
Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...
- Spring的bean创建详解
IoC容器,又名控制反转,全称为Inverse of Control,其是Spring最为核心的一个组件,其他的组件如AOP,Spring事务等都是直接或间接的依赖于IoC容器的.本文主 ...
- js备忘录6
- PSP总结
一.回顾1 (1)回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 以前的回答:当初之所以选择计算机专业,是因为我比较喜欢数学,觉得计 ...
- Hibernate笔记④--一级二级缓存、N+1问题、saveorupdate、实例代码
一级缓存及二级缓存 一级缓存也是Session 缓存 一个链接用户的多次查询使用缓存 跨用户 则无缓存 hibernate自带的 get和load都会填充并利用一级缓存 二级缓 ...