답안 #557247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557247 2022-05-05T02:57:25 Z balbit Golf (JOI17_golf) C++14
100 / 100
2634 ms 643420 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<ll, ll>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 1e5+5;



struct seg{
    int l,r,y,id;
};

int IT = 0; // each segment gets assigned an id
int dst[maxn * 20 * 4 * 4];
bool dir[maxn * 20 * 4 * 4];

bool NOWHOR = 0;

struct rect{
    int x1, x2, y1, y2;
};

int Sx, Sy, Tx, Ty;

vector<seg> who;


vector<seg> get(vector<rect> &v) {
    set<int> have; // current y positions
    // scan from left to right along x axis

    struct Ev{
        int x,y;
        int id; // id 1: remove, id 2: ask, id 3: add
        bool operator < (Ev & o) {
            return x != o.x? x < o.x : id < o.id;
        }
    };

    vector<Ev> ev;

    ev.pb({Sx, Sy, 2});
    ev.pb({Tx, Ty, 2});

    for (rect r : v) {
        ev.pb({r.x1, r.y1, 3});
        ev.pb({r.x1, r.y2, 3});

        ev.pb({r.x1, r.y1, 2});
        ev.pb({r.x2, r.y1, 2});

        ev.pb({r.x2, r.y1, 1});
        ev.pb({r.x2, r.y2, 1});
    }

    sort(ALL(ev));

    vector<seg> ret;
    set<pair<pii, int> > seghave;

    for (Ev & e : ev) {
        if (e.id == 1) {
            have.erase(e.y);
        }
        if (e.id == 3) {
            have.insert(e.y);
        }
        if (e.id == 2) {
            auto it = have.lower_bound(e.y);
            int ybig = it==have.end()?iinf : *it;
            int ysmall = it==have.begin()? 0 : *prev(it);
            if (seghave.count({{ysmall, ybig}, e.x})) {
                continue;
            }
            seghave.insert({{ysmall, ybig}, e.x});
            dir[IT] = NOWHOR;
            ret.pb({ysmall, ybig, e.x, IT++});
        }
    }

    return ret;
}



queue<int> qq;

struct smolseg{
    vector<pii> s;
    vector<int> id;

    void build(int o, int l, int r) {
        bug(o,l,r);
        if (l > r) return;
        if (l == r) {
            id[o] = s[l].s;
            return;
        }
        dir[IT] = NOWHOR;
        id[o] = IT++;
        int mid = (l+r)/2;
        build(o*2,l,mid); build(o*2+1, mid+1,r);
    }

    void build(){
        sort(ALL(s));
        id.resize(SZ(s) * 4);
        build(1,0,SZ(s)-1);
    }

    void runupd(int L, int R, int d, int o, int l, int r) {
        if (l > r) return;
        if (s[l].f > R || s[r].f < L) return;
        int mid = (l+r)/2;
        if (s[l].f >= L && s[r].f <= R) {
            if (dst[id[o]] <= d) {
                return;
            }
            dst[id[o]] = d;
            if (l == r) {
                qq.push(id[o]);
                return;
            }
        }
        runupd(L,R,d,o*2,l,mid);
        runupd(L,R,d,o*2+1,mid+1,r);
    }

    void runupd(int L, int R, int d) {
        runupd(L,R,d,1,0,SZ(s)-1);

    }

};

struct STree{
    int MX;
    smolseg s[maxn*4*3]; // y position, index

    void put(int L, int R, pii v, int o=1, int l=0, int r=-1) {
        if (r == -1) r = MX-1;
        if (l > R || r < L) return;
        if (l >= L && r <= R) {
            s[o].s.pb(v); return;
        }
        int mid = (l+r)/2;
        put(L,R,v,o*2,l,mid);
        put(L,R,v,o*2+1,mid+1,r);
    }

    void build(int o, int l, int r){
        s[o].build();
        if (l!=r) {
            int mid = (l+r)/2;
            build(o*2, l,mid);
            build(o*2+1, mid+1,r);
        }
    }

    void run(seg S, int o=1, int l=0, int r=-1) {
        if (r == -1) r = MX-1;
        if (r < S.y || l > S.y) return;
        s[o].runupd(S.l, S.r, dst[S.id]+1);
        if (l == r) return;
        int mid = (l+r)/2;
        run(S,o*2,l,mid); run(S,o*2+1,mid+1,r);
    }
}AA[2]; // this is the big, scary segment tree you were waiting for!!!!!

signed main(){
    IOS();


    cin>>Sx>>Sy>>Tx>>Ty;

    int n; cin>>n;
    vector<rect> v;
    REP(i,n) {
        int x1,x2,y1,y2;
        cin>>x1>>x2>>y1>>y2;
        v.pb({x1,x2,y1,y2});
    }
    NOWHOR = 0;
    vector<seg> gver = get(v); // gver returns vertical lines
    for (auto &r : v) {
        swap(r.x1, r.y1);
        swap(r.x2, r.y2);
    }
    swap(Sx, Sy); swap(Tx, Ty);

    NOWHOR = 1;
    vector<seg> ghor = get(v);

    for (auto &r : v) {
        swap(r.x1, r.y1);
        swap(r.x2, r.y2);
    }
    swap(Sx, Sy); swap(Tx, Ty);

    for (auto s : gver) {
        bug(s.y, s.l, s.r);
    }


    // step 2: compress coordinates
    vector<int> posx, posy;
    for (auto s : gver) {
        posy.pb(s.l);
        posy.pb(s.r);
        posx.pb(s.y);
    }
    for (auto s : ghor) {
        posx.pb(s.l);
        posx.pb(s.r);
        posy.pb(s.y);
    }

    SORT_UNIQUE(posx); SORT_UNIQUE(posy);

    who.resize(IT);

    for (auto &s : gver) {
        s.l = lower_bound(ALL(posy), s.l) - posy.begin();
        s.r = lower_bound(ALL(posy), s.r) - posy.begin();
        s.y = lower_bound(ALL(posx), s.y) - posx.begin();
        who[s.id] = s;
    }
    for (auto &s : ghor) {
        s.l = lower_bound(ALL(posx), s.l) - posx.begin();
        s.r = lower_bound(ALL(posx), s.r) - posx.begin();
        s.y = lower_bound(ALL(posy), s.y) - posy.begin();
        who[s.id] = s;
    }

    Sx = lower_bound(ALL(posx), Sx) - posx.begin();
    Tx = lower_bound(ALL(posx), Tx) - posx.begin();
    Sy = lower_bound(ALL(posy), Sy) - posy.begin();
    Ty = lower_bound(ALL(posy), Ty) - posy.begin();

    // step 3: build the segtree layers

    NOWHOR = 0;

    AA[0].MX = SZ(posy);
    for (auto &s : gver) { // AA[0] stores the vertical ones
        AA[0].put(s.l, s.r, make_pair(s.y, s.id));
    }
    AA[0].build(1,0,SZ(posy)-1);

    NOWHOR = 1;

    AA[1].MX = SZ(posx);
    for (auto &s : ghor) { // AA[1] stores the horizontal ones
        AA[1].put(s.l, s.r, make_pair(s.y, s.id));
    }
    AA[1].build(1,0,SZ(posx)-1);

    // step 4: run the bfs

    memset(dst, 0x3f, sizeof dst);

    for (auto & s : gver) {
        if (s.y == Sx && (s.l <= Sy && Sy <= s.r)) {
            bug(s.id);
            bug(s.y,s.l,s.r,dst[s.id]);
            qq.push(s.id);
            dst[s.id] = 0;
        }
    }

    for (auto & s : ghor) {
        if (s.y == Sy && (s.l <= Sx && Sx <= s.r)) {
            bug(s.id);
            bug("p2", s.y,s.l,s.r,dst[s.id]);
            qq.push(s.id);
            dst[s.id] = 0;
        }
    }

    while (!qq.empty()) {
        int v = qq.front(); qq.pop();
        bug(v, dst[v]);
        bug(dir[v], who[v].l, who[v].r);
        bug(who[v].y);
        AA[dir[v]^1].run(who[v]);
    }

    int re = 0x3f3f3f3f;

    for (auto & s : gver) {
        if (s.y == Tx && (s.l <= Ty && Ty <= s.r)) {
            bug(s.id);
            bug(s.y,s.l,s.r,dst[s.id]);
            MN(re, dst[s.id]);
        }
    }

    for (auto & s : ghor) {
        if (s.y == Ty && (s.l <= Tx && Tx <= s.r)) {
            bug(s.id);
            bug("p2", s.y,s.l,s.r,dst[s.id]);
            MN(re, dst[s.id]);
        }
    }
    cout<<re+1<<endl;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:9:11: warning: variable 'second' set but not used [-Wunused-but-set-variable]
    9 | #define s second
      |           ^~~~~~
golf.cpp:248:15: note: in expansion of macro 's'
  248 |     for (auto s : gver) {
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 363444 KB Output is correct
2 Correct 159 ms 363536 KB Output is correct
3 Correct 166 ms 363456 KB Output is correct
4 Correct 147 ms 363660 KB Output is correct
5 Correct 158 ms 364896 KB Output is correct
6 Correct 152 ms 364920 KB Output is correct
7 Correct 154 ms 364708 KB Output is correct
8 Correct 154 ms 364872 KB Output is correct
9 Correct 160 ms 364800 KB Output is correct
10 Correct 163 ms 364908 KB Output is correct
11 Correct 157 ms 364964 KB Output is correct
12 Correct 157 ms 364840 KB Output is correct
13 Correct 150 ms 364848 KB Output is correct
14 Correct 156 ms 364960 KB Output is correct
15 Correct 150 ms 363972 KB Output is correct
16 Correct 153 ms 364452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 363444 KB Output is correct
2 Correct 159 ms 363536 KB Output is correct
3 Correct 166 ms 363456 KB Output is correct
4 Correct 147 ms 363660 KB Output is correct
5 Correct 158 ms 364896 KB Output is correct
6 Correct 152 ms 364920 KB Output is correct
7 Correct 154 ms 364708 KB Output is correct
8 Correct 154 ms 364872 KB Output is correct
9 Correct 160 ms 364800 KB Output is correct
10 Correct 163 ms 364908 KB Output is correct
11 Correct 157 ms 364964 KB Output is correct
12 Correct 157 ms 364840 KB Output is correct
13 Correct 150 ms 364848 KB Output is correct
14 Correct 156 ms 364960 KB Output is correct
15 Correct 150 ms 363972 KB Output is correct
16 Correct 153 ms 364452 KB Output is correct
17 Correct 156 ms 365448 KB Output is correct
18 Correct 156 ms 365448 KB Output is correct
19 Correct 155 ms 365472 KB Output is correct
20 Correct 158 ms 365364 KB Output is correct
21 Correct 157 ms 365424 KB Output is correct
22 Correct 158 ms 365452 KB Output is correct
23 Correct 157 ms 365400 KB Output is correct
24 Correct 157 ms 365464 KB Output is correct
25 Correct 154 ms 365460 KB Output is correct
26 Correct 159 ms 365464 KB Output is correct
27 Correct 149 ms 363992 KB Output is correct
28 Correct 149 ms 364560 KB Output is correct
29 Correct 154 ms 364580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 363444 KB Output is correct
2 Correct 159 ms 363536 KB Output is correct
3 Correct 166 ms 363456 KB Output is correct
4 Correct 147 ms 363660 KB Output is correct
5 Correct 158 ms 364896 KB Output is correct
6 Correct 152 ms 364920 KB Output is correct
7 Correct 154 ms 364708 KB Output is correct
8 Correct 154 ms 364872 KB Output is correct
9 Correct 160 ms 364800 KB Output is correct
10 Correct 163 ms 364908 KB Output is correct
11 Correct 157 ms 364964 KB Output is correct
12 Correct 157 ms 364840 KB Output is correct
13 Correct 150 ms 364848 KB Output is correct
14 Correct 156 ms 364960 KB Output is correct
15 Correct 150 ms 363972 KB Output is correct
16 Correct 153 ms 364452 KB Output is correct
17 Correct 156 ms 365448 KB Output is correct
18 Correct 156 ms 365448 KB Output is correct
19 Correct 155 ms 365472 KB Output is correct
20 Correct 158 ms 365364 KB Output is correct
21 Correct 157 ms 365424 KB Output is correct
22 Correct 158 ms 365452 KB Output is correct
23 Correct 157 ms 365400 KB Output is correct
24 Correct 157 ms 365464 KB Output is correct
25 Correct 154 ms 365460 KB Output is correct
26 Correct 159 ms 365464 KB Output is correct
27 Correct 149 ms 363992 KB Output is correct
28 Correct 149 ms 364560 KB Output is correct
29 Correct 154 ms 364580 KB Output is correct
30 Correct 2497 ms 636476 KB Output is correct
31 Correct 2450 ms 637992 KB Output is correct
32 Correct 2507 ms 635212 KB Output is correct
33 Correct 2520 ms 637848 KB Output is correct
34 Correct 2598 ms 643420 KB Output is correct
35 Correct 2530 ms 642228 KB Output is correct
36 Correct 2530 ms 639232 KB Output is correct
37 Correct 2634 ms 636356 KB Output is correct
38 Correct 2509 ms 642368 KB Output is correct
39 Correct 2557 ms 637224 KB Output is correct
40 Correct 672 ms 446476 KB Output is correct
41 Correct 683 ms 446348 KB Output is correct
42 Correct 668 ms 443356 KB Output is correct
43 Correct 689 ms 443352 KB Output is correct
44 Correct 699 ms 443868 KB Output is correct
45 Correct 690 ms 443896 KB Output is correct
46 Correct 678 ms 443892 KB Output is correct
47 Correct 684 ms 443788 KB Output is correct
48 Correct 675 ms 443916 KB Output is correct
49 Correct 677 ms 444016 KB Output is correct
50 Correct 153 ms 364476 KB Output is correct
51 Correct 157 ms 364488 KB Output is correct
52 Correct 152 ms 364588 KB Output is correct