1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <iostream>
using namespace std;
const int N = 100010;
int n, m; int q[N];
int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
while (m -- ) { int x; scanf("%d", &x);
int l = 0, r = n - 1; while (l < r) { int mid = l + r >> 1; if (q[mid] >= x) r = mid; else l = mid + 1; }
if (q[l] != x) cout << "-1 -1" << endl; else { cout << l << ' ';
int l = 0, r = n - 1; while (l < r) { int mid = l + r + 1 >> 1; if (q[mid] <= x) l = mid; else r = mid - 1; }
cout << l << endl; } }
return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; int num[n]; for(int i=0;i<n;i++){ cin>>num[i]; } while(m--){ int target;cin>>target; int l = 0,r = n - 1; while(l < r){ int mid = (l + r) >> 1; if (num[mid] == target) { r = mid; } else if(num[mid] > target) { r = mid - 1; } else { l = mid + 1; } } if (num[r] == target) cout<<l<<" "; else { cout<<"-1 -1"<<endl; continue; } l = 0, r = n - 1; while(l < r){ int mid = (l+r + 1) >>1; if (num[mid] == target) { l = mid; } else if (num[mid] > target) { r = mid - 1; } else { l = mid + 1; } } cout<<l<<endl; } }
|