Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define SQ 175
  4.  
  5. const int MAXN = 3e4+10 ;
  6.  
  7. using namespace std ;
  8.  
  9. struct qry
  10. {
  11. int l , r , id ;
  12. bool operator < (qry other) const
  13. {
  14. if( l/SQ == other.l/SQ ) return r < other.r ;
  15. return l/SQ < other.l/SQ ;
  16. }
  17. } ;
  18.  
  19. //MO algorithm
  20. int mainV[MAXN] , freq[MAXN] , marcFreq[MAXN] ;
  21.  
  22. void mo(int x, int val)
  23. {
  24.  
  25. marcFreq[freq[x]] -- ;
  26. freq[x] += val ;
  27. marcFreq[ freq[x] ] ++ ;
  28. }
  29.  
  30. int getAns()
  31. {
  32. int tot = 0 ;
  33. for(int i = MAXN ; i > 0 ; i-- )
  34. {
  35. tot += marcFreq[i] ;
  36. if( tot >= i ) return i ;
  37. }
  38. }
  39.  
  40.  
  41. int main()
  42. {
  43. int n , m ;
  44. qry q[MAXN] ;
  45.  
  46. scanf("%d%d", &n, &m) ;
  47. for(int i = 1 ; i <= n ; i++ ) scanf("%d", &mainV[i] ) ;
  48. for(int i = 0 ; i < m; i++ )
  49. scanf("%d %d", &q[i].l, &q[i].r) , q[i].id = i ;
  50.  
  51. //Coordinate compression
  52. map<int,int> myMap ;
  53. int key = 1 ;
  54.  
  55. for(int i = 1 ; i <= n ; i++ )
  56. if(myMap.find(mainV[i]) == myMap.end() )
  57. myMap.insert( make_pair(mainV[i] , key++ ) ) ;
  58.  
  59. //Main code
  60. sort(q,q+m) ;
  61.  
  62. int l = 1 , r = 0 ;
  63. vector<int> myAns(MAXN) ;
  64. for(int i = 0 ; i < m ; i++ )
  65. {
  66. int newL = q[i].l , newR = q[i].r ;
  67.  
  68. while( r < newR ) mo( myMap[ mainV[++r] ] , 1) ;
  69. while( l < newL ) mo( myMap[ mainV[l++] ] , -1) ;
  70. while( r > newR ) mo( myMap[ mainV[r--] ] , -1) ;
  71. while( l > newL ) mo( myMap[ mainV[--l] ] , 1) ;
  72.  
  73. myAns[ q[i].id ] = getAns() ;
  74.  
  75. }
  76.  
  77. for(int i = 0 ; i < m ; i++) printf("%d\n" , myAns[i] ) ;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement