Skip to content

Commit 512eddc

Browse files
committed
binary search misc
1 parent 5705bd7 commit 512eddc

File tree

2 files changed

+256
-2
lines changed

2 files changed

+256
-2
lines changed

Algorithm/42 Binary Search.cpp

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+

Data Structure/01 BIT (POINT update , RANGE query).cpp

+63-2
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ string to_str(LL x)
5555
//
5656
//}
5757

58-
int ara[nmax];
58+
LL ara[nmax];
5959

6060
/** BIT / FENWICK TREE **/
6161
/*** 1 based indexing ***/
6262

6363
LL BIT[nmax];
64-
void update(int index,int val,int len) /** POINT update , adds to the current value **/
64+
void update(int index,LL val,int len) /** POINT update , adds to the current value **/
6565
{
6666
while(index<=len)
6767
{
@@ -81,6 +81,67 @@ LL query(int index) /** RANGE query **/
8181
return sum;
8282
}
8383

84+
85+
///===================================================================================\\\
86+
87+
88+
/** binary search on BIT : lower_bound and upper_bound **/
89+
90+
/// for 1 based indexing with n elements , set lo = 1 , hi = n+1
91+
92+
int lowerBound_BIT(int lo,int hi,int key) /** O( (logN)^2 ) **/
93+
{
94+
while(lo!=hi)
95+
{
96+
int mid = lo + (hi-lo)/2;
97+
98+
if(query(mid)<key) lo = mid + 1;
99+
else hi = mid;
100+
}
101+
102+
return lo;
103+
}
104+
105+
int upperBound_BIT(int lo,int hi,int key) /** O( (logN)^2 ) **/
106+
{
107+
while(lo!=hi)
108+
{
109+
int mid = lo + (hi-lo)/2;
110+
111+
if(query(mid)<=key) lo = mid + 1;
112+
else hi = mid;
113+
}
114+
115+
return lo;
116+
}
117+
118+
/** binary search on BIT : lower_bound and upper_bound [OPTIMIZED] using Binary Lifting **/
119+
120+
int lowerBound_BIT_efficient(LL v,int N) /** O( logN ) **/
121+
{
122+
LL sum = 0;
123+
LL pos = 0;
124+
125+
int k = 0;
126+
while (1 << (k + 1) <= N)
127+
k++;
128+
129+
/** k = log(N) **/
130+
131+
for(int i=k; i>=0; i--)
132+
{
133+
int next_idx = pos + (1 << i);
134+
135+
if( next_idx < N && sum + BIT[next_idx] < v) /// For upper_bound change to "BIT[next_idx] < v" to "BIT[next_idx] <= v"
136+
{
137+
sum += BIT[next_idx];
138+
pos = next_idx;
139+
}
140+
}
141+
142+
return pos + 1; /// +1 because 'pos' will have position of largest value less than 'v'
143+
}
144+
84145
int main()
85146
{
86147
//optimizeIO();

0 commit comments

Comments
 (0)