-
Notifications
You must be signed in to change notification settings - Fork 0
/
G.cpp
62 lines (51 loc) · 931 Bytes
/
G.cpp
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
/*
G - 查找[二分]
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10000010;
int nums[ MAXN ];
int Query[ MAXN ];
int search( int q, int n ) {
int len = n;
int ret = -1;
int l = 1, r = len;
while ( len > 0 ) {
if ( nums[ l ] >= q ) {
ret = l;
break;
}
int mid = l + ( r - l ) / 2;
if ( nums[ mid ] < q ) {
l = mid + 1;
} else {
l += 1;
r = mid;
}
len = r - l + 1;
}
return ret;
}
int main() {
int N, M;
scanf( "%d %d", &N, &M );
for ( int i = 1; i <= N; ++i )
scanf( "%d", &nums[ i ] );
for ( int i = 0; i < M; ++i ) {
scanf( "%d", &Query[ i ] );
printf( "%d\n", search( Query[ i ], N ) );
}
return 0;
}
/*
input:
5 3
1 3 5 7 9
1
3
6
output:
1
2
4
*/