Skip to content

Commit e0cabf3

Browse files
committed
Solved day7 que1
1 parent d0fba44 commit e0cabf3

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
3+
Problem : Number of pairs 1583C
4+
5+
Approach :
6+
Sort the array
7+
Fix one element
8+
For each a[i], try to find how many a[j] (j < i) exists such that sum lie inside [l, r].
9+
Convert sum condition into a range l−a[i]≤a[j]≤r−a[i]
10+
we can count how many values lie in this range using lower_bound and upper_bound.
11+
12+
Time Complexity : O(nlogn)​
13+
Space Complexity : O(n)
14+
15+
Submission Link : https://codeforces.com/problemset/submission/1538/356132119
16+
*/
17+
#include <bits/stdc++.h>
18+
#include <bits/stdc++.h>
19+
#include <ext/pb_ds/assoc_container.hpp>
20+
#include <ext/pb_ds/tree_policy.hpp>
21+
using namespace std;
22+
using namespace __gnu_pbds;
23+
24+
#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
25+
#define int long long
26+
#define ull unsigned long long
27+
#define ld long double
28+
#define vi vector<int>
29+
#define vb vector<bool>
30+
#define vs vector<string>
31+
#define pii pair<int,int>
32+
#define pll pair<long long,long long>
33+
#define vpi vector<pii>
34+
#define vpl vector<pll>
35+
#define vvi vector<vi>
36+
#define mii map<int,int>
37+
#define si set<int>
38+
#define osi ordered_set<int>
39+
#define loop(i,n) for (int i = 0; i < n; i++)
40+
#define rloop(i,n) for (int i = n - 1; i >= 0; i--)
41+
#define loop1(i,a,b) for (int i = a; i <= b; i++)
42+
#define rloop1(i,a,b) for (int i = a; i >= b; i--)
43+
#define pb push_back
44+
#define ppb pop_back
45+
#define mp make_pair
46+
#define F first
47+
#define S second
48+
#define lb lower_bound
49+
#define ub upper_bound
50+
#define all(x) (x).begin(), (x).end()
51+
#define rall(x) (x).rbegin(), (x).rend()
52+
#define sz(x) ((int)(x).size())
53+
#define findbo(x) find_by_order(x) // returns iterator to the k-th largest element (0-based)
54+
#define orderbk(x) order_of_key(x) // returns number of items strictly smaller than x
55+
#define endl '\n'
56+
#define printy cout<<"YES"<<endl
57+
#define printn cout<<"NO"<<endl
58+
#define vout(a) for (int i = 0; i < a.size(); i++) cout << a[i] << ' '; cout << endl;
59+
#define vpout(a) loop(i,sz(a)) cout <<a[i].F<<' '<< a[i].S<< endl; cout << endl;
60+
#define rt(x){ cout<<x<<endl; return; }
61+
template<typename T>
62+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // less_equal<T> for multiset, greater<T> for decreasing order
63+
64+
const int MOD = 1e9 + 7;
65+
const int INF = 1e9;
66+
67+
#ifndef ONLINE_JUDGE
68+
#define debug(x) cerr<<#x<<" = "; _print(x); cerr<<endl;
69+
template<typename T> void _print(const T& x){ cerr<<x; }
70+
template<typename T,typename U> void _print(const pair<T,U>& p){ cerr<<"("; _print(p.first); cerr<<", "; _print(p.second); cerr<<")"; }
71+
template<typename T> void _print(const vector<T>& v){ cerr<<"["; for(size_t i=0;i<v.size();++i){ _print(v[i]); if(i+1!=v.size()) cerr<<", "; } cerr<<"]"; }
72+
template<typename T> void _print(const set<T>& s){ cerr<<"{"; for(auto it=s.begin(); it!=s.end(); ++it){ _print(*it); if(next(it)!=s.end()) cerr<<", "; } cerr<<"}"; }
73+
template<typename T,typename U> void _print(const map<T,U>& m){ cerr<<"{"; for(auto it=m.begin(); it!=m.end(); ++it){ _print(it->first); cerr<<": "; _print(it->second); if(next(it)!=m.end()) cerr<<", "; } cerr<<"}"; }
74+
#else
75+
#define debug(x)
76+
#endif
77+
78+
#define sumV(a) accumulate(all(a), 0LL)
79+
#define minV(a) min_element(all(a))
80+
#define maxV(a) max_element(all(a))
81+
82+
int log2f(long long n) {
83+
if (n==0) return -1;
84+
int res = 0;
85+
while (n > 1){
86+
n >>= 1;
87+
res++;
88+
}
89+
return res;
90+
}
91+
92+
int binpow(int a, int n) {
93+
int result = 1;
94+
a %= MOD;
95+
while (n > 0) {
96+
if (n & 1)
97+
result = (result * a) % MOD;
98+
a = (a * a) % MOD;
99+
n >>= 1;
100+
}
101+
return result;
102+
}
103+
104+
void solve(){
105+
int n,l,r;
106+
cin>>n>>l>>r;
107+
vi a(n);
108+
loop(i,n) cin>>a[i];
109+
sort(all(a));
110+
111+
int cnt = 0;
112+
loop(i,n){
113+
if(a[i]>r) continue;
114+
int low = l-a[i], high = r-a[i];
115+
auto itl = lb(a.begin(), a.begin()+i,low);
116+
auto itr = ub(a.begin(), a.begin()+i,high);
117+
cnt += itr - itl ;
118+
}
119+
cout<<cnt<<endl;
120+
}
121+
122+
int32_t main(){
123+
fast_io;
124+
int t=1;
125+
cin>>t;
126+
while(t--){
127+
solve();
128+
}
129+
return 0;
130+
}

0 commit comments

Comments
 (0)