Advertisement
yungyao

tioj 2122

Dec 28th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #define pb push_back
  7. #include <utility>
  8. #define pii pair<int,int>
  9.  
  10. struct Qs{
  11.     int id;
  12.     int st,ed;
  13. };
  14.  
  15. int A[100100];
  16. int appearCnt[100100],numCnt[100100];
  17.  
  18. void Init(){
  19.     for (int i=0;i<100050;++i){
  20.         appearCnt[i] = 0;
  21.         numCnt[i] = 0;
  22.     }
  23.     appearCnt[1] = 1;
  24. }
  25.  
  26. int main(){
  27.     ios_base::sync_with_stdio(false),cin.tie(0);
  28.     int n,q;
  29.  
  30.     cin >> n >> q;
  31.     for (int i=1;i<=n;++i){
  32.         cin >> A[i];
  33.     }
  34.    
  35.     int const spaces = sqrt(n);
  36.     vector <Qs> group[500]{};
  37.     vector <pii> ans;
  38.  
  39.     for (int i=1;i<=q;++i){
  40.         int s,e;
  41.  
  42.         cin >> s >> e;
  43.         group[(s-1) / (n / spaces)].pb({i,s,e});
  44.     }
  45.     for (int i=0;i<=n/spaces;++i)
  46.         sort (group[i].begin(),group[i].end(),[](Qs a,Qs b){return a.ed < b.ed;});
  47.  
  48.     for (int i=0;i<=spaces;++i){
  49.         int LB = i * spaces + 1,RB = LB;
  50.         int maxAppear = A[LB];
  51.         Init();
  52.         numCnt[A[LB]] = 1;
  53.  
  54.         for (auto d:group[i]){
  55.             while (RB < d.ed){
  56.                 ++RB;
  57.                 --appearCnt[numCnt[A[RB]]];
  58.                 ++numCnt[A[RB]];
  59.                 ++appearCnt[numCnt[A[RB]]];
  60.                 if (numCnt[A[RB]] > numCnt[maxAppear]){
  61.                     maxAppear = A[RB];
  62.                 }
  63.             }
  64.             while (LB > d.st){
  65.                 --LB;
  66.                 --appearCnt[numCnt[A[LB]]];
  67.                 ++numCnt[A[LB]];
  68.                 ++appearCnt[numCnt[A[LB]]];
  69.                 if (numCnt[A[LB]] > numCnt[maxAppear]){
  70.                     maxAppear = A[LB];
  71.                 }
  72.             }
  73.             while (LB < d.st){
  74.                 --appearCnt[numCnt[A[LB]]];
  75.                 --numCnt[A[LB]];
  76.                 ++appearCnt[numCnt[A[LB]]];
  77.                 ++LB;
  78.                 if (!numCnt[maxAppear]){
  79.                     maxAppear = A[LB];
  80.                 }
  81.             }
  82.  
  83.             ans.pb({d.id,numCnt[maxAppear]});
  84.         }
  85.     }
  86.  
  87.     sort (ans.begin(),ans.end(),[](pii a,pii b){return a.first < b.first;});
  88.     for (auto i:ans){
  89.         cout << i.second << '\n';
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement