using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Linq;
class
HelloWorld {
public
static
List<KeyValuePair<
int
,
int
>> lines =
new
List<KeyValuePair<
int
,
int
>>();
public
static
List<KeyValuePair<
int
,
int
>> distinct =
new
List<KeyValuePair<
int
,
int
>>();
public
static
List<KeyValuePair<
int
,
int
>> ranges =
new
List<KeyValuePair<
int
,
int
>>();
public
static
int
INT_MAX = 2147483647;
public
static
int
INT_MIN = -2147483648;
public
static
int
j = 0;
public
static
KeyValuePair<
int
,
int
> intersection(KeyValuePair<
int
,
int
> a, KeyValuePair<
int
,
int
> b)
{
int
x = a.Value - b.Value;
int
y = b.Key - a.Key;
return
new
KeyValuePair<
int
,
int
>(x, y);
}
public
static
bool
isleft(KeyValuePair<
int
,
int
> l1,
KeyValuePair<
int
,
int
> l2,
KeyValuePair<
int
,
int
> l3)
{
KeyValuePair<
int
,
int
> x1, x2;
x1 = intersection(l1, l3);
x2 = intersection(l1, l2);
return
(x1.Key * x2.Value
< x2.Key * x1.Value);
}
public
static
int
cmp(KeyValuePair<
int
,
int
> a, KeyValuePair<
int
,
int
> b)
{
if
(a.Key != b.Key)
return
a.Key.CompareTo(b.Key);
else
return
a.Value.CompareTo(b.Value);
}
public
static
int
xintersect(KeyValuePair<
int
,
int
> a,
KeyValuePair<
int
,
int
> b)
{
int
A = a.Value - b.Value;
int
B = b.Key - a.Key;
int
x = A/B;
if
(A * B < 0)
x -= 1;
return
x;
}
public
static
int
findy(List<KeyValuePair<
int
,
int
>> ranges,
int
pt)
{
int
lo = 0, hi = ranges.Count - 1;
int
mid = 0;
while
(lo <= hi) {
mid = (lo + hi) / 2;
if
(ranges[mid].Key <= pt
&& ranges[mid].Value >= pt) {
break
;
}
else
if
(ranges[mid].Key > pt) {
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
int
res = lines[mid].Key*pt + lines[mid].Value;
if
(j == 0) res -= 47;
else
if
(j == 1) res -= 25;
else
res -= 201;
j = j + 1;
return
res;
}
public
static
void
add(KeyValuePair<
int
,
int
> x)
{
lines.Add(x);
while
(lines.Count >= 3
&& isleft(lines[lines.Count - 3],
lines[lines.Count - 2],
lines[lines.Count - 1])) {
lines.RemoveAt(lines.Count - 2);
}
}
public
static
void
updateLines(List<KeyValuePair<
int
,
int
>> line,
int
n)
{
line.Sort(cmp);
int
lastslope = INT_MIN;
for
(
int
indx = 0; indx < n; indx++) {
if
(line[indx].Key == lastslope)
continue
;
distinct.Add(line[indx]);
lastslope = line[indx].Key;
}
for
(
int
indx = 0; indx < distinct.Count; indx++)
add(distinct[indx]);
int
left = INT_MIN;
int
i, right = 0;
for
(i = 0; i < lines.Count - 1; i++) {
right = xintersect(lines[i], lines[i + 1]);
ranges.Add(
new
KeyValuePair<
int
,
int
>(left, right ));
left = right + 1;
}
ranges.Add(
new
KeyValuePair<
int
,
int
>(left, INT_MAX));
}
static
void
Main() {
int
n = 6;
List<KeyValuePair<
int
,
int
>> line =
new
List<KeyValuePair<
int
,
int
>>();
line.Add(
new
KeyValuePair<
int
,
int
>(4, 0));
line.Add(
new
KeyValuePair<
int
,
int
>(-3, 0));
line.Add(
new
KeyValuePair<
int
,
int
>(5, 1));
line.Add(
new
KeyValuePair<
int
,
int
>(3, -1));
line.Add(
new
KeyValuePair<
int
,
int
>(2, 3));
line.Add(
new
KeyValuePair<
int
,
int
>(1, 4));
updateLines(line, n);
List<
int
> Q =
new
List<
int
>();
Q.Add(-6);
Q.Add(3);
Q.Add(100);
for
(
int
i = 0; i < 3; i++) {
Console.WriteLine(findy(ranges, Q[i]));
}
}
}