|
| 1 | + |
| 2 | +/** Which of the favors of your Lord will you deny ? **/ |
| 3 | + |
| 4 | +#include<bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +#define LL long long |
| 8 | +#define PII pair<int,int> |
| 9 | +#define PLL pair<LL,LL> |
| 10 | +#define MP make_pair |
| 11 | +#define F first |
| 12 | +#define S second |
| 13 | + |
| 14 | +#define ALL(x) (x).begin(), (x).end() |
| 15 | +#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl |
| 16 | +#define READ freopen("alu.txt", "r", stdin) |
| 17 | +#define WRITE freopen("vorta.txt", "w", stdout) |
| 18 | + |
| 19 | +#include <ext/pb_ds/assoc_container.hpp> |
| 20 | +#include <ext/pb_ds/tree_policy.hpp> |
| 21 | +using namespace __gnu_pbds; |
| 22 | + |
| 23 | +template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>; |
| 24 | + |
| 25 | +/** |
| 26 | +
|
| 27 | +PBDS |
| 28 | +------------------------------------------------- |
| 29 | +1) insert(value) |
| 30 | +2) erase(value) |
| 31 | +3) order_of_key(value) // 0 based indexing |
| 32 | +4) *find_by_order(position) // 0 based indexing |
| 33 | +
|
| 34 | +**/ |
| 35 | + |
| 36 | +template<class T1, class T2> |
| 37 | +ostream &operator <<(ostream &os, pair<T1,T2>&p); |
| 38 | +template <class T> |
| 39 | +ostream &operator <<(ostream &os, vector<T>&v); |
| 40 | +template <class T> |
| 41 | +ostream &operator <<(ostream &os, set<T>&v); |
| 42 | + |
| 43 | +inline void optimizeIO() |
| 44 | +{ |
| 45 | + ios_base::sync_with_stdio(false); |
| 46 | + cin.tie(NULL); |
| 47 | +} |
| 48 | + |
| 49 | +const int nmax = 2e5+7; |
| 50 | +const LL LINF = 1e17; |
| 51 | + |
| 52 | +template <class T> |
| 53 | +string to_str(T x) |
| 54 | +{ |
| 55 | + stringstream ss; |
| 56 | + ss<<x; |
| 57 | + return ss.str(); |
| 58 | +} |
| 59 | + |
| 60 | +//bool cmp(const PII &A,const PII &B) |
| 61 | +//{ |
| 62 | +// |
| 63 | +//} |
| 64 | + |
| 65 | +int ara[nmax]; |
| 66 | + |
| 67 | +int binarySearch(int lo,int hi,int key) |
| 68 | +{ |
| 69 | + while(lo<=hi) |
| 70 | + { |
| 71 | + int mid = lo + (hi-lo)/2; |
| 72 | + |
| 73 | + if(ara[mid]<key) lo = mid + 1; |
| 74 | + else if (ara[mid]>key) hi = mid - 1; |
| 75 | + else return mid; |
| 76 | + } |
| 77 | + |
| 78 | + return -1; |
| 79 | +} |
| 80 | + |
| 81 | +int lowerBound(int lo,int hi,int key) |
| 82 | +{ |
| 83 | + while(lo!=hi) |
| 84 | + { |
| 85 | + int mid = lo + (hi-lo)/2; |
| 86 | + |
| 87 | + if(ara[mid]<key) lo = mid + 1; |
| 88 | + else hi = mid; |
| 89 | + } |
| 90 | + |
| 91 | + return lo; |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +int upperBound(int lo,int hi,int key) |
| 96 | +{ |
| 97 | + while(lo!=hi) |
| 98 | + { |
| 99 | + int mid = lo + (hi-lo)/2; |
| 100 | + |
| 101 | + if(ara[mid]<=key) lo = mid + 1; |
| 102 | + else hi = mid; |
| 103 | + } |
| 104 | + |
| 105 | + return lo; |
| 106 | +} |
| 107 | + |
| 108 | +int main() |
| 109 | +{ |
| 110 | + optimizeIO(); |
| 111 | + |
| 112 | + int n; |
| 113 | + cin>>n; |
| 114 | + |
| 115 | + for(int i=1;i<=n;i++) |
| 116 | + cin>>ara[i]; |
| 117 | + |
| 118 | + sort(ara+1,ara+n+1); |
| 119 | + |
| 120 | + for(int i=1;i<=n;i++) |
| 121 | + cout<<ara[i]<<" "; |
| 122 | + cout<<endl; |
| 123 | + |
| 124 | + while(1) |
| 125 | + { |
| 126 | + int val; |
| 127 | + cin>>val; |
| 128 | + int idx = binarySearch(1,n,val); |
| 129 | + if(idx!=-1) cout<<val<<" present at : "<<idx<<endl; |
| 130 | + else cout<<val<<" absent"<<endl; |
| 131 | + } |
| 132 | + |
| 133 | + /// for 1 based indexing with n elements , set lo = 1 , hi = n+1 |
| 134 | + |
| 135 | + while(1) |
| 136 | + { |
| 137 | + int val; |
| 138 | + cin>>val; |
| 139 | + int idx = lowerBound(1,n+1,val); |
| 140 | + if(idx>n) cout<<"lower_bound doesn't exist"<<endl; |
| 141 | + else cout<<"lower_bound of "<<val<<" : "<<idx<<endl; |
| 142 | + } |
| 143 | + |
| 144 | + while(1) |
| 145 | + { |
| 146 | + int val; |
| 147 | + cin>>val; |
| 148 | + int idx = upperBound(1,n+1,val); |
| 149 | + if(idx>n) cout<<"upper_bound doesn't exist"<<endl; |
| 150 | + else cout<<"upper_bound of "<<val<<" : "<<idx<<endl; |
| 151 | + } |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | +
|
| 158 | +10 |
| 159 | +10 2 2 8 5 6 4 5 4 4 |
| 160 | +
|
| 161 | +**/ |
| 162 | + |
| 163 | +template<class T1, class T2> |
| 164 | +ostream &operator <<(ostream &os, pair<T1,T2>&p) |
| 165 | +{ |
| 166 | + os<<"{"<<p.first<<", "<<p.second<<"} "; |
| 167 | + return os; |
| 168 | +} |
| 169 | +template <class T> |
| 170 | +ostream &operator <<(ostream &os, vector<T>&v) |
| 171 | +{ |
| 172 | + os<<"[ "; |
| 173 | + for(int i=0; i<v.size(); i++) |
| 174 | + { |
| 175 | + os<<v[i]<<" " ; |
| 176 | + } |
| 177 | + os<<" ]"; |
| 178 | + return os; |
| 179 | +} |
| 180 | + |
| 181 | +template <class T> |
| 182 | +ostream &operator <<(ostream &os, set<T>&v) |
| 183 | +{ |
| 184 | + os<<"[ "; |
| 185 | + for(T i:v) |
| 186 | + { |
| 187 | + os<<i<<" "; |
| 188 | + } |
| 189 | + os<<" ]"; |
| 190 | + return os; |
| 191 | +} |
| 192 | + |
| 193 | + |
0 commit comments