//team42 (Lukasz Zatorski, Damian Rusak, Krzysztof Piecuch) #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef pair pi; typedef vector vi; typedef vector< vi > vii; #define ft first #define sd second #define mp make_pair #define pb push_back #define REP(i,a,b) for(register int i = a ; i < b; i++) #define DOWN(i,a,b) for(register int i = a; i>=b ; i--) #define WSK(C) typeof((C).begin()) #define FOREACH(wsk, C) for(WSK(C) wsk = (C).begin(); wsk!=(C).end; wsk++) int n,m,a,b,c,q,w,r,x,t,y; int A[1000100]; set >S; set >::iterator it,it2; vector, int> >V; pairE[1000100]; int Okej[1000100]; int main(){ int uu=0; while(1){ uu++; if(uu>1) printf("\n"); scanf("%d%d", &m, &q); if(m==0 && q==0) return 0; REP(i,0,m) scanf("%d", &A[i]); REP(i,0,q) Okej[i] = -1; S.clear(); V.clear(); REP(i,0,m){ a = A[i]; it = S.lower_bound(mp(a,-1)); if(it==S.end() || (*it).ft != a) { S.insert(mp(a,i)); } else{ b = (*it).second; S.erase(it); S.insert(mp(a,i)); V.pb(mp(mp(i,b), -a-1)); } } REP(i,0,q){ scanf("%d%d", &a,&b); a--;b--; E[i] = mp(a,b); V.pb(mp(mp(a,-1), i)); V.pb(mp(mp(b,2000000),i)); } S.clear(); sort(V.begin(), V.end()); //REP(i,0,V.size()){ // cout << V[i].ft.ft << " " << V[i].ft.sd << " " << V[i].sd << endl; //} bool czy = 0; int n = V.size(); REP(i,0,n){ a = V[i].ft.ft; b = V[i].ft.sd; c = V[i].sd; // cout << " i = " << i << endl; if(b == -1){ S.insert(mp(a,c)); } else if(c >= 0){ x = E[c].ft; it = S.find(mp(x,c)); if(it!=S.end()) S.erase(it); } else{ // cout << " usuwam " << " up dla " << b << endl; it = S.begin(); while(it!=S.end()){ // cout << (*it).ft << " " << (*it).sd << endl; it++; } it = S.upper_bound(mp(b+1,-1)); if(it==S.begin()) { continue;} it--; czy = false; // cout << " ( "; do{ c = -c-1; // cout << " usuwam dla " << c << endl; x = (*it).sd; Okej[x] = c; it2 = it; // cout << " usuwam " << (*it).ft << " " << (*it).sd << endl; it--; if(it2==S.begin()) czy = true; S.erase(it2); }while(!czy); // cout<< ")"<