// Update function in solution 1
// just clear block and fill again whole gives me AC
void update(lli ind , lli val)
{
lli block_number = ind / blk_sz;
lli start = block_number*blk_sz;
lli endi = (block_number+1)*blk_sz;
a[ind] = val;
block[block_number].clear();
for(lli i=start;i<endi;i++)
block[block_number].pb(a[i]);
sort(all(block[block_number]));
}
// Update function in solution 2
// just find block number and for that block number find index too using binary search after that
// change it and sort again gives me WA why ?
void change_val_binary_search(vi &vc, lli crval , lli val)
{
lli l=0;
lli r = sz(vc)-1;
while(l<r)
{
lli mid = (l+r)/2;
if(vc[mid] == crval)
{
vc[mid] = val;
return ;
}
else if(vc[mid]>crval)
{
r = mid-1;
}
else
l = mid+1;
}
}
void update(lli ind , lli val)
{
lli block_number = ind / blk_sz;
lli start = block_number*blk_sz;
lli endi = (block_number+1)*blk_sz;
change_val_binary_search(block[block_number] , a[ind] , val);
a[ind] = val;
sort(all(block[block_number]));
}
I was wrong before because I just noticed that you are sorting the block so that function should still work.
did you try printing the block and check if it actually updates it or not?
Bro just tell me how to write binary search function for my above algo , as lower_bound ±3 works but if u correct my own BS function , it’s very big help .
L and R represents the range at which the element might exist and it is inclusive. So you might have to check if arr[L] or arr[R] is the element you are looking for. If you are still uncomfortable try doing binary search on paper to understand the corner cases.