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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
| const int maxn = 50000 + 10; const int maxm = 8000000; const double eps = 1e-10; int n, m; int mx = 0;
class Parabola { public: int x, y, p; double a, b, c; inline double cal(double x) { return a*x*x + b*x + c; }
void read(int& mx) { scanf("%d%d%d", &p, &x, &y);
a = (double)y * 1.0 / (1ll * (x - p) * (p - x) * 1.0); b = -2 * a * x; c = -a*p*p - b*p;
mx = max(mx, 2*x-p); } } para[maxn];
// == seg Tree nested seg Tree == int tot = 0; int rt[maxn << 2]; double X[maxn << 2], _X[maxn << 2];
void init() { tot = 0; Set(rt, 0); Set(X, 0); Set(_X, 0); }
class segTree { public: int l, r; } tree[maxn << 2];
class nestTree { public: int ls, rs; double maxh; } ntree[maxm];
void spread1(int& o, int l, int r, int x, int y) { if(!o) o = ++tot; maxh(o) = y;
if(l == r) return; int mid = (l + r) >> 1; if(x <= mid) spread1(ls(o), l, mid, x, y); else spread1(rs(o), mid + 1, r, x, y); }
int merge1(int p, int q, int l, int r) { if(!p || !q) return p + q; if(l == r) return maxh(p) > maxh(q) ? p : q;
int u = ++tot; int mid = (l + r) >> 1; ls(u) = merge1(ls(p), ls(q), l, mid); rs(u) = merge1(rs(p), rs(q), mid + 1, r); maxh(u) = max(maxh(ls(u)), maxh(rs(u))); return u; }
// usage, build1(1, 1, n) void build1(int p, int l, int r) { // leaf node means lth parabola // spread 2D-segTree l(p) = l, r(p) = r; if(l == r) { spread1(rt[p], 0, mx, para[l].x, para[l].y); return; } int mid = (l + r) >> 1; build1(p<<1, l, mid); build1(p<<1|1, mid + 1, r); rt[p] = merge1(rt[p<<1], rt[p<<1|1], 0, mx); }
// query 2D, usage, query2(rt[p], 0, mx, x1, x2) // query interval [x1, x2] // query2(p), query2(ls(p), ..), query2(rs(p), ...) double query2(int p, int l, int r, int ql, int qr) { if(!p || ql > r || qr < l) return 0; if(ql <= l && r <= qr) return maxh(p); int mid = (l + r) >> 1; return max(query2(ls(p), l, mid, ql, qr), query2(rs(p), mid + 1, r, ql, qr)); }
// usage, query1(1, t1, t2, x1, x2) double query1(int p, int tl, int tr, int xl, int xr) { if(r(p) < tl || l(p) > tr) return 0; if(tl <= l(p) && r(p) <= tr) return query2(rt[p], 0, mx, xl, xr);
return max(query1(p<<1, tl, tr, xl, xr), query1(p<<1|1, tl, tr, xl, xr)); } // == seg Tree nested finished ==
// == outline seg Tree, build by parabola id, solve interval endpoint == struct Line { double xl, xr; int id;
Line(double _xl = 0.0, double _xr = 0.0, int _id = 0) : xl(_xl), xr(_xr), id(_id) {}
bool operator< (const Line& rhs) const { return xr < rhs.xr; } };
class outlineTree { public: int _l, _r; vector<Line> lines; } lineTree[maxn << 2]; #define _l(x) lineTree[x]._l #define _r(x) lineTree[x]._r #define lines(x) lineTree[x].lines
inline double intersection(int i, int j, double l, double r) { // find intersction of two parabola in seg [l, r] const Parabola& u = para[i]; const Parabola& v = para[j];
double A = u.a - v.a; double B = u.b - v.b; double C = u.c - v.c;
if(fabs(A) < eps) { // A = 0, find solution // B = 0, no solution, return xr if(fabs(B) < eps) return r; double ans = -C / B; if(l + eps < ans && ans < r - eps) return ans; return r; }
double D = B*B - 4*A*C; if(D < -eps) return r;
D = sqrt(D); double ans = r;
double x1 = (-B - D) / A / 2; if(l + eps < x1 && x1 < r - eps) ans = x1; double x2 = (-B + D) / A / 2; if(l + eps < x2 && x2 < r - eps) ans = min(ans, x2);
return ans; }
vector<Line> tmp; inline void combine(vector<Line>& A, vector<Line>& B, vector<Line>& C) { sort(A.begin(), A.end()); sort(B.begin(), B.end());
if(A.size() == 0) { C = B; return; } if(B.size() == 0) { C = A; return; }
// solve all (x, 0) to a new array _X, then unique to X tmp.clear(); int tot = 0; _for(i, 0, A.size()) { _X[++tot] = A[i].xl; _X[++tot] = A[i].xr; } _for(i, 0, B.size()) { _X[++tot] = B[i].xl; _X[++tot] = B[i].xr; } sort(_X + 1, _X + 1 + tot);
int sz = 0; for(int i = 1; i <= tot; ) { int j = i; for(; j < tot && fabs(_X[j + 1] - _X[j]) < eps; j++); X[++sz] = _X[j]; i = j + 1; }
// X[1,..,sz] is the sub segment // {l, r, paraID} construct outline int p1 = 0, p2 = 0; _for(i, 1, sz) { double l = X[i], r = X[i + 1]; while (p1 < A.size() && A[p1].xr + eps < r) p1++; while (p2 < B.size() && B[p2].xr + eps < r) p2++;
if(((p1 == A.size()) || (A[p1].xl > l + eps)) && ((p2 == B.size()) || (B[p2].xl > l + eps))) continue; if((p1 == A.size()) || (A[p1].xl > l + eps)) { tmp.push_back(Line(l, r, B[p2].id)); continue; } if((p2 == B.size()) || (B[p2].xl > l + eps)) { tmp.push_back(Line(l, r, A[p1].id)); continue; }
// intersction [l, x1, x2, ..., r], get point while (l + eps < r) { double x = intersection(A[p1].id, B[p2].id, l, r); double phi = (l + x) / 2;
if(para[A[p1].id].cal(phi) > para[B[p2].id].cal(phi)) { if(para[A[p1].id].cal(phi) >= 0) tmp.push_back(Line(l, x, A[p1].id)); } else { if(para[B[p2].id].cal(phi) >= 0) tmp.push_back(Line(l, x, B[p2].id)); }
l = x; } }
C.clear(); for(int i = 0; i < tmp.size(); ) { int j = i; for(; j < tmp.size()-1 && tmp[j + 1].id == tmp[i].id; j++); C.push_back(Line(tmp[i].xl, tmp[j].xr, tmp[i].id)); i = j + 1; } }
void build2(int p, int l, int r) { _l(p) = l, _r(p) = r; if(l == r) { const Parabola& cur = para[l]; lines(p).push_back(Line(cur.p, 2*cur.x-cur.p, l)); return; }
int mid = (l + r) >> 1; build2(p<<1, l, mid); build2(p<<1|1, mid + 1, r); combine(lines(p<<1), lines(p<<1|1), lines(p)); } // == outline seg Tree finished ==
// == solve the problem == inline double work(int p, double x) { int l = 0, r = lines(p).size()-1; int ans = r;
while (l <= r) { int mid = (l + r) >> 1; if(lines(p)[mid].xr >= x + eps) { ans = mid; r = mid - 1; } else l = mid + 1; } return para[lines(p)[ans].id].cal(x); }
void solve(int p, int tl, int tr, int x1, int x2, double& ans) { if(tl > _r(p) || tr < _l(p)) return; if(tl <= _l(p) && _r(p) <= tr) { ans = max(ans, work(p, x1)); ans = max(ans, work(p, x2)); return; }
solve(p<<1, tl, tr, x1, x2, ans); solve(p<<1|1, tl, tr, x1, x2, ans); } // == solve finished ==
int main() { freopen("flights.in", "r", stdin); freopen("flights.out", "w", stdout);
scanf("%d", &n); _rep(i, 1, n) { para[i].read(mx); }
build1(1, 1, n); build2(1, 1, n);
scanf("%d", &m); _rep(i, 1, m) { int t1, t2, x1, x2; scanf("%d%d%d%d", &t1, &t2, &x1, &x2); double ans = 0.0; ans = query1(1, t1, t2, x1, x2); solve(1, t1, t2, x1, x2, ans); cout << ans << endl; }
// debug(para[1].cal(11)); // debug(mx);
}
|