-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmo.cpp
111 lines (81 loc) · 1.56 KB
/
mo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//Program to find no. of distinct elements in the range[l,r] efficiently using mo's algorithm
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int block_size;
int MAX=1000000;
//structure for one query
//index is to store actual index of query
struct query
{
int l, r , index, result;
};
//to sort queries according to mo's algorithm
bool mosort(query x, query y)
{
if(x.l/block_size != y.l/block_size)
return x.l/block_size < y.l/block_size;
return x.r < y.r;
}
//for actual sorting if queries
bool actualsort(query x, query y)
{
return x.index < y.index;
}
//find, store and print result of queries
void distinct(int *a, int n, query q[], int m)
{
block_size = int(sqrt(n));
sort(q,q+m,mosort);
int cl=0,cr=0,count=0;
int freq[MAX]={0};
for (int i = 0; i < m; ++i)
{
int l=q[i].l, r=q[i].r;
while(cl<l)
{
freq[a[cl]]--;
if(freq[a[cl]]==0)
count--;
cl++;
}
while(cl>l)
{
freq[a[cl-1]]++;
if(freq[a[cl--]]==1)
count++;
cl--;
}
while(cr<=r)
{
freq[a[cr]]++;
if(freq[a[cr]]==1)
count++;
cr++;
}
while(cr>=r+1)
{
freq[a[cr-1]]--;
if(freq[a[cr-1]]==0)
count--;
cr--;
}
q[i].result=count;
}
sort(q,q+m,actualsort);
for (int i = 0; i < m; ++i)
{
cout<<"The no. of distinct elements between "<<q[i].l<<" and "<<q[i].r-1<<" is "<<q[i].result<<endl;
}
}
//driver function
int main()
{
int a[]={1,2,3,3,4,6,5,4,4,7};
query q[]={{0,9,0,0},{0,5,1,0}};
int n = sizeof(a)/sizeof(a[0]);
int m = sizeof(q)/sizeof(q[0]);
distinct(a,n,q,m);
return 0;
}