-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD. Fast search(CF).cpp
81 lines (70 loc) · 1.51 KB
/
D. Fast search(CF).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
// https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << x << " ";
// bool check(vector<ll>& v, ll mid, ll target) {
// return v[mid] >= target;
// }
ll f1(vector<ll>& v, ll x) {
int n = v.size();
int low = 0;
int high = n - 1;
int mid;
int ans = v.size();
while (low <= high) {
mid = (low + high) / 2;
if (v[mid] > x) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
ll f2(vector<ll>& v, ll x) {
int n = v.size();
int low = 0;
int high = n - 1;
int mid;
int ans = v.size();
while (low <= high) {
mid = (low + high) / 2;
if (v[mid] >= x) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vector<ll> v(n);
for (ll i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
ll k;
cin >> k;
while (k--) {
ll a, b;
cin >> a >> b;
cout << f1(v, b) - f2(v, a) << endl;
}
}