Skip to content

Commit 0711c86

Browse files
authored
Merge pull request #452 from Apoorv012/day7/q1
Day 7 Q 1
2 parents 6d4249d + 1b400d9 commit 0711c86

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Submission Link: https://codeforces.com/contest/1538/submission/356110702
3+
4+
Instead of finding the pairs to be greater than equal to L, and less than equal to R
5+
I found the pairs that were less than equal to R separately, and less than L separately.
6+
and the answer was the difference of it.
7+
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
int main() {
14+
int t;
15+
cin >> t;
16+
while (t--) {
17+
long long n, L, R;
18+
cin >> n >> L >> R;
19+
vector<long long> v(n);
20+
for (auto &x: v) cin >> x;
21+
sort(v.begin(), v.end());
22+
long long lteR = 0, ltL = 0;
23+
24+
long long l = 0, r = n-1;
25+
while (l < r) {
26+
long long s = v[l] + v[r];
27+
// 1 2 3 4 5 ; 3-5
28+
if (s <= R) {
29+
lteR += r-l;
30+
l++;
31+
}
32+
else {
33+
r--;
34+
}
35+
}
36+
l = 0, r = n-1;
37+
while (l < r) {
38+
long long s = v[l] + v[r];
39+
// 1 2 3 4 5 ; 3-5
40+
if (s < L) {
41+
ltL += r-l;
42+
l++;
43+
}
44+
else {
45+
r--;
46+
}
47+
}
48+
cout << (lteR - ltL) << endl;
49+
// cout << lteR << " " << ltL << " " << (lteR - ltL) << endl;
50+
}
51+
}

0 commit comments

Comments
 (0)