Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- theory from here:
- https://blog.anudeep2011.com/mos-algorithm/
- */
- // soluton of problem : http://lightoj.com/volume_showproblem.php?problem=1188
- #include<bits/stdc++.h>
- using namespace std;
- #define si(n) scanf("%d",&n)
- #define f first
- #define s second
- #define mp(a,b) make_pair(a,b)
- #define MAX 100005
- vector<pair<int,int> >quary,original;
- int arr[MAX],cnt[MAX];
- int n,t,q,block;
- map<pair<int,int>,int>mm;
- bool compare(pair<int,int>x, pair<int,int>y)
- {
- // Different blocks, sort by block.
- if (x.f/block != y.f/block)
- return x.f/block < y.f/block;
- // Same block, sort by R value
- return x.s < y.s;
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- int i,j,k;
- si(t);
- for(int cs=1;cs<=t;cs++){
- mm.clear();
- quary.clear();
- original.clear();
- //memset(arr,0,sizeof(arr));
- memset(cnt,0,sizeof(cnt));
- si(n);si(q);
- for(i=1;i<=n;i++)
- si(arr[i]);
- for(i=1;i<=q;i++){
- int l,r;
- si(l);si(r);
- quary.push_back(mp(l,r));
- original.push_back(mp(l,r));
- }
- block=sqrt(n);
- sort(quary.begin(),quary.end(),compare);
- int curr_l=1,curr_r=1,ans=1;
- cnt[arr[1]]++;
- for(i=0;i<quary.size();i++){
- int l=quary[i].f,r=quary[i].s;
- while(curr_l<l){
- cnt[arr[curr_l]]--;
- if(!cnt[arr[curr_l]])ans--;
- curr_l++;
- }
- while(curr_l>l){
- curr_l--;
- cnt[arr[curr_l]]++;
- if(cnt[arr[curr_l]]==1)ans++;
- }
- while(curr_r<r){
- //cout<<curr_r<<endl;
- curr_r++;
- cnt[arr[curr_r]]++;
- if(cnt[arr[curr_r]]==1)ans++;
- }
- while(curr_r>r){
- cnt[arr[curr_r]]--;
- if(!cnt[arr[curr_r]])ans--;
- curr_r--;
- }
- //cout<<l<<' '<<r<<' '<<curr_l<<' '<<curr_r<<endl;
- mm[mp(l,r)]=ans;
- }
- printf("Case %d:\n",cs);
- for(i=0;i<original.size();i++){
- printf("%d\n",mm[mp(original[i].f,original[i].s)]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement