#define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using dbl = long double; constexpr dbl INF = 1e30; constexpr dbl EPS = 1e-7; struct SegTree { struct Node { pair<dbl, int> elem = {INF, 1e9}; pair<dbl, int> opt = {INF, 1e9}; dbl lazy = {INF}; DBP(elem, opt, lazy); }; vector<Node> tree; int len; SegTree() {} SegTree(const vector<dbl>& elems) { for (len = 1; len < sz(elems); len *= 2); tree.resize(len*2); rep(i, 0, sz(elems)) { tree[i+len] = { {elems[i], i}, {INF, i}, INF }; } for (int i = len-1; i > 0; i--) { update(i); } } void update(int i) { tree[i].elem = min(tree[i*2].elem, tree[i*2+1].elem); tree[i].opt = min(tree[i*2].opt, tree[i*2+1].opt); tree[i].opt = min(tree[i].opt, mp(tree[i].elem.x+tree[i].lazy, tree[i].elem.y)); } void del(int i) { tree[i += len] = {}; while (i /= 2) update(i); } void rangeMin(int vb, int ve, dbl val, int i = 1, int b = 0, int e = -1) { if (e < 0) e = len; if (b >= ve || vb >= e || vb >= ve) return; if (b >= vb && e <= ve) { tree[i].lazy = min(tree[i].lazy, val); tree[i].opt = min(tree[i].opt, mp(tree[i].elem.x+tree[i].lazy, tree[i].elem.y)); return; } int m = (b+e) / 2; rangeMin(vb, ve, val, i*2, b, m); rangeMin(vb, ve, val, i*2+1, m, e); update(i); } pair<dbl, int> get() const { return tree[1].opt; } }; struct Segment { dbl begin, end; dbl arrival{INF}; bool done{false}; DBP(begin, end, arrival, done); Segment(dbl b, dbl e) : begin(b), end(e) {} }; struct Street { vector<Segment> segments; dbl speed; SegTree que1, que2; DBP(segments, speed); Street() {} Street(const vector<Segment>& segs, dbl v) : segments(segs), speed(v) {} void initQue(dbl vc, dbl vj) { vector<dbl> c1, c2; each(s, segments) { c1.pb(s.begin / (vc-speed)); c2.pb(s.begin / (vj-speed)); } que1 = {c1}; que2 = {c2}; } void del(int i) { que1.del(i); que2.del(i); } pair<dbl, int> get() const { return min(que1.get(), que2.get()); } }; dbl vc; Street s[3]; void hop(int from, dbl begin, dbl end, dbl time) { dbl vi = s[from].speed; dbl beginPos = begin + vi * time; dbl wallTime = time + (end-begin) / (vc - vi); dbl wallPos = end + vi * wallTime; if (from <= 1) { // hop right auto& segs = s[from+1].segments; dbl vj = s[from+1].speed; int first = int(lower_bound(all(segs), beginPos - vj*time - EPS, [&](const Segment& seg, dbl r) { return seg.end < r; }) - segs.begin()); if (beginPos > segs[first].begin + vj * time + EPS) { if (from == 1) { hop(from+1, beginPos - vj*time, segs[first].end, time); } first++; } int second = int(lower_bound(all(segs), (wallTime - time) * (vc - vj) - vj * time + beginPos, [&](const Segment& seg, dbl r) { return seg.begin < r; }) - segs.begin()); s[from+1].que1.rangeMin(first, second, time + (vj * time - beginPos) / (vc - vj)); s[from+1].que2.rangeMin(second, sz(segs), wallTime + (vj * wallTime - wallPos) / (vi - vj)); } if (from >= 1) { // hop left auto& segs = s[from-1].segments; dbl vj = s[from-1].speed; int first = int(lower_bound(all(segs), beginPos - vj*time - EPS, [&](const Segment& seg, dbl r) { return seg.end < r; }) - segs.begin()); if (first > 0 && from == 1) { auto& seg = segs[first-1]; dbl e = seg.end + vj * time; dbl arrival = time + (e - beginPos) / (vc - vj); if (arrival > wallTime) { e = seg.end + vj * wallTime; arrival = wallTime + (e - wallPos) / (vi - vj); } hop(from-1, seg.end, seg.end, arrival); } if (beginPos > segs[first].begin + vj * time + EPS) { if (from == 1) { hop(from-1, beginPos - vj*time, segs[first].end, time); } first++; } int second = int(lower_bound(all(segs), (wallTime - time) * (vc - vj) - vj * time + beginPos + EPS, [&](const Segment& seg, dbl r) { return seg.begin < r; }) - segs.begin()); s[from-1].que1.rangeMin(first, second, time + (vj * time - beginPos) / (vc - vj)); } } Street readStreet(dbl v) { string tmp; cin >> tmp; vector<Segment> segs; ll begin = 0; rep(i, 1, sz(tmp)) { if (tmp[i] == '#') { if (begin < i) segs.pb({dbl(begin), dbl(i-1)}); begin = i+1; } } segs.pb({dbl(begin), INF}); return {segs, dbl(v)}; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); ll L; dbl v0, v1, v2; cin >> L >> vc >> v0 >> v1 >> v2; s[0] = readStreet(v0); s[1] = readStreet(v1); s[2] = readStreet(v2); s[0].initQue(vc, vc); s[1].initQue(vc, v0); s[2].initQue(vc, v1); s[1].segments[0].arrival = 0; s[1].que1.rangeMin(0, 1, 0); while (!s[1].segments.back().done) { dbl minTime = INF; int type = -1, id = -1; rep(j, 0, 3) { pair<dbl, int> alt = s[j].get(); if (alt.y < sz(s[j].segments) && alt.x < minTime) { minTime = alt.x; type = j; id = alt.y; } } assert(id != -1); auto &seg = s[type].segments[id]; assert(!seg.done); seg.done = true; seg.arrival = minTime; s[type].del(id); hop(type, seg.begin, seg.end, seg.arrival); } auto& seg = s[1].segments.back(); dbl midPos = seg.begin + s[1].speed * seg.arrival; dbl ans = seg.arrival; for (int i : {0, 2}) { auto& other = s[i].segments.back(); dbl b = other.begin + s[i].speed * seg.arrival; if (midPos < b) { ans = max(ans, seg.arrival + (b - midPos) / (vc - s[i].speed)); } } cout << ans << '\n'; return 0; }
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using dbl = long double; constexpr dbl INF = 1e30; constexpr dbl EPS = 1e-7; struct SegTree { struct Node { pair<dbl, int> elem = {INF, 1e9}; pair<dbl, int> opt = {INF, 1e9}; dbl lazy = {INF}; DBP(elem, opt, lazy); }; vector<Node> tree; int len; SegTree() {} SegTree(const vector<dbl>& elems) { for (len = 1; len < sz(elems); len *= 2); tree.resize(len*2); rep(i, 0, sz(elems)) { tree[i+len] = { {elems[i], i}, {INF, i}, INF }; } for (int i = len-1; i > 0; i--) { update(i); } } void update(int i) { tree[i].elem = min(tree[i*2].elem, tree[i*2+1].elem); tree[i].opt = min(tree[i*2].opt, tree[i*2+1].opt); tree[i].opt = min(tree[i].opt, mp(tree[i].elem.x+tree[i].lazy, tree[i].elem.y)); } void del(int i) { tree[i += len] = {}; while (i /= 2) update(i); } void rangeMin(int vb, int ve, dbl val, int i = 1, int b = 0, int e = -1) { if (e < 0) e = len; if (b >= ve || vb >= e || vb >= ve) return; if (b >= vb && e <= ve) { tree[i].lazy = min(tree[i].lazy, val); tree[i].opt = min(tree[i].opt, mp(tree[i].elem.x+tree[i].lazy, tree[i].elem.y)); return; } int m = (b+e) / 2; rangeMin(vb, ve, val, i*2, b, m); rangeMin(vb, ve, val, i*2+1, m, e); update(i); } pair<dbl, int> get() const { return tree[1].opt; } }; struct Segment { dbl begin, end; dbl arrival{INF}; bool done{false}; DBP(begin, end, arrival, done); Segment(dbl b, dbl e) : begin(b), end(e) {} }; struct Street { vector<Segment> segments; dbl speed; SegTree que1, que2; DBP(segments, speed); Street() {} Street(const vector<Segment>& segs, dbl v) : segments(segs), speed(v) {} void initQue(dbl vc, dbl vj) { vector<dbl> c1, c2; each(s, segments) { c1.pb(s.begin / (vc-speed)); c2.pb(s.begin / (vj-speed)); } que1 = {c1}; que2 = {c2}; } void del(int i) { que1.del(i); que2.del(i); } pair<dbl, int> get() const { return min(que1.get(), que2.get()); } }; dbl vc; Street s[3]; void hop(int from, dbl begin, dbl end, dbl time) { dbl vi = s[from].speed; dbl beginPos = begin + vi * time; dbl wallTime = time + (end-begin) / (vc - vi); dbl wallPos = end + vi * wallTime; if (from <= 1) { // hop right auto& segs = s[from+1].segments; dbl vj = s[from+1].speed; int first = int(lower_bound(all(segs), beginPos - vj*time - EPS, [&](const Segment& seg, dbl r) { return seg.end < r; }) - segs.begin()); if (beginPos > segs[first].begin + vj * time + EPS) { if (from == 1) { hop(from+1, beginPos - vj*time, segs[first].end, time); } first++; } int second = int(lower_bound(all(segs), (wallTime - time) * (vc - vj) - vj * time + beginPos, [&](const Segment& seg, dbl r) { return seg.begin < r; }) - segs.begin()); s[from+1].que1.rangeMin(first, second, time + (vj * time - beginPos) / (vc - vj)); s[from+1].que2.rangeMin(second, sz(segs), wallTime + (vj * wallTime - wallPos) / (vi - vj)); } if (from >= 1) { // hop left auto& segs = s[from-1].segments; dbl vj = s[from-1].speed; int first = int(lower_bound(all(segs), beginPos - vj*time - EPS, [&](const Segment& seg, dbl r) { return seg.end < r; }) - segs.begin()); if (first > 0 && from == 1) { auto& seg = segs[first-1]; dbl e = seg.end + vj * time; dbl arrival = time + (e - beginPos) / (vc - vj); if (arrival > wallTime) { e = seg.end + vj * wallTime; arrival = wallTime + (e - wallPos) / (vi - vj); } hop(from-1, seg.end, seg.end, arrival); } if (beginPos > segs[first].begin + vj * time + EPS) { if (from == 1) { hop(from-1, beginPos - vj*time, segs[first].end, time); } first++; } int second = int(lower_bound(all(segs), (wallTime - time) * (vc - vj) - vj * time + beginPos + EPS, [&](const Segment& seg, dbl r) { return seg.begin < r; }) - segs.begin()); s[from-1].que1.rangeMin(first, second, time + (vj * time - beginPos) / (vc - vj)); } } Street readStreet(dbl v) { string tmp; cin >> tmp; vector<Segment> segs; ll begin = 0; rep(i, 1, sz(tmp)) { if (tmp[i] == '#') { if (begin < i) segs.pb({dbl(begin), dbl(i-1)}); begin = i+1; } } segs.pb({dbl(begin), INF}); return {segs, dbl(v)}; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); ll L; dbl v0, v1, v2; cin >> L >> vc >> v0 >> v1 >> v2; s[0] = readStreet(v0); s[1] = readStreet(v1); s[2] = readStreet(v2); s[0].initQue(vc, vc); s[1].initQue(vc, v0); s[2].initQue(vc, v1); s[1].segments[0].arrival = 0; s[1].que1.rangeMin(0, 1, 0); while (!s[1].segments.back().done) { dbl minTime = INF; int type = -1, id = -1; rep(j, 0, 3) { pair<dbl, int> alt = s[j].get(); if (alt.y < sz(s[j].segments) && alt.x < minTime) { minTime = alt.x; type = j; id = alt.y; } } assert(id != -1); auto &seg = s[type].segments[id]; assert(!seg.done); seg.done = true; seg.arrival = minTime; s[type].del(id); hop(type, seg.begin, seg.end, seg.arrival); } auto& seg = s[1].segments.back(); dbl midPos = seg.begin + s[1].speed * seg.arrival; dbl ans = seg.arrival; for (int i : {0, 2}) { auto& other = s[i].segments.back(); dbl b = other.begin + s[i].speed * seg.arrival; if (midPos < b) { ans = max(ans, seg.arrival + (b - midPos) / (vc - s[i].speed)); } } cout << ans << '\n'; return 0; } |