ydvsahil

random_number_generator

Apr 22nd, 2021
953
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int a[300005];
  4. vector<int> v[300005];
  5. int main()
  6. {
  7.     int n,q;
  8.     scanf("%d%d",&n,&q);
  9.     for (int i=1;i<=n;i++)
  10.     {
  11.         scanf("%d",&a[i]);
  12.         v[a[i]].push_back(i);
  13.     }
  14.     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  15.     while (q--)
  16.     {
  17.         int l,r,ans=1;
  18.         scanf("%d%d",&l,&r);
  19.         for (int _=0;_<40;_++)
  20.         {
  21.             int c=a[uniform_int_distribution<int>(l,r)(rng)],f=upper_bound(v[c].begin(),v[c].end(),r)-lower_bound(v[c].begin(),v[c].end(),l);
  22.             ans=max(ans,2*f-(r-l+1));
  23.         }
  24.         printf("%d\n",ans);
  25.     }
  26. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×