-
Notifications
You must be signed in to change notification settings - Fork 34
/
1088-Points-in-Segements.cpp
103 lines (72 loc) · 1.23 KB
/
1088-Points-in-Segements.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
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
99
100
101
#include <iostream>
#include <stdio.h>
using namespace std;
int a[100008];
int n;
int q;
int binary_left(int left, int right, int x)
{
int mid;
mid = (left + right) / 2;
if((mid+1 == n ||a[mid+1] > x) and (mid == 0 || a[mid-1] < x)) {
if(a[mid] < x) {
mid++;
}
return mid;
}
if(left > right) {
return -9999;
}
if(a[mid] < x) {
binary_left(mid +1, right, x);
}
else {
binary_left(left, mid - 1, x);
}
}
int binary_right(int left, int right, int x)
{
int mid;
mid = (left + right) / 2;
if((mid+1 == n ||a[mid+1] > x) and (mid == 0 || a[mid-1] < x)) {
if(a[mid] > x) {
mid--;
}
return mid;
}
if(left > right) {
return -1;
}
if(a[mid] < x) {
binary_right(mid +1, right, x);
}
else {
binary_right(left, mid - 1, x);
}
}
int main()
{
int t;
int left;
int right;
int x;
int y;
int ans;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
scanf("%d", &q);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Case %d:\n", cs);
for (int i = 0; i < q; i++) {
scanf("%d", &x);
scanf("%d", &y);
left = binary_left(0, n-1, x);
right = binary_right(0, n-1, y);
ans = right - left + 1;
printf("%d\n",ans);
}
}
}