Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Mo's Algorithm and Sqrt-Decomposition Folder #67

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Square Root Decomposition/MO's Algorithm/MOAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//Problem : http://www.spoj.com/problems/DQUERY/
//Time Complexity : O((n+q)*sqrt(n))

#include <bits/stdc++.h>

#define ll long long int
#define mod 1000000007
#define show(a) for(i=0;i<a.size();i++) cout<<a[i]<<" ";
#define fi first
#define se second
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long int>
#define pb push_back
#define pi pair<int,int>
#define si set<int>
#define sll set<ll>
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
#define mp make_pair
#define fast_io() cin.sync_with_stdio(false);cout.sync_with_stdio(false);
#define long_zero 0ll
#define long_one 1ll
inline int sbt(int x){return __builtin_popcount(x);}
using namespace std;
int freq[1111111];
int BLOCK;
//Mo Sorting
bool f(pair<int,pi> a, pair<int,pi> b){
if(a.se.fi/BLOCK == b.se.fi/BLOCK)
return a.se.se>b.se.se;
return a.se.fi/BLOCK>b.se.fi/BLOCK;
}
int main() {
//fast_io()
int n;
scanf("%d",&n);
int a[n+3];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int l,r,s=0,e=0;
int Q;
vector<pair<int,pi>>q;
scanf("%d",&Q);
//block size:SQRT(N)
BLOCK = floor(sqrt(1.0*double(n)));
for(int i=0;i<Q;i++){
scanf("%d%d",&l,&r);
q.pb(mp(i,mp(l-1,r-1)));
}
int v[Q+4],ans=0;
sort(q.begin(),q.end(),f); //f is comparator
for(int i=0;i<Q;i++){
l=q[i].se.fi;
r=q[i].se.se;
while(s<l){
freq[a[s]]--;
if(!freq[a[s]])
ans--;
s++;
}
while(s>l){
freq[a[s-1]]++;
if(freq[a[s-1]]==1)
ans++;
s--;
}
while(e<=r){
freq[a[e]]++; // mantains frequency
if(freq[a[e]]==1)
ans++;
e++;
}
while(e>r+1){
freq[a[e-1]]--;
if(freq[a[e-1]]==0)
ans--;
e--;
}
v[q[i].fi]=ans;
}
for(int i=0;i<Q;i++)
printf("%d\n",v[i]);
return 0;
}