Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define SQ 175
- const int MAXN = 3e4+10 ;
- using namespace std ;
- struct qry
- {
- int l , r , id ;
- bool operator < (qry other) const
- {
- if( l/SQ == other.l/SQ ) return r < other.r ;
- return l/SQ < other.l/SQ ;
- }
- } ;
- //MO algorithm
- int mainV[MAXN] , freq[MAXN] , marcFreq[MAXN] ;
- void mo(int x, int val)
- {
- marcFreq[freq[x]] -- ;
- freq[x] += val ;
- marcFreq[ freq[x] ] ++ ;
- }
- int getAns()
- {
- int tot = 0 ;
- for(int i = MAXN ; i > 0 ; i-- )
- {
- tot += marcFreq[i] ;
- if( tot >= i ) return i ;
- }
- }
- int main()
- {
- int n , m ;
- qry q[MAXN] ;
- scanf("%d%d", &n, &m) ;
- for(int i = 1 ; i <= n ; i++ ) scanf("%d", &mainV[i] ) ;
- for(int i = 0 ; i < m; i++ )
- scanf("%d %d", &q[i].l, &q[i].r) , q[i].id = i ;
- //Coordinate compression
- map<int,int> myMap ;
- int key = 1 ;
- for(int i = 1 ; i <= n ; i++ )
- if(myMap.find(mainV[i]) == myMap.end() )
- myMap.insert( make_pair(mainV[i] , key++ ) ) ;
- //Main code
- sort(q,q+m) ;
- int l = 1 , r = 0 ;
- vector<int> myAns(MAXN) ;
- for(int i = 0 ; i < m ; i++ )
- {
- int newL = q[i].l , newR = q[i].r ;
- while( r < newR ) mo( myMap[ mainV[++r] ] , 1) ;
- while( l < newL ) mo( myMap[ mainV[l++] ] , -1) ;
- while( r > newR ) mo( myMap[ mainV[r--] ] , -1) ;
- while( l > newL ) mo( myMap[ mainV[--l] ] , 1) ;
- myAns[ q[i].id ] = getAns() ;
- }
- for(int i = 0 ; i < m ; i++) printf("%d\n" , myAns[i] ) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement