Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int maxN = 2e5 + 100;
- long long a[maxN];
- int find_min_idx (long long L,int n,long long x) {
- //a[m] >= L
- int l = 1,r = n;
- int res = -1;
- while (l <= r) {
- int m = (l + r) / 2;
- if (a[m] + x >= L) {
- res = m;
- r = m - 1;
- } else {
- l = m + 1;
- }
- }
- return res;
- }
- int find_max_idx (long long R,int n,long long x) {
- //a[m] <= R
- int l = 1,r = n;
- int res = -1;
- while (l <= r) {
- int m = (l + r) / 2;
- if (a[m] + x <= R) {
- res = m;
- l = m + 1;
- } else {
- r = m - 1;
- }
- }
- return res;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T;
- cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- long long n,L,R;
- cin >> n >> L >> R;
- for (int i = 1; i <= n; ++i) cin >> a[i];
- sort (a + 1,a + n + 1);
- long long res = 0;
- for (int i = 1; i <= n; ++i) {
- int l = find_min_idx(L,i - 1,a[i]);
- int r = find_max_idx(R,i - 1,a[i]);
- if (l == -1 or r == -1 or l > r) {
- continue;
- }
- res += (r - l + 1);
- }
- cout << res << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement