Skip to content

Commit 948e565

Browse files
authored
Merge pull request #443 from PHOX-9/main
Create Soln1.cpp
2 parents 3ee9ad3 + d603c90 commit 948e565

1 file changed

Lines changed: 206 additions & 0 deletions

File tree

  • Problems/Binary-Search/Day-07/sol/Ibrahim
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//https://codeforces.com/contest/1538/submission/348249121
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
#define int long long
7+
#define ll long long
8+
#define ull unsigned long long
9+
#define ld long double
10+
#define pii pair<int,int>
11+
#define vi vector<int>
12+
#define vvi vector<vector<int>>
13+
#define vpi vector<pair<int,int>>
14+
#define all(x) (x).begin(), (x).end()
15+
16+
17+
const long long MAXN = 2e6;
18+
const long long MOD = 1e9 + 7;
19+
const int INF = 1e18;
20+
const ld EPS = 1e-9;
21+
22+
23+
#define fastio ios::sync_with_stdio(false); cin.tie(nullptr);
24+
25+
#ifndef ONLINE_JUDGE
26+
#define debug(x) cerr << #x << " = " << x << "\n";
27+
#else
28+
#define debug(x)
29+
#endif
30+
31+
// Utility functions
32+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
33+
int lcm(int a, int b) { return a / gcd(a, b) * b; }
34+
int mod_add(int a, int b, int m=MOD) { return ((a % m) + (b % m) + m) % m; }
35+
int mod_sub(int a, int b, int m=MOD) { return ((a % m) - (b % m) + m) % m; }
36+
int mod_mul(int a, int b, int m=MOD) { return ((a % m) * (b % m)) % m; }
37+
38+
//Binary Exponentiation
39+
int binexp(int a, int b, int m=MOD) {
40+
int res = 1;
41+
a %= m;
42+
while(b > 0) {
43+
if(b & 1) res = (res * a) % m;
44+
a = (a * a) % m;
45+
b >>= 1;
46+
}
47+
return res;
48+
}
49+
50+
int mod_inv(int a, int m=MOD) {
51+
return binexp(a, m - 2, m);
52+
}
53+
54+
long long fac[MAXN + 1];
55+
long long inv[MAXN + 1];
56+
57+
long long exp(long long x, long long n, long long m) {
58+
x %= m;
59+
long long res = 1;
60+
while (n > 0) {
61+
if (n % 2 == 1) { res = res * x % m; }
62+
x = x * x % m;
63+
n /= 2;
64+
}
65+
return res;
66+
}
67+
68+
void factorial() {
69+
fac[0] = 1;
70+
for (long long i = 1; i <= MAXN; i++) { fac[i] = fac[i - 1] * i % MOD; }
71+
}
72+
73+
void inverses() {
74+
inv[MAXN] = exp(fac[MAXN], MOD - 2, MOD);
75+
for (long long i = MAXN; i >= 1; i--) { inv[i - 1] = inv[i] * i % MOD; }
76+
}
77+
78+
long long choose(long long n, long long r) {
79+
if (r > n)return 0ll;
80+
return (fac[n] * inv[r] % MOD * inv[n - r] % MOD) % MOD;
81+
}
82+
83+
long long catalan(long long n) {
84+
return (exp(n + 1, MOD - 2, MOD) % MOD * choose(2 * n, n) % MOD) % MOD;
85+
}
86+
87+
//Returns all prime numbers <=N
88+
vector<int> sieve(int n) {
89+
vector<bool> prime(n + 1, true);
90+
for (int p=2;p*p<=n;p++) {
91+
if (prime[p] == true) {
92+
93+
for (int i=p*p;i<=n;i+=p)
94+
prime[i] = false;
95+
}
96+
}
97+
98+
vector<int> res;
99+
for (int p = 2; p <= n; p++){
100+
if (prime[p]){
101+
res.push_back(p);
102+
}
103+
}
104+
return res;
105+
}
106+
107+
//Prime factors of all numbers upto N
108+
vector<vector<int>> primefactors(int N)
109+
{
110+
vvi pfac(N + 1);
111+
for (int i=2;i<=N;i++){
112+
if (!pfac[i].empty())
113+
continue;
114+
115+
for (int j = i; j <= N; j += i)
116+
pfac[j].push_back(i);
117+
}
118+
119+
return pfac;
120+
}
121+
122+
//smallest prime factor of a number
123+
int spf(int n) {
124+
if (n % 2 == 0) return 2;
125+
for (int i = 3; i * i <= n; i += 2) {
126+
if (n % i == 0) return i;
127+
}
128+
return n;
129+
}
130+
131+
int power2(int p)
132+
{
133+
int v=1ll<<p;
134+
return v;
135+
}
136+
137+
//Sort functions
138+
139+
void sort(vector<int>&a)
140+
{
141+
sort(a.begin(),a.end());
142+
}
143+
144+
void psort(vector<pair<int,int>>&a)
145+
{
146+
sort(a.begin(),a.end());
147+
}
148+
149+
void rsort(vector<int>&a)
150+
{
151+
sort(a.rbegin(),a.rend());
152+
}
153+
154+
void rpsort(vector<pair<int,int>>&a)
155+
{
156+
sort(a.rbegin(),a.rend());
157+
}
158+
159+
//2-D Vector Declaration:
160+
//vvi mat(n,vi(m)); // nxm matrix, all initialized to 0
161+
162+
163+
/***************Code***************/
164+
165+
void solve() {
166+
int n,i,l,r;
167+
cin>>n>>l>>r;
168+
vi a(n);
169+
170+
multiset<int> mt;
171+
172+
for(i=0;i<n;i++)
173+
{
174+
cin>>a[i];
175+
}
176+
177+
sort(a);
178+
int ans=0;
179+
180+
for(i=0;i<n;i++)
181+
{
182+
int v=a[i];
183+
184+
int il=lower_bound(a.begin(),a.end(),l-v)-a.begin();
185+
int ir=upper_bound(a.begin(),a.end(),r-v)-a.begin()-1;
186+
187+
if(il<=i)
188+
il=i+1;
189+
// cout<<a[i]<<endl;
190+
// cout<<il<<" "<<ir<<endl;
191+
if(il<=ir)
192+
ans+=ir-il+1;
193+
}
194+
cout<<ans<<endl;
195+
}
196+
197+
198+
signed main() {
199+
fastio;
200+
int t = 1;
201+
cin >> t;
202+
while (t--) {
203+
solve();
204+
}
205+
return 0;
206+
}

0 commit comments

Comments
 (0)