Advertisement
yuhung94

b417. 區間眾數

Dec 13th, 2022
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #pragma GCC optimzize("Ofast,no-stack-protector")
  2. #include<bits/stdc++.h>
  3. #define int long long
  4. #define quick ios::sync_with_stdio(0);cin.tie(0);
  5. #define rep(x,a,b) for(int x=a;x<=b;x++)
  6. #define repd(x,a,b) for(int x=a;x>=b;x--)
  7. #define lowbit(x) (x&-x)
  8. #define sz(x) (int)(x.size())
  9. #define F first
  10. #define S second
  11. #define all(x) x.begin(),x.end()
  12. #define mp make_pair
  13. #define eb emplace_back
  14. using namespace std;
  15. typedef complex<int> P;
  16. #define X real()
  17. #define Y imag()
  18. typedef pair<int,int> pii;
  19. void debug(){
  20.     cout<<"\n";
  21. }
  22. template <class T,class ... U >
  23. void debug(T a, U ... b){
  24.     cout<<a<<" ",debug(b...);
  25. }
  26. const int N=2e5+7;
  27. const int INF=1e18;
  28. int s[N];
  29. int cnt[N];
  30. int num[N];
  31. int Max=0;
  32. void add(int x){
  33.     if(!x) return ;
  34.     int c0=cnt[s[x]]++;
  35.     num[c0]--;
  36.     num[c0+1]++;
  37.     if(Max<c0+1) Max=c0+1;
  38. }
  39. void sub(int x){
  40.     if(!x) return ;
  41.     int c0=cnt[s[x]]--;
  42.     num[c0]--;
  43.     num[c0-1]++;
  44.     if(!num[Max]) Max--;
  45. }
  46. const int Q=1e6+7;
  47. pii ans[Q];
  48. int K;
  49. struct query{
  50.     int l;
  51.     int r;
  52.     int pos;
  53.     int block;
  54.     inline bool operator < (const query&o) const{
  55.         if(block!=o.block) return block<o.block;
  56.         //if(block&1) return r>o.r;
  57.         return r<o.r;
  58.  
  59.     }
  60. }qry[Q];
  61. signed main(){
  62.     quick
  63.     int n,q;
  64.     cin>>n>>q;
  65.     K=n/sqrt(q*1.0);
  66.     rep(i,1,n) cin>>s[i];
  67.     rep(i,0,q-1){
  68.         cin>>qry[i].l>>qry[i].r;
  69.         qry[i].pos=i;
  70.         qry[i].block=qry[i].l/K;
  71.     }
  72.     sort(qry,qry+q);
  73.     Max=0;
  74.     int l=0;
  75.     int r=-1;
  76.     rep(i,0,q-1){
  77.         while(r<qry[i].r) add(++r);
  78.         while(r>qry[i].r) sub(r--);
  79.         while(l<qry[i].l) sub(l++);
  80.         while(l>qry[i].l) add(--l);
  81.         ans[qry[i].pos]=mp(Max,num[Max]);
  82.     }
  83.     rep(i,0,q-1){
  84.         cout<<ans[i].F<<" "<<ans[i].S<<"\n";
  85.     }
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement