fork download
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int n , q ;
  5. cin >> n >> q;
  6. int a[n] ;
  7. for (int i = 0; i < n; i++) {
  8. cin >> a[i];
  9. }
  10. int num , index = -1 , st =0 , end = n ;
  11. for (int i = 0; i < q; i++)
  12. {
  13. cin >> num;
  14. while ( st <= end) {
  15. int it = ((st+end )/ 2);
  16. if (a[it] == num) {
  17. index = it;
  18. break;
  19. }
  20. else if (num >a[it]) {
  21. st = it +1 ;
  22. }
  23. else {
  24. end = it-1 ;
  25. }
  26. }
  27. if (index == -1) {
  28. cout << "-1" << endl;
  29. }
  30. else {
  31. cout << index+1 << endl;
  32. }
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5312KB
stdin
3 3
1 2 3
1
3
5
stdout
1
1
1