제출 #1331738

#제출 시각아이디문제언어결과실행 시간메모리
1331738tung04885Walk (CEOI06_walk)C++20
0 / 100
35 ms5468 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 100100
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define __buitin_popcount __builtin_popcountll
#define BIT(x,i) (((x)>>(i))&1ll)
#define MASK(i) (1ll<<(i))
// removed: #define left __left
// removed: #define right __right

template<class X,class Y> bool maximize(X &x,Y y)
{
    if(x<y)
    {
        x=y;
        return 1;
    }
    return 0;
}

template<class X,class Y> bool minimize(X &x,Y y)
{
    if(y<x)
    {
        x=y;
        return 1;
    }
    return 0;
}

const int inf=1e9+412009;
const ll INF=2e18+412009;

struct POINT
{
    int x,y;
    int id;

    void input(int i=0)
    {
        cin>>x>>y;
        id=i;
    }

    bool operator < (POINT other) const
    {
        return x==other.x ? y<other.y : x<other.x;
    }
};

double LEN(POINT a,POINT b)
{
    return abs(a.x-b.x)+abs(a.y-b.y);
}

struct RECT
{
    int top=0,bot=0;
    int L=0,R=0; 
    int id;

    void input(int i=0)
    {
        id=i;

        int x,y,u,v;

        cin>>x>>y>>u>>v;

        top=max(y,v);

        bot=min(y,v);

        R=max(x,u);

        L=min(x,u);
    }

    bool operator < (RECT other) const
    {
        return L<other.L;
    }
};

POINT T;
int n;
RECT a[MAX];

void nhap()
{
    T.input(1);

    int tmp;

    cin>>tmp;

    for(int i=1;i<=tmp;i++)
    {
        RECT vl; vl.input();

        if(T.x<=vl.R) continue;

        a[++n]=vl;
    }
}

ll dist[MAX][2];
pii trace[MAX][2];
set<pii> s;

void solve()
{
    memset(dist,0x3f,sizeof(dist));

    sort(a+1,a+n+1);

    s.insert({0,0});
    dist[0][0]=0;

    for(int i=1;i<=n;i++)
    {
        for(auto it=s.lower_bound({a[i].bot,-inf}); it!=s.end() && it->fi<=a[i].top; )
        {
            int id=abs(it->se), type=it->se>0;

            if(minimize(dist[i][0], a[i].L - a[id].L + abs(a[i].bot-1-it->fi)+ dist[id][type])) trace[i][0]=*it;

            if(minimize(dist[i][1], a[i].L - a[id].L + abs(a[i].top+1 - it->fi) + dist[id][type])) trace[i][1]=*it;

            auto it1=it;
            ++it;
            s.erase(it1);
        }

        if(dist[i][0]<INF) s.insert({a[i].bot-1,-i});
        if(dist[i][1]<INF) s.insert({a[i].top+1,i});
    }

    ll ans=INF;

    for(pii it:s)
    {
        int id=abs(it.se), type=it.se>0;

        if(minimize(ans, dist[id][type] + abs(it.fi - T.y) + abs(a[id].L - T.x))) trace[n+1][0]=it;
    }

    cout<<ans<<'\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    nhap();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...