莫队算法链接:传送门

题意:

有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 离散化+莫队算法的更多相关文章

  1. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  2. Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树

    题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...

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

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  4. Powerful array CodeForces - 86D (莫队算法)

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

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

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

  6. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  7. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 关于软件架构中的b/s

    **B/S架构 b/s只需要一个浏览器,用户就可以通过不同的网址访问不同的服务器程序. 优点:开发,安装,部署,维护简单 缺点:对硬件要求过高,用户的体验会受到影响 首先是资源分类:**可以分为静态资 ...

  2. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  3. 15V转5V转3.3V转3V芯片,DC-DC和LDO

    15V电压是属于一般电压,降压转成5V电压,3.3V电压和3V电压,适用于这个电压的DC-DC很多,LDO也是有可以选择的.LDO芯片如PW6206,PW8600等.DC-DC芯片如:PW2162,P ...

  4. 1V升3V芯片,1V升3.3V芯片,大电流的,低功耗

    一般来说,1V的电压实在很低了,即使是干电池的话,再1V时,也是基本属于没电状态了.还有一种是干电池输出电流大时,也会把干电池的电压从1.5V拉低到1V左右. 更多的是客户对于1V时要能升到3V或者3 ...

  5. C#从入门到放弃治疗一:初探C#世界

    C#是一款高级的面向对象语言,运行于.NET framework之上的高级程序设计语言.其语言规范和,语法和java有着惊人的类似之处.所以如果你在学习C#之前有着java的基础,你将快速地入门.当然 ...

  6. 翻译 - ASP.NET Core 基本知识 - 通用主机 (Generic Host)

    翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0 ...

  7. Android 开发学习进程0.27 kotlin使用 和viewbinding的使用

    kotlin-android-extensions 插件被废弃 笔者曾经尝试写过一部分的kotlin代码 主要是项目中一些代码是kotlin完成的,其中我认为 kotlin的kotlin-androi ...

  8. OpenDaylight — YANG

    1. 介绍 YANG 是一种用于为 NETCONF 协议建模数据的语言. YANG 将数据的层次结构建模为一棵树. 2. 节点类型 2.1 leaf 它只有一个特定类型的值,并且没有子节点. YANG ...

  9. GDB 简单学习

    一般来说,GDB主要帮忙你完成下面四个方面的功能:       1.启动你的程序,可以按照你的自定义的要求随心所欲的运行程序.     2.可让被调试的程序在你所指定的调置的断点处停住.(断点可以是条 ...

  10. Google performance Tools (gperftools) 使用心得

    Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器 ...