#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) {
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';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Unexpected end of file - token expected |
2 |
Incorrect |
1 ms |
348 KB |
Unexpected end of file - token expected |
3 |
Incorrect |
1 ms |
348 KB |
Unexpected end of file - token expected |
4 |
Incorrect |
3 ms |
1112 KB |
Unexpected end of file - token expected |
5 |
Incorrect |
285 ms |
44108 KB |
Found length 175741, official output says 175579. |
6 |
Incorrect |
80 ms |
15136 KB |
Unexpected end of file - token expected |
7 |
Incorrect |
154 ms |
25876 KB |
Unexpected end of file - token expected |
8 |
Incorrect |
322 ms |
51064 KB |
Found length 16353, official output says 16349. |
9 |
Incorrect |
343 ms |
52836 KB |
Found length 219559, official output says 217249. |
10 |
Incorrect |
326 ms |
52752 KB |
Found length 197941, official output says 196657. |