莫队算法链接:传送门

题意:

有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. SpringSecurity应用篇

    前面吹水原理吹了一篇幅了,现在讲解下应用篇幅,前面说过,如果要用SpringSecurity的话要先导入一个包 <dependency> <groupId>org.spring ...

  2. 立完flag,你可能需要对flag进行量化

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design Ng组件库:ng-devui(欢 ...

  3. Kafka底层原理剖析(近万字建议收藏)

    Kafka 简介 Apache Kafka 是一个分布式发布-订阅消息系统.是大数据领域消息队列中唯一的王者.最初由 linkedin 公司使用 scala 语言开发,在2010年贡献给了Apache ...

  4. Upload - Labs (下)

    Pass - 11: 1.查看源代码,发现进行了一次对后缀名替换成空格,因此考虑双写绕过, 2.上传成功, 关键代码: $is_upload = false; $msg = null; if (iss ...

  5. Centos6.9安装ACFS

    安装完oracle 11GR2的RAC后,使用asmca打开图形化界面后,发现Volumes和ASM Cluster File System两个选项卡不能用 原因是因为ACFS不支持CentOS 解决 ...

  6. APM调用链产品对比

    APM调用链产品对比 随着企业经营规模的扩大,以及对内快速诊断效率和对外SLA(服务品质协议,service-level agreement)的追求,对于业务系统的掌控度的要求越来越高,主要体现在: ...

  7. linux opt, usr文件夹说明

    linux下各文件夹介绍: https://www.pathname.com/fhs/pub/fhs-2.3.html /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为 ...

  8. Connections could not be acquired from the underlying database!

    Connections could not be acquired from the underlying database! 报错截图: 报错内容: Exception in thread &quo ...

  9. PE节表

  10. 前序遍历 排序 二叉搜索树 递归函数的数学定义 return 递归函数不能定义为内联函数 f(x0)由f(f(x0))决定

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...