Advertisement
Saleh127

SPOJ Most Frequent Value _FREQ2 / Sqrt Decomposition

Jul 19th, 2022
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. /***
  2.  created: 2022-07-20-02.10.30
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll int
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. struct queris
  17. {
  18.     ll left,right,in;
  19.  
  20. }queri[100005];
  21.  
  22. ll block=500,mx=0;
  23.  
  24. ll a[100005],f[100005],fc[100005],ans[100005];
  25.  
  26. bool cmp(queris A, queris B)
  27. {
  28.   if (A.left / block != B.left / block ) return A.left < B.left;
  29.   return (A.right < B.right)^(A.left/block%2);
  30. }
  31.  
  32. void add(ll ind)
  33. {
  34.     fc[f[a[ind]]]--;
  35.     f[a[ind]]++;
  36.     fc[f[a[ind]]]++;
  37.     if(fc[mx+1]>0) mx++;
  38. }
  39.  
  40. void del(ll ind)
  41. {
  42.     fc[f[a[ind]]]--;
  43.     f[a[ind]]--;
  44.     fc[f[a[ind]]]++;
  45.     if(fc[mx]==0) mx--;
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     ios_base::sync_with_stdio(0);
  52.     cin.tie(0);
  53.     cout.tie(0);
  54.  
  55.     ll n,m,i,j,q,k,l,r;
  56.  
  57.     cin>>n>>q;
  58.  
  59.     for(i=0;i<n;i++)
  60.     {
  61.         cin>>a[i];
  62.     }
  63.  
  64.     for(i=0;i<q;i++)
  65.     {
  66.         cin>>queri[i].left>>queri[i].right;
  67.         queri[i].in=i;
  68.     }
  69.  
  70.     sort(queri,queri+q,cmp);
  71.  
  72.     l=0,r=-1;
  73.  
  74.     for(i=0;i<q;i++)
  75.     {
  76.         ll cl=queri[i].left;
  77.         ll cr=queri[i].right;
  78.         ll ci=queri[i].in;
  79.  
  80.         while(r<cr) add(++r);
  81.         while(l>cl) add(--l);
  82.         while(r>cr) del(r--);
  83.         while(l<cl) del(l++);
  84.  
  85.         ans[ci]=mx;
  86.     }
  87.  
  88.     for(i=0;i<q;i++) cout<<ans[i]<<nl;
  89.  
  90.     get_lost_idiot;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement