Advertisement
LinKin

Convex Trick

Jun 26th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. void INSERT(Long m, Long c)
  2. {
  3.     Long m1, c1, m2, c2;
  4.     while(head > tail+1)
  5.     {
  6.         m1 = DQ[head-1].first;
  7.         c1 = DQ[head-1].second;
  8.         m2 = DQ[head-2].first;
  9.         c2 = DQ[head-2].second;
  10.  
  11.         if( (c2-c1)*(m2-m) >= (m2-m1)*(c2-c) )
  12.             head--;
  13.         /*if( (c2-c1)*(m1-m) >= (m2-m1)*(c1-c) )
  14.             head--;*/
  15.         else
  16.             break;
  17.     }
  18.  
  19.     DQ[head++] = PLL(m, c);
  20. }
  21.  
  22. Long QUERY(Long x)
  23. {
  24.     Long m1, c1, m2, c2;
  25.     while( head > tail+1 )
  26.     {
  27.         m1 = DQ[tail].first;
  28.         c1 = DQ[tail].second;
  29.         m2 = DQ[tail+1].first;
  30.         c2 = DQ[tail+1].second;
  31.  
  32.         if(x*(m2-m1) >= (c2 - c1))
  33.             tail++;
  34.         else
  35.             break;
  36.     }
  37.  
  38.     return -DQ[tail].first*x + DQ[tail].second;
  39. }
  40. /* here storing positive m,actually -m , returns minimum */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement