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
| #ifdef ShadowDrunk
template<class T> class LiSegTree {
private : static constexpr double eps = 1e-9; static constexpr double inf = 1e18;
int n; int m;
int cmp(double x, double y){ if(x - y > eps) return 1; if(y - x > eps) return -1; return 0; }
struct line{ double k, b; line(double _k, double _b){ k = _k; b = _b; } line(){ k = 0; b = -inf; } };
std::vector<line> p; int cnt = 0; std::vector<int> tag;
double calc(int id, T d){ return p[id].b + p[id].k * d; }
void add(T x0, T y0, T x1, T y1){ cnt += 1; if(x0 == x1){ p[cnt].k = 0; p[cnt].b = std::max(y0, y1); } else{ p[cnt].k = 1.0 * (y1 - y0) / (x1 - x0); p[cnt].b = y0 - p[cnt].k * x0; } }
void change(int root, T pl, T pr, int u){ int &v = tag[root]; T mid = (pl + pr) >> 1; int bmid = cmp(calc(u, mid), calc(v, mid)); if(bmid == 1 || (!bmid && u < v)) std::swap(u, v);
int bl = cmp(calc(u, pl), calc(v, pl)); int br = cmp(calc(u, pr), calc(v, pr)); if(bl == 1 || (!bl && u < v)) change(root << 1, pl, mid, u); if(br == 1 || (!br && u < v)) change((root << 1) | 1, mid + 1, pr, u);
}
void update(int root, T pl, T pr, T l, T r, int u){ if(l <= pl && pr <= r){ change(root, pl, pr, u); return; } T mid = (pl + pr) >> 1; if(l <= mid) update(root << 1, pl, mid, l, r, u); if(r > mid) update((root << 1) | 1, mid + 1, pr, l, r, u); }
std::pair<double, int> pmax(std::pair<double, int> x, std::pair<double, int> y){ if(cmp(x.ff, y.ff) == -1) return y; else if(cmp(x.ff, y.ff) == 1) return x; else return (x.ss < y.ss ? x : y); }
std::pair<double, int> pmin(std::pair<double, int> x, std::pair<double, int> y){ if(cmp(x.ff, y.ff) == -1) return x; else if(cmp(x.ff, y.ff) == 1) return y; else return (x.ss < y.ss ? x : y); }
std::pair<double, int> query(int root, T l, T r, T d){ if(r < d || l > d) return {-inf, 0}; T mid = (l + r) >> 1; double res = calc(tag[root], d); if(l == r) return {res, tag[root]}; return pmax({res, tag[root]}, pmax(query(root << 1, l, mid, d), query((root << 1) | 1, mid + 1, r, d)));
}
public :
void addline(T x0, T y0, T x1, T y1){ if(x0 > x1){ std::swap(x0, x1); std::swap(y0, y1); } add(x0, y0, x1, y1); update(1, 1, n, x0, x1, cnt); }
std::pair<double, int> query(T d){ return query(1, 1, n, d); }
LiSegTree(int _n, int _m){ n = _n, m = _m; cnt = 0; tag.assign(n << 2, 0); p.assign(m + 1, line()); }
LiSegTree(){ n = 5e5, m = 5e5; cnt = 0; tag.assign(n << 2, 0); p.assign(m + 1, line()); } };
#endif
|