Skip to content

Commit 76225bc

Browse files
committed
Day 7 q1
1 parent 7e0c491 commit 76225bc

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//problem link- https://codeforces.com/problemset/problem/1538/C
2+
//submission link- https://codeforces.com/problemset/submission/1538/356104640
3+
//approach- for every index find a lower index where possible pair value starts and ends unsing binary searches
4+
//time complexity- O(nlogn)
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
#define endl '\n'
8+
#define sp << " " <<
9+
#define int long long
10+
#define fora(a) for(auto i:a)
11+
#define gcd(a,b) __gcd(a,b)
12+
#define lcm(a,b) (a*(b/gcd(a,b)))
13+
#define forn(i,n) for(int i=0; i<n; i++)
14+
#define printv(a) {for(auto u:a) cout<<u<<" "; cout<<endl;}
15+
#define inputv(a) {for(int i=0;i<n;i++) cin>>a[i];}
16+
#define ll long long
17+
#define vi vector<int>
18+
#define vll vector<long long>
19+
#define pii pair<int, int>
20+
#define pll pair<long long, long long>
21+
#define all(x) x.begin(),x.end()
22+
23+
void fast_io() {
24+
ios_base::sync_with_stdio(false);
25+
cin.tie(NULL);
26+
cout.tie(NULL);
27+
}
28+
29+
30+
const int MOD = 1e9 + 7;
31+
ll addMod(ll a, ll b) { return (a + b) % MOD; }
32+
ll subMod(ll a, ll b) { return (a - b + MOD) % MOD; }
33+
ll mulMod(ll a, ll b) { return (a * b) % MOD; }
34+
35+
ll powerMod(ll a, ll b) {
36+
ll res = 1;
37+
while (b > 0) {
38+
if (b & 1) res = mulMod(res, a);
39+
a = mulMod(a, a);
40+
b >>= 1;
41+
}
42+
return res;
43+
}
44+
45+
46+
47+
48+
49+
void solve() {
50+
int n,l,r;
51+
cin>>n>>l>>r;
52+
vi a(n);
53+
inputv(a);
54+
int ans=0;
55+
sort(all(a));
56+
for(int i=0;i<n;i++){
57+
58+
auto it=lower_bound(a.begin(),a.begin()+i,l-a[i]);
59+
int low=-1;
60+
if (it!=a.begin()+i) {
61+
low=it-a.begin();
62+
}
63+
64+
it =upper_bound(a.begin(),a.begin()+i,r-a[i]);
65+
int high=-1;
66+
if(it!=a.begin()) {
67+
high=it-a.begin()-1;
68+
}
69+
if(high!=-1&&low!=-1&&high>=low){
70+
ans+=high-low+1;
71+
}
72+
}
73+
74+
cout<<ans<<endl;
75+
}
76+
77+
78+
79+
80+
81+
signed main() {
82+
fast_io();
83+
84+
int t;
85+
cin >> t;
86+
87+
while (t--) {
88+
solve();
89+
}
90+
91+
return 0;
92+
}

0 commit comments

Comments
 (0)