-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinct Colors_Mo's.cpp
More file actions
126 lines (106 loc) · 3.16 KB
/
Distinct Colors_Mo's.cpp
File metadata and controls
126 lines (106 loc) · 3.16 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// using Mo's algorithm on trees
// Flatten the tree to an array using the same technique mentioned in solution of Subtree Queries.
// Then the subtree of each node will correspond to a contiguous subarray of flattened array.
// We can then use Mo's algorithm to answer each query in O(√n) time with O(n√n) preprocessing.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
const int N=2e5+1 ;
vector<int> adj[N] ;
int n , SQRT=700 ; // It's good to choose 700 & also greater than sqrt(n)
ll col[N]={} , res[N] , freq[N]={} , ans=0 ; // res[i] stores result of query i .
ll start[N],finish[N], flat_arr[2*N+1], timer=0 ; // start[i] - start time for node i in flat_arr
struct node{
ll L,R,pos ;
} query[N] ;
void map_colors(){ // convert larger values(like 1e9) into smaller values , in between 0 to n
map<ll,ll> mp ;
vector<ll> col_copy(col+1,col+1+n) ;
sort(col_copy.begin(),col_copy.end()) ;
for(ll i=0 ; i<n ; i++)
mp[col_copy[i]]=i ;
for(ll i=1 ; i<=n ; i++)
col[i] = mp[col[i]] ;
}
void Eular_tour_array(ll x,ll p){
start[x]=timer ;
flat_arr[timer]=col[x] ;
timer++ ;
for(ll i : adj[x]){
if (i!=p)
Eular_tour_array(i,x) ;
}
finish[x]=timer ;
flat_arr[timer]=col[x] ;
timer++ ;
}
bool cmp(node a, node b){
if (a.L/SQRT != b.L/SQRT)
return a.L < b.L ;
return a.R < b.R ;
}
void Add(ll pos){
freq[flat_arr[pos]]++ ;
if (freq[flat_arr[pos]]==1)
ans++ ;
}
void Remove(ll pos){
freq[flat_arr[pos]]-- ;
if (freq[flat_arr[pos]]==0)
ans-- ;
}
void mos_algorithm(){
sort(query+1,query+1+n,cmp) ;
ll currL=0 , currR=0 , L , R ;
for(ll i=1; i<=n ; i++){
L = query[i].L ;
R = query[i].R ;
while(currL<L){
Remove(currL) ;
currL++ ;
}
while(currL>L){
Add(currL-1) ; // currL is already included from previous query
if (currL==L) break ;
currL-- ;
}
while(currR>R){
Remove(currR) ;
currR-- ;
}
while(currR<R){
Add(currR+1) ; // currR is already included from previous query
if (currR==R) break ;
currR++ ;
}
res[query[i].pos]= ans ;
}
}
int main() {
IOS ;
ll x,y ;
cin>>n ;
SQRT = ceil(sqrt(2*n)) ;
for(ll i=1 ; i<=n ; i++){
cin>>col[i] ;
}
map_colors(); // convert larger values into smaller values , in between 0 to n
for(int i=1 ; i<n ; i++){
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
Eular_tour_array(1,-1) ;
// for(ll i=0 ; i<=2*n ; i++) cout<<flat_arr[i]<<" \n"[i==2*n] ;
for(int i=1 ; i<=n ; i++){
query[i].L = start[i] ;
query[i].R = finish[i] ;
query[i].pos = i ;
}
mos_algorithm();
for(ll i=1 ; i<=n ; i++)
cout<<res[i]<<" " ;
return 0;
}