Submission #104528

#TimeUsernameProblemLanguageResultExecution timeMemory
104528BenqGolf (JOI17_golf)C++14
30 / 100
10093 ms487908 KiB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

template<class A, class B> A operator+=(A& l, const B& r) { return l = l+r; }
template<class A, class B> A operator-=(A& l, const B& r) { return l = l-r; }
template<class A, class B> A operator*=(A& l, const B& r) { return l = l*r; }
template<class A, class B> A operator/=(A& l, const B& r) { return l = l/r; }

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { 
        re(first); re(rest...); 
    }

    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { 
        pr(first); pr(rest...); 
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) { 
        pr("{",x.f,", ",x.s,"}"); 
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0; 
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
    
    void ps() { pr("\n"); } 
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { 
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

struct Line {
    int x; pi y;
    Line(int _x, pi _y) {
        x = _x; y = _y;
    }
    Line() : Line(0,{1,-1}) {}
    friend bool operator<(const Line& l, const Line& r) {
        if (l.y != r.y) return l.y < r.y;
        return l.x < r.x;
    }
    friend bool operator>(const Line& l, const Line& r) {
        if (l.y != r.y) return l.y > r.y;
        return l.x > r.x;
    }
    friend bool operator==(const Line& l, const Line& r) {
        return l.x == r.x && l.y == r.y;
    }
    friend bool operator>=(const Line& l, const Line& r) {
        return l > r || l == r;
    }
    friend void pr(const Line& l) {
        pr(mp(l.x,l.y));
    }
};

namespace treap {
    typedef struct tnode* pt;
    
    struct tnode {
        int pri; Line val; pt c[2]; // essential
        Line best;
    
        tnode (Line _val) {
            pri = rand()+(rand()<<15); val = _val; c[0] = c[1] = NULL;
            best = val;
        }
    };
    
    /*
    void tour(pt x, vi& v) {
        if (!x) return;
        prop(x);
        tour(x->c[0],v); v.pb(x->val); tour(x->c[1],v);
    }*/
    
    Line getBest(pt x) { return x?x->best:Line(); }
    
    Line bet(const Line& l, const Line& r) {
        if (l.y.s > r.y.s) return l;
        return r;
    }
    
    pt recalc(pt x) {
        x->best = bet(x->val,bet(getBest(x->c[0]),getBest(x->c[1])));
        return x;
    }
    
    pair<pt,pt> split(pt t, const Line& v) { // >= v goes to the right
        if (!t) return {t,t};
        if (t->val >= v) {
            auto p = split(t->c[0], v); t->c[0] = p.s;
            return {p.f, recalc(t)};
        } else {
            auto p = split(t->c[1], v); t->c[1] = p.f;
            return {recalc(t), p.s};
        }
    }
    
    pt merge(pt l, pt r) {
        if (!l || !r) return l ? l : r;
        pt t;
        if (l->pri > r->pri) l->c[1] = merge(l->c[1],r), t = l;
        else r->c[0] = merge(l,r->c[0]), t = r;
        return recalc(t);
    }
    
    pt ins(pt x, const Line& v) { // insert v
        auto a = split(x,v);
        return merge(a.f,merge(new tnode(v),a.s));
    }
    pt remFst(pt x) {
        if (x->c[0]) {
            x->c[0] = remFst(x->c[0]);
            return recalc(x);
        }
        return x->c[1];
    }
    pt del(pt x, const Line& v) { // delete v
        auto a = split(x,v);
        return merge(a.f,remFst(a.s));
    }
    Line queryBest(pt x, int y) {
        auto a = split(x,Line({-MOD,{y+1,-MOD}}));
        Line L = getBest(a.f);
        x = merge(a.f,a.s);
        return L;
    }
}

using namespace treap;

template<int SZ> struct Seg { // 0: store vertical lines
    pt seg[2*SZ];
    void add(const Line& L, int p, int ind = 1, int l = 0, int r = SZ-1) { // L.x, L.y.f, L.y.s;
        if (r < L.x || L.x < l) return;
        // ps("ADD",l,r,L);
        if (p == 1) seg[ind] = ins(seg[ind],L);
        else seg[ind] = del(seg[ind],L);
        if (l == r) return;
        int m = (l+r)/2;
        add(L,p,2*ind,l,m); add(L,p,2*ind+1,m+1,r);
    }
    void query(vector<Line>& v, const Line& L, int ind = 1, int l = 0, int r = SZ-1) {
        if (r < L.y.f || L.y.s < l) return;
        if (L.y.f <= l && r <= L.y.s) {
            // ps("HUH",L,ind,l,r,seg[ind] != NULL);
            while (1) {
                auto a = queryBest(seg[ind],L.x); 
                // ps("??",a,L.x,seg[ind]->val);
                if (a.y.s < L.x) break;
                v.pb(a); add(a,-1);
            }
            return;
        }
        int m = (l+r)/2;
        query(v,L,2*ind,l,m); query(v,L,2*ind+1,m+1,r);
    }
};

Seg<1<<18> stor[2];

int N;
pi S,E;
vector<pair<pi,pi>> v;
vi x, y;

void getInd(vi& a, int& b) {
    b = lb(all(a),b)-a.begin()+1;
}

void compressX() {
    sort(all(x)); x.erase(unique(all(x)),x.end());
    getInd(x,S.f);
    getInd(x,E.f);
    trav(t,v) {
        getInd(x,t.f.f);
        getInd(x,t.f.s);
    }
}
 
void compressY() {
    sort(all(y)); y.erase(unique(all(y)),y.end());
    getInd(y,S.s);
    getInd(y,E.s);
    trav(t,v) {
        getInd(y,t.s.f);
        getInd(y,t.s.s);
    }
}
 
void init() {
    setIO(); re(S,E); 
    x.pb(S.f), x.pb(E.f), y.pb(S.s), y.pb(E.s);
    re(N);
    F0R(i,N) {
        int a,b,c,d; re(a,b,c,d);
        v.pb({{a,b},{c,d}});
        x.pb(a), x.pb(b), y.pb(c), y.pb(d);
    }
    compressX();
    compressY();
}

queue<pair<pi,Line>> q;
 
bool finished(pair<pi,Line> a) {
    if (a.f.s == 0) return a.s.x == E.f && a.s.y.f <= E.s && a.s.y.s >= E.s;
    return a.s.x == E.s && a.s.y.f <= E.f && a.s.y.s >= E.f;
}

map<int,int> m;
map<pi,int> M;

void ins(pi y, int x) {
    if (y.f > y.s) return;
    while (1) {
        auto it = M.lb({y.s+1,-MOD}); if (it == M.begin()) break;
        it = prev(it); if (it->f.s < y.f) break;
        auto t = *it; M.erase(it);
        if (t.f.f < y.f) M[{t.f.f,y.f-1}] = t.s;
        if (y.s < t.f.s) M[{y.s+1,t.f.s}] = t.s;
    }
    M[y] = x;
}

int getMax(int y) {
    return prev(M.ub({y,MOD}))->s;
}

vector<Line> hseg, vseg;
vector<Line> tmp;

void genSeg() {
    m.clear(), M.clear(); tmp.clear();
    sort(all(v));
    ins({1,sz(y)},1);
    trav(t,v) {
        auto it = m.ub(t.s.f);
        while (it != m.end() && it->f < t.s.s) {
            tmp.pb(Line(it->f,{it->s,t.f.f}));
            it = next(it); 
            m.erase(prev(it));
        }
        if (!m.count(t.s.f)) m[t.s.f] = getMax(t.s.f);
        if (!m.count(t.s.s)) m[t.s.s] = getMax(t.s.s);
        // ps("??",t,sz(y),getMax(t.s.f),getMax(t.s.s));
        ins({t.s.f+1,t.s.s-1},t.f.s);
    }
    trav(t,m) tmp.pb(Line(t.f,{t.s,sz(x)}));
    // ps("HA",t.s,sz(x),t.f);
    // ps("HA",m,M); exit(0);
    
}

void swapXY() {
    trav(z,v) swap(z.f,z.s);
    swap(x,y);
}

int main() {
    init();
    v.pb({{S.f,S.f},{S.s,S.s}});
    v.pb({{E.f,E.f},{E.s,E.s}});
    genSeg();
    swap(hseg,tmp);
    // ps(hseg);
    swapXY();
    genSeg();
    swap(vseg,tmp);
    // ps(vseg);
    swapXY();
    // ps(v);
    // ps(hseg); ps(vseg); 
    trav(t,vseg) {
        stor[0].add(t,1);
        if (t.x == S.f && t.y.f <= S.s && S.s <= t.y.s) {
            q.push({{1,0},t});
            // ps("AA",t);
        }
    }
    trav(t,hseg) {
        stor[1].add(t,1);
        if (t.x == S.s && t.y.f <= S.f && S.f <= t.y.s) {
            q.push({{1,1},t});
            // ps("BB",t);
        }
    }
    // exit(0);
    // ps(S,E,v);
    
    while (sz(q)) {
        auto a = q.front(); q.pop();
        // ps("WUT",a);
        if (finished(a)) {
            ps(a.f.f); 
            exit(0);
        }
        vector<Line> V; stor[a.f.s^1].query(V,a.s);
        // ps("??",a,V);
        trav(t,V) q.push({{a.f.f+1,a.f.s^1},t});
    }
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/

Compilation message (stderr)

golf.cpp: In function 'void io::setIn(std::__cxx11::string)':
golf.cpp:117:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setIn(string s) { freopen(s.c_str(),"r",stdin); }
                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
golf.cpp: In function 'void io::setOut(std::__cxx11::string)':
golf.cpp:118:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setOut(string s) { freopen(s.c_str(),"w",stdout); }
                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...