CodeForces - 220B 离散化+莫队算法
莫队算法链接:传送门
题意:
有n个数,m个区间。问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3)。
题解:
因为a[i]太大,所以要离散化一下,但是不能用map容器,因为map容器多一个log
莫队就是离线问题+区间的移动。复杂度是O((N+M)*√N)
莫队代码还要分块要不然还会TLE,分块大小为sqrt(n)
未分块-TLE代码:
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 #include <cmath>
5 #include <cstdlib>
6 #include <algorithm>
7 #include <set>
8 #include <map>
9 #include <string>
10 #include <queue>
11 #include <stack>
12 #include <string>
13 using namespace std;
14 typedef long long ll;
15 using namespace std;
16 const int maxn=1e5+7;
17 int unit,n,m;
18 struct Node
19 {
20 int l,r,id,result;
21 }node[maxn];
22 bool mmp1(Node x,Node y)
23 {
24 if(x.l==y.l)
25 return x.r<y.r;
26 return x.l<y.l;
27 }
28 bool mmp2(Node x,Node y)
29 {
30 return x.id<y.id;
31 }
32 int a[maxn],b[maxn],num[maxn],pr[maxn],ans;
33 int add(int i)
34 {
35 num[a[i]]++;
36 if(num[a[i]]==b[a[i]]) ans++;
37 else if(num[a[i]]==b[a[i]]+1) ans--;
38 }
39 void del(int i)
40 {
41 num[a[i]]--;
42 if(num[a[i]]==b[a[i]]) ans++;
43 else if(num[a[i]]==b[a[i]]-1) ans--;
44 }
45 int main()
46 {
47 scanf("%d%d",&n,&m);
48 unit=(int)sqrt(n);
49 for(int i=1;i<=n;++i)
50 scanf("%d",&a[i]),b[i]=a[i];
51 sort(b+1,b+n+1);
52 int cnt=unique(b+1,b+1+n)-(b+1);
53 for(int i=1;i<=n;++i)
54 {
55 a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
56 }
57 for(int i=1;i<=m;++i)
58 scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i;
59 sort(node+1,node+1+m,mmp1);
60 int lpos=node[1].l,rpos=lpos-1;
61 for(int i=1;i<=m;++i)
62 {
63 while(lpos>node[i].l) add(--lpos);
64 while(rpos<node[i].r) add(++rpos);
65 while(lpos<node[i].l) del(lpos++);
66 while(rpos>node[i].r) del(rpos--);
67 node[i].result=ans;
68 }
69 sort(node+1,node+1+m,mmp2);
70 for(int i=1;i<=m;++i)
71 printf("%d\n",node[i].result);
72 return 0;
73 }
分块-正确代码:
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 #include <cmath>
5 #include <cstdlib>
6 #include <algorithm>
7 #include <set>
8 #include <map>
9 #include <string>
10 #include <queue>
11 #include <stack>
12 #include <string>
13 using namespace std;
14 typedef long long ll;
15 using namespace std;
16 const int maxn=1e5+7;
17 int unit,n,m;
18 struct Node
19 {
20 int l,r,id,result;
21 bool operator < (const Node x)const
22 {
23 return l/unit==x.l/unit?r<x.r:l<x.l;
24 }
25 }node[maxn];
26 //bool mmp1(Node x,Node y)
27 //{
28 // if(x.l==y.l)
29 // return x.r<y.r;
30 // return x.l<y.l;
31 //}
32 bool mmp2(Node x,Node y)
33 {
34 return x.id<y.id;
35 }
36 int a[maxn],b[maxn],num[maxn],pr[maxn],ans;
37 int add(int i)
38 {
39 num[a[i]]++;
40 if(num[a[i]]==b[a[i]]) ans++;
41 else if(num[a[i]]==b[a[i]]+1) ans--;
42 }
43 void del(int i)
44 {
45 num[a[i]]--;
46 if(num[a[i]]==b[a[i]]) ans++;
47 else if(num[a[i]]==b[a[i]]-1) ans--;
48 }
49 int main()
50 {
51 scanf("%d%d",&n,&m);
52 unit=(int)sqrt(n);
53 for(int i=1;i<=n;++i)
54 scanf("%d",&a[i]),b[i]=a[i];
55 sort(b+1,b+n+1);
56 int cnt=unique(b+1,b+1+n)-(b+1);
57 for(int i=1;i<=n;++i)
58 {
59 a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
60 }
61 for(int i=1;i<=m;++i)
62 scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i;
63 sort(node+1,node+1+m);
64 int lpos=node[1].l,rpos=lpos-1;
65 for(int i=1;i<=m;++i)
66 {
67 while(lpos>node[i].l) add(--lpos);
68 while(rpos<node[i].r) add(++rpos);
69 while(lpos<node[i].l) del(lpos++);
70 while(rpos>node[i].r) del(rpos--);
71 node[i].result=ans;
72 }
73 sort(node+1,node+1+m,mmp2);
74 for(int i=1;i<=m;++i)
75 printf("%d\n",node[i].result);
76 return 0;
77 }
CodeForces - 220B 离散化+莫队算法的更多相关文章
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树
题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- Powerful array CodeForces - 86D (莫队算法)
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
随机推荐
- 单片机—Arduino UNO-R3—学习笔记001
连接方法 下载Arduino软件 安装完成打开如图所示 观察右下角的连接接口"Arduino Uno在COM(X)" 在工具-->端口-->选择之前查看的端口 即为连接 ...
- 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案
错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...
- Dubbo中的统一契约是如何实现的?
写在前面 之前,很多小伙伴私信我:如何才能快速的掌握Dubbo的核心原理和源码.所以,我写了一篇<我是如何在短期内快速掌握Dubbo的原理和源码的(纯干货)?>.对于Dubbo的源码解析系 ...
- python_mmdt:一种基于敏感哈希生成特征向量的python库(一)
概述 python_mmdt是一种基于敏感哈希的特征向量生成工具.核心算法使用C实现,提高程序执行效率.同时使用python进行封装,方便研究人员使用. 本篇幅主要介绍涉及的相关基本内容与使用,相关内 ...
- Python爬虫学习笔记(一)
概念: 使用代码模拟用户,批量发送网络请求,批量获取数据. 分类: 通用爬虫: 通用爬虫是搜索引擎(Baidu.Google.Yahoo等)"抓取系统"的重要组成部分. 主要目的是 ...
- SQL Server 2012 忘记sa用户处理方法
SQL Server 2012 忘记sa用户的密码,可重置sa密码,方法如下: 1.将身份验证改成Windows身份验证,登录进去 2.进入SQL Server控制台,在对象资源管理器中找到Secur ...
- jQuery 多选与清除
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- NAT模式、路由模式、桥接模式的区别
NAT模式 NAT模式概述 NAT是"Network Address Translation"的缩写,中文意思是"网络地址转换",它允许一个整体机构以一个公用I ...
- HTML Standard系列:Event loop、requestIdleCallback 和 requestAnimationFrame
HTML Standard系列:Event loop.requestIdleCallback 和 requestAnimationFrame - 掘金 https://juejin.im/post/5 ...
- (Oracle)常用的数据库函数
Trim: Trim() 函数的功能是去掉首尾空格. Eg: trim(to_char(level, '00')) Trunc: 1.TRUNC函数为指定元素而截去的日期值. trun ...