#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; struct Dvojice { int a; int b; int id; int chybne; }; struct Dvojice d[1000100]; long long int ids[1000100]; int compare (const void *a, const void *b) { struct Dvojice a1 = *(struct Dvojice*)a; struct Dvojice b1 = *(struct Dvojice*)b; if (a1.a == b1.a) return (a1.b-b1.b); else return (a1.a-b1.a); } int compare2 (const void *a, const void *b) { struct Dvojice a1 = *(struct Dvojice*)a; struct Dvojice b1 = *(struct Dvojice*)b; return (a1.id-b1.id); } int main(void) { int m, q; int x, y; int from, to, to2, od, od2, od3, pocet, ok, zac = 1; long long tmp; int i, ii; map mapa; while (1) { scanf("%d %d", &m, &q); if (m == 0 && q == 0) break; else if (zac == 0) { printf("\n"); } if (zac == 1) zac--; for(i=0; i < m; i++) { scanf("%lld", &tmp); ids[i] = tmp; } for(i=0; i < q; i++) { scanf("%d %d", &x, &y); d[i].a = x; d[i].b = y; d[i].id = i; } qsort (d, q, sizeof(struct Dvojice), compare); /*for (i = 0; i < q; i++) printf("%d %d %d\n", d[i].a, d[i].b, d[i].id);*/ pocet = 0; for (i = 0; i < q; i++) { if (i == 0) { od = d[i].a-1; to = d[i].b-1; od2 = od; to2 = to; od3 = od; } else { od = d[i].a-1; to = d[i].b-1; for (ii = od2; ii < od; ii++) { mapa[ids[ii]]--; if (mapa[ids[ii]] == 1) { pocet--; } } for (ii = to+1; ii <= to2; ii++) { mapa[ids[ii]]--; if (mapa[ids[ii]] == 1) pocet--; } od3 = to2+1; to2 = to; od2 = od; } ok = 0; for (ii = od3; ii <= to; ii++) { mapa[ids[ii]]++; if (mapa[ids[ii]] == 2) { d[i].chybne = ids[ii]; ok = 1; pocet++; } } if (ok == 0 && pocet > 0) { for (map::iterator huh=mapa.begin(); huh!=mapa.end(); huh++) { if (huh->second > 1) { d[i].chybne = huh->first; break; } } } else if (ok == 0) d[i].chybne = -1; } qsort (d, q, sizeof(struct Dvojice), compare2); for (i = 0; i < q; i++) { if (d[i].chybne == -1) printf("OK\n"); else printf("%d\n", d[i].chybne); } mapa.clear(); } return 0; }