-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_4.c
47 lines (43 loc) · 823 Bytes
/
7_4.c
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
#include <stdio.h>
int main(){
int n, q, val, index, left, right, tmp = 0;
int array[200010];
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &array[i]);
}
scanf("%d", &q);
for (int i = 0; i < q; i++){
scanf("%d", &val);
val += tmp;
left = 0;
right = n - 1;
do{
index = (right + left) / 2;
if (array[index] > val){
right = index;
}
else {
left = index;
}
} while (right > left + 1);
if (array[index + 1] == val){
index++;
while(array[index + 1] == val){
index++;
}
tmp = index;
}
else if (array[index] == val){
tmp = index;
}
else if (array[index - 1] == val){
index--;
tmp = index;
}
else{
tmp = -1;
}
printf("%d\n", tmp);
}
}