Advertisement
Emiliatan

Mo algorithm

Mar 2nd, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. const int MAXQ = 1000001, MAXN = 100001, MAXNUM = 100001;
  8.  
  9. struct Query
  10. {
  11.     int l, r, Qid, block;
  12.     bool operator < (const Query &b) const
  13.     {
  14.         if(block == b.block)
  15.             if(block & 1) return r < b.r;
  16.             else return r > b.r;
  17.  
  18.         return block < b.block;
  19.     }
  20. }qry[MAXQ];
  21.  
  22. int N, Q, unit, maxcur = -1;
  23. int arr[MAXN], app[MAXNUM] {}, cnt[MAXNUM] {};
  24. int modeTimes[MAXQ], modeCnts[MAXQ];
  25.  
  26. void add(int x)
  27. {
  28.     cnt[app[x]]--;
  29.     app[x]++;
  30.     cnt[app[x]]++;
  31.     maxcur = max(maxcur, app[x]);
  32. }
  33. void sub(int x)
  34. {
  35.     cnt[app[x]]--;
  36.     app[x]--;
  37.     cnt[app[x]]++;
  38.     if(!cnt[maxcur]) maxcur--;
  39. }
  40. void solve()
  41. {
  42.     sort(qry + 1, qry + 1 + Q);
  43.     cnt[0] = N;
  44.     for(int i = 1, cL = 1, cR = 0;i <= Q; i++)
  45.     {
  46.         while(qry[i].r > cR)
  47.             add(arr[++cR]);
  48.  
  49.         while(qry[i].l < cL)
  50.             add(arr[--cL]);
  51.  
  52.         while(qry[i].r < cR)
  53.             sub(arr[cR--]);
  54.  
  55.         while(qry[i].l > cL)
  56.             sub(arr[cL++]);
  57.  
  58.         modeTimes[qry[i].Qid] = maxcur;
  59.         modeCnts[qry[i].Qid] = cnt[maxcur];
  60.     }
  61.     for(int i = 1; i <= Q; i++) printf("%d %d\n",modeTimes[i],modeCnts[i]);
  62. }
  63. int main()
  64. {
  65.     scanf("%d %d", &N, &Q);
  66.  
  67.     unit = sqrt(N);
  68.     for(int i = 1;i <= N && scanf("%d", &arr[i]); i++);
  69.  
  70.     for(int i = 1, L, R; i <= Q; i++)
  71.     {
  72.         scanf("%d %d", &L, &R);
  73.         if(L > R) swap(L, R);
  74.         qry[i].Qid = i;
  75.         qry[i].l = L;
  76.         qry[i].r = R;
  77.         qry[i].block = L / unit;
  78.     }
  79.     solve();
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement