3

Given an array where each element contains 2 numbers : a(i),b(i) i.e

array[0] = pair(a(0),b(0))...

array[i] = pair(a(i),b(i))

There are two type of operations :

UPDATE i,c,d : array[i] = pair(c,d) i.e array[i] is updated to new pair

QUERY x : Find maximum of all : (a(i))*x + b(i)

I created a segment tree with each node as pair of ((a(i)),(b(i)))

Now my doubt is since every query requires multiplying first member of each node of segment tree with x , complexity for each query turns to be O(n)

How can I reduce the complexity to O(log n ).

MasterMind
  • 49
  • 1
  • 2

1 Answers1

0

Order the array by a[i]. Build the set of the top edges using a Graham scan. Basically start with the first edge, then every time you add an edge check if the previous one remains visible and remove it if not, and repeat until the previous one remains visible.

Once you have the set, store the points where you switch from one line to another (intersections between the visible lines). Then for a query do a binary search to figure out between which point it falls and take the line there O(log N).

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sorin
  • 11,863
  • 22
  • 26
  • What do you mean by edges here ? – MasterMind Jan 08 '16 at 13:30
  • `ax + b` defines a line. a edge is one of those lines, sorry for being confusing. – Sorin Jan 08 '16 at 14:26
  • every time a line is added all previous lines need to be checked,which leads to O(n) per update.Much lower complexity is required though.It would be great if you could explain how to achieve this – MasterMind Jan 09 '16 at 17:24
  • Your question was about queries. Regardless, adding a line is easy, you binary search to find where you line will be in the solution, then check if you're not completely hidden by the next one, and if not remove all the hidden segments from before. Since each line can still only be removed once you're still amortized O(log n)(from the search). – Sorin Jan 11 '16 at 10:17
  • To remove a line I'm not fully sure. I think you can keep the lines you removed for each line added, when you remove it, try to add those back. But since a line can hide many other lines it might still be O(N) per removal. – Sorin Jan 11 '16 at 10:19
  • Asked on cstheory: https://cstheory.stackexchange.com/q/10894 – xskxzr Aug 30 '21 at 03:23