-
Notifications
You must be signed in to change notification settings - Fork 0
/
POLY.cpp
173 lines (143 loc) · 4.57 KB
/
POLY.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//30 Points
//Reference for Convex Hull Trick : Codeforces.com
#include<bits/stdc++.h>
#include<set>
#include<cmath>
#include<algorithm>
#define ll long long int
using namespace std;
class Dconvexhull
{
typedef long long co_eff;
typedef long long co_ord;
typedef long long setval;
private:
struct Line
{
co_eff a, b;
double xLeft;
enum Type {line, maxqry, minqry} type;
co_ord val;
explicit Line(co_eff a1=0, co_eff b1=0) : a(a1), b(b1), xLeft(-INFINITY), type(Type::line), val(0) {}
setval value_at(co_ord x) const
{ return a*x+b; }
friend bool areParallel(const Line& l1, const Line& l2)
{ return l1.a==l2.a; }
friend double intersectX(const Line& l1, const Line& l2)
{ return areParallel(l1,l2)?INFINITY:1.0*(l2.b-l1.b)/(l1.a-l2.a); }
bool operator<(const Line& l2) const
{
if (l2.type == line)
return this->a < l2.a;
if (l2.type == maxqry)
return this->xLeft < l2.val;
if (l2.type == minqry)
return this->xLeft > l2.val;
}
};
private:
bool isMax;
std::set<Line> hull;
private:
bool hasPrev(std::set<Line>::iterator it)
{ return it!=hull.begin(); }
bool hasNext(std::set<Line>::iterator it)
{ return it!=hull.end() && std::next(it)!=hull.end(); }
bool irrelvt(const Line& l1, const Line& l2, const Line& l3)
{ return intersectX(l1,l3) <= intersectX(l1,l2); }
bool irrelvt(std::set<Line>::iterator it)
{
return hasPrev(it) && hasNext(it)
&& ( isMax && irrelvt(*std::prev(it), *it, *std::next(it))
|| !isMax && irrelvt(*std::next(it), *it, *std::prev(it)) );
}
std::set<Line>::iterator update_l_b(std::set<Line>::iterator it)
{
if (isMax && !hasPrev(it) || !isMax && !hasNext(it))
return it;
double val = intersectX(*it, isMax?*std::prev(it):*std::next(it));
Line buf(*it);
it = hull.erase(it);
buf.xLeft = val;
it = hull.insert(it, buf);
return it;
}
public:
explicit Dconvexhull(bool isMax): isMax(isMax) {}
void add_Line(co_eff a, co_eff b)
{
Line l3 = Line(a, b);
auto it = hull.lower_bound(l3);
if (it!=hull.end() && areParallel(*it, l3))
{
if (isMax && it->b < b || !isMax && it->b > b)
it = hull.erase(it);
else
return;
}
it = hull.insert(it, l3);
if (irrelvt(it)) { hull.erase(it); return; }
while (hasPrev(it) && irrelvt(std::prev(it))) hull.erase(std::prev(it));
while (hasNext(it) && irrelvt(std::next(it))) hull.erase(std::next(it));
it = update_l_b(it);
if (hasPrev(it))
update_l_b(std::prev(it));
if (hasNext(it))
update_l_b(std::next(it));
}
setval Min_val_at(co_ord x) const
{
Line q;
q.val = x;
q.type = isMax ? Line::Type::maxqry : Line::Type::minqry;
//cout << isMax << "\n";
auto bestLine = hull.lower_bound(q);
if (isMax) --bestLine;
return bestLine->value_at(x);
}
};
int main()
{
ll t=0;
cin >>t;
while(t--)
{
ll N,qry,Q;
Dconvexhull h(false);
cin>>N;
ll count=0;
ll arr[N][4];
for(ll i=0; i<N; i++)
{
cin>>arr[i][0]>>arr[i][1]>>arr[i][2]>>arr[i][3];
h.add_Line(arr[i][1],arr[i][0]);
if(arr[i][2]==0 && arr[i][3]==0)
++count;
}
cin>>Q;
while(Q--)
{
cin>>qry;
/* When count == n then we get co-efficients such that they will become straight lines.
So using Dconvexhull we can find minimise the value. */
if(count==N)
{
cout<<h.Min_val_at(qry)<<endl;
}
else
{
ll result =0, min=0;
for(int i=0; i<N; i++)
{
result= arr[i][0] + (arr[i][1] *qry) +(arr[i][2]*(qry*qry)) + (arr[i][3] * (qry*qry*qry));
if(i == 0)
min = result;
else if(result<min)
min = result;
}
cout<<min<<endl;
}
}
}
return 0;
}