|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#include<queue> |
| 3 | +using namespace std; |
| 4 | +typedef unordered_map<int, int> umii; |
| 5 | +typedef unordered_map<long long, long long> umll; |
| 6 | +typedef unordered_map<char, long long> umci; |
| 7 | +typedef vector<pair<int, int>> vpi; |
| 8 | +typedef vector<int> vi; |
| 9 | +typedef long long ll; |
| 10 | +typedef vector<long long> vll; |
| 11 | +typedef unordered_map<int , bool> umib; |
| 12 | +#define sum(v) accumulate(v.begin(), v.end(), 0) |
| 13 | +#define endl '\n' |
| 14 | +#define f0(i, n) for(long long i = 0; i < n; i++) |
| 15 | +#define f1(i, n) for(long long i = 1; i < n; i++) |
| 16 | +#define as(v) sort(v.begin(), v.end()) |
| 17 | +#define all(x) (x).begin(), (x).end() |
| 18 | +#define pb push_back |
| 19 | +template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;} |
| 20 | +template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;} |
| 21 | +template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;} |
| 22 | +ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; } |
| 23 | +void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;} |
| 24 | +void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;} |
| 25 | + |
| 26 | + |
| 27 | +// submission link :- |
| 28 | +//https://codeforces.com/contest/1538/submission/352943894 |
| 29 | +// C_Number_of_Pairs.cpp |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +void solve(){ |
| 37 | + |
| 38 | +//-------------INPUT------------- |
| 39 | + ll n; |
| 40 | + cin >> n; |
| 41 | + ll l,r; |
| 42 | + cin >> l >> r; |
| 43 | + vll v(n); input(v); |
| 44 | + map<ll,ll>mp1; |
| 45 | + map<ll,ll>mp2; |
| 46 | + map<ll,ll>mp; |
| 47 | + ll count=0; |
| 48 | + as(v); |
| 49 | + vll temp; |
| 50 | + |
| 51 | + for(ll i=0;i<n;i++) |
| 52 | + { |
| 53 | + ll req1 = l-v[i]; |
| 54 | + ll a,b; |
| 55 | + ll req2 = r-v[i]; |
| 56 | + // apply lowerbound to find req1 |
| 57 | + auto it = lower_bound(temp.begin(),temp.end(),req1); |
| 58 | + |
| 59 | + // using binary search to find the req1 index |
| 60 | + |
| 61 | + |
| 62 | + if( it!=temp.end() && *it>=req1) |
| 63 | + { |
| 64 | + a = mp[*it]; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + a =-1; |
| 69 | + } |
| 70 | + |
| 71 | + //cout << a << endl; |
| 72 | + |
| 73 | + // if got a particular a then try to find b |
| 74 | + |
| 75 | + if(a!=-1 && !temp.empty()) |
| 76 | + { |
| 77 | + |
| 78 | + auto it = lower_bound(temp.begin(),temp.end(),req2); |
| 79 | + //cout << req2 << " " << mp2[*it] << " " << *it << endl; |
| 80 | + //cout << mp1[*it]; |
| 81 | + if(it == temp.end() && !temp.empty()) |
| 82 | + { |
| 83 | + b = mp2[*(it-1)]; |
| 84 | + //cout << "j"; |
| 85 | + } |
| 86 | + else if(*it>req2) |
| 87 | + { |
| 88 | + b = mp[*it]-1; |
| 89 | + //cout << "l"; |
| 90 | + } |
| 91 | + else{ |
| 92 | + b= mp2[*it]; |
| 93 | + //cout << "lf"; |
| 94 | + } |
| 95 | + if(b>=a) |
| 96 | + { |
| 97 | + // this will be total pairs |
| 98 | + count = count+b-a+1; |
| 99 | + } |
| 100 | + } |
| 101 | + //cout << "b" << " " << b << endl; |
| 102 | + |
| 103 | + temp.push_back(v[i]); |
| 104 | + if(mp[v[i]]==0 && v[i]!=v[0]) |
| 105 | + { |
| 106 | + |
| 107 | + mp[v[i]] = i; |
| 108 | + } |
| 109 | + mp2[v[i]] = i; |
| 110 | + } |
| 111 | + |
| 112 | + cout << count << endl; |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +//time complexity o(nlogn) |
| 117 | +//-------------CODE-------------- |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +int main(){ |
| 125 | + int tt; cin >> tt; while(tt--) |
| 126 | +{solve();}; |
| 127 | +} |
0 commit comments