#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<fstream>
#include<algorithm>
#include <iomanip>
#include<map>
#include <set>
#include <math.h>
#define ll long long
#define For(i, n) for(ll i = 0; i < (ll)n; i++)
#define ffor(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define rfor(i, n) for(ll i = (ll)n; i >= (ll)0; i--)
#define rffor(i, a, n) for(ll i = (ll)n; i >= (ll)a; i--)
#define vec vector
#define ff first
#define ss second
#define pb push_back
#define pii pair<ll, ll>
#define NEK 1000000000
#define mod (1e9 + 7)
#define rsz resize
#define prv 4
#define D 8
using namespace std;
struct budova {
vec<ll> x;
vec<ll> y;
ll ind;
};
struct udalost {
ll y, x, ind, typ;
};
bool zorad_udalosti(udalost a, udalost b) {
if (a.y == b.y) {
if (a.x == b.x) {
return a.typ < b.typ;
}
return a.x < b.x;
}
return a.y < b.y;
}
bool zorad_obdlzniky(budova a, budova b) {
return a.x[0] < b.x[0];
}
ll dist(pii a, pii b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pii finish;
cin >> finish.ff >> finish.ss;
ll n;
cin >> n;
vec<budova> p;
vec<budova> p_pov;
vec<udalost> p2;
For(i, n) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (y1 < y2) swap(y1, y2);
if (x1 > x2) swap(x1, x2);
p_pov.pb({ { x1, x2 }, { y1, y2 }, i });
p.push_back({ {x1, x2}, {y1, y2}, i });
p2.push_back({ y2, x2, i, 0});
p2.push_back({ y1 + 1, x2, i, 1 });
p2.push_back({ y2 - 1, x2 + 1, i, 2 });
p2.push_back({ y1 + 1, x2 + 1, i, 3 });
}
p2.push_back({ finish.ss, finish.ff, n, 2 });
vec<vec<ll>> next(n + 1, vec<ll>(2, 0));
sort(p2.begin(), p2.end(), zorad_udalosti);
set<pii> s;
For(i, p2.size()) {
if (p2[i].typ == 0) {
s.insert({ p2[i].x, p2[i].ind });
}
if (p2[i].typ == 1) {
s.erase({ p2[i].x, p2[i].ind });
}
if (p2[i].typ == 2) {
auto q = s.lower_bound({ p2[i].x, p2[i].ind });
ll dalsi = -1;
if (q != s.begin()) {
q--;
dalsi = q->second;
}
next[p2[i].ind][0] = dalsi;
}
if (p2[i].typ == 3) {
auto q = s.lower_bound({ p2[i].x, p2[i].ind });
ll dalsi = -1;
if (q != s.begin()) {
q--;
dalsi = q->second;
}
next[p2[i].ind][1] = dalsi;
}
}
sort(p.begin(), p.end(), zorad_obdlzniky);
vec<vec<ll>> dp(n, vec<ll>(2, NEK));
pii start = { 0, 0 };
For(i, n) {
pii roh = { p[i].x[1] + 1, p[i].y[1] - 1 };
ll o = next[p[i].ind][0];
if (o == -1) dp[p[i].ind][0] = dist(start, roh);
else {
pii roh_1_z = { p_pov[o].x[1] + 1, p_pov[o].y[1] - 1 };
pii roh_2_z = { p_pov[o].x[1] + 1, p_pov[o].y[0] + 1 };
dp[p[i].ind][0] = min(dp[o][0] + dist(roh_1_z, roh), dp[o][1] + dist(roh_2_z, roh));
}
roh = { p[i].x[1] + 1, p[i].y[0] + 1 };
o = next[p[i].ind][1];
if (o == -1) dp[p[i].ind][1] = dist(start, roh);
else {
pii roh_1_z = { p_pov[o].x[1] + 1, p_pov[o].y[1] - 1 };
pii roh_2_z = { p_pov[o].x[1] + 1, p_pov[o].y[0] + 1 };
dp[p[i].ind][1] = min((dp[o][0] + dist(roh_1_z, roh)), (dp[o][1] + dist(roh_2_z, roh)));
}
}
ll o = next[n][0];
if (o == -1) {
ll odp = dist(start, finish);
cout << odp << '\n';
return 0;
}
pii roh1 = { p_pov[o].x[1] + 1, p_pov[o].y[1] - 1 };
pii roh2 = { p_pov[o].x[1] + 1, p_pov[o].y[0] + 1 };
ll d1 = dist(roh1, finish);
ll d2 = dist(roh2, finish);
ll odp = min(dp[o][0] + d1, dp[o][1] + d2);
cout << odp << '\n';
cout << 2 << '\n';
cout << (finish.ff - start.ff) << ' ' << 0 << ' ' << 0 << ' ' << (finish.ss - start.ss) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
348 KB |
Partially correct (80% score). Path intersects a building. |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct (80% score). Path intersects a building. |
3 |
Partially correct |
1 ms |
348 KB |
Partially correct (80% score). Path intersects a building. |
4 |
Partially correct |
2 ms |
1120 KB |
Partially correct (80% score). Path intersects a building. |
5 |
Partially correct |
273 ms |
43844 KB |
Partially correct (80% score). Path intersects a building. |
6 |
Partially correct |
79 ms |
14368 KB |
Partially correct (80% score). Path intersects a building. |
7 |
Partially correct |
149 ms |
24064 KB |
Partially correct (80% score). Path intersects a building. |
8 |
Partially correct |
358 ms |
48440 KB |
Partially correct (80% score). Path intersects a building. |
9 |
Partially correct |
326 ms |
50676 KB |
Partially correct (80% score). Path intersects a building. |
10 |
Partially correct |
314 ms |
48280 KB |
Partially correct (80% score). Path intersects a building. |