From facfb2f35e2b3661f387afe5a0c805640aac49d9 Mon Sep 17 00:00:00 2001 From: Madhav Date: Mon, 28 Aug 2017 11:31:04 +0530 Subject: [PATCH] Added MO's algorithm --- Range queries/MO.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Range queries/MO.cpp diff --git a/Range queries/MO.cpp b/Range queries/MO.cpp new file mode 100644 index 000000000..2a2cbfffd --- /dev/null +++ b/Range queries/MO.cpp @@ -0,0 +1,75 @@ +#include "bits/stdc++.h" +using namespace std; +const int N = 1e6+5; +int a[N],bucket[N],cnt[N]; +int bucket_size; +struct query{ + int l,r,i; +}q[N]; +int ans=0; + +void add(int index) +{ + cnt[a[index]]++; + if(cnt[a[index]] == 1) + ans++; +} +void remove(int index) +{ + cnt[a[index]]--; + if(cnt[a[index]] == 0) + ans--; +} + + +bool mycmp(query x, query y) +{ + if(x.l/bucket_size != y.l/bucket_size) + return x.l/bucket_size < y.l/bucket_size; + return x.rL) + { + add(left-1); + left--; + } + while(right<=R) + { + add(right); + right++; + } + while(right>R+1) + { + remove(right-1); + right--; + } + bucket[q[i].i] = ans; + } + for(i=0;i