#include #include #include #include #define size (10000000) #define hash(x) (((x)*19u+13)%(size)) #define uint unsigned int struct ww { uint num; uint pos; }; int main() { ww *set = (ww*)malloc( size * sizeof( ww ) ); ww *end = set; uint *key = (uint*)malloc( size * sizeof( uint ) ); uint *jmp = (uint*)malloc( size * sizeof( uint ) ); end += size - 1; if( !set || !key ) { printf("mem"); return 1; }; for( ;; ) { uint ks, qs, f, t; scanf( "%u %u", &ks, &qs ); if( !ks && !qs ) return 0; for( uint i = 0; i < ks; i++ ) { scanf( "%u", &key[i] ); } memset( set, 0, size * sizeof( ww ) ); memset( jmp, 0, size * sizeof( uint ) ); for( uint i = 0; i < ks; i++ ) { uint n = key[i]; ww *s = set + hash( hash( n ) ); ww *start = s; while( s->num ) { if( s->num == n ) { jmp[ s->pos ] = i; break; } s++; if( s == start ) return 1; if( s > end ) s = set; } s->num = n; s->pos = i; } for( uint j = 0; j < qs; j++ ) { scanf( "%u %u", &f, &t ); t--; for( uint i = f - 1; i < t; i++ ) { if( jmp[i] && jmp[ i ] <= t ) { printf( "%d\n", key[i] ); goto skip; } } printf("OK\n"); skip: ; } printf("\n"); } }