# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1014168 | vjudge1 | Walk (CEOI06_walk) | C++17 | 46 ms | 6236 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include"debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 15;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int offset = 1e6 + 5;
const int off = (1<<21);
struct sgt {
#define ls (idx<<1)
#define rs (idx<<1|1)
int t[off<<1];
int unit = 0;
int get(int idx){
int mx = t[idx+=off];
while (idx/=2)
ckmax(mx, t[idx]);
return mx;
}
void update(int l, int r, int v, int idx=1, int lo=0, int hi=off){
if (r <= lo || hi <= l)
return;
if (l <= lo && hi <= r){
t[idx] = v;
return;
}
int mi = (lo+hi)>>1;
update(l, r, v, ls, lo, mi);
update(l, r, v, rs, mi, hi);
return;
}
} seg;
int dp[MAXN][2];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int x, y, n;
cin >> x >> y >> n;
y += offset;
vector<pair<pii, pii>> a(n + 1);
vector<array<int, 3>> lines(n + 1);
for (int i = 1; i <= n; i++){
cin >> a[i].F.F >> a[i].F.S >> a[i].S.F >> a[i].S.S;
a[i].F.S += offset;
a[i].S.S += offset;
if (a[i].F.F > a[i].S.F) swap(a[i].F.F, a[i].S.F);
if (a[i].F.S < a[i].S.S) swap(a[i].F.S, a[i].S.S);
lines[i] = {a[i].S.F + 1, a[i].F.S + 1, a[i].S.S - 1}; // x axis, up, down
}
lines.pb({x, y, y});
sort(1 + all(lines), [](array<int, 3> lhs, array<int, 3> rhs){
if (lhs[0] != rhs[0]) return lhs[0] < rhs[0];
return lhs[2] < rhs[2];
});
for (int i = 1; i <= n + 1; i++){
int up = seg.get(lines[i][1]);
int dw = seg.get(lines[i][2]);
if (up == 0){
dp[i][0] = lines[i][0] + abs(offset - lines[i][1]);
} else {
dp[i][0] = lines[i][0] - lines[up][0] + min(abs(lines[up][1] - lines[i][1]) + dp[up][0], abs(lines[up][2] - lines[i][1]) + dp[up][1]);
}
if (dw == 0){
dp[i][1] = lines[i][0] + abs(100 - lines[i][2]);
} else {
dp[i][1] = lines[i][0] - lines[dw][0] + min(abs(lines[dw][1] - lines[i][2]) + dp[dw][0], abs(lines[dw][2] - lines[i][2]) + dp[dw][1]);
}
seg.update(lines[i][2], lines[i][1] + 1, i);
if (lines[i][0] == x && lines[i][1] == y){
cout << min(dp[i][0], dp[i][1]) << endl;
}
debug(i, dp[i][0], dp[i][1], lines[i], up, dw);
}
debug(a);
debug(lines);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |