Submission #590644

# Submission time Handle Problem Language Result Execution time Memory
590644 2022-07-06T08:00:42 Z 이동현(#8415) Golf (JOI17_golf) C++17
30 / 100
1674 ms 437456 KB
#include <bits/stdc++.h>

using namespace std;

int wayx[2004][2004], wayy[2004][2004];
int wx[4] = {-1, 0, 1, 0}, wy[4] = {0, 1, 0, -1};
int que[2004 * 2004][2];
int dist[2004][2004];
int f, r;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int sx, sy, ex, ey; cin >> sx >> sy >> ex >> ey;
    int n; cin >> n;
    vector<vector<int>> a(n, vector<int>(4));
    vector<int> xsrt, ysrt;
    for(int i = 0; i < n; ++i){
        cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];
        xsrt.push_back(a[i][0]), xsrt.push_back(a[i][1]);
        ysrt.push_back(a[i][2]), ysrt.push_back(a[i][3]);
    }
    xsrt.push_back(sx), xsrt.push_back(ex);
    ysrt.push_back(sy), ysrt.push_back(ey);
    xsrt.push_back(0), ysrt.push_back(0);
    sort(xsrt.begin(), xsrt.end()), xsrt.erase(unique(xsrt.begin(), xsrt.end()), xsrt.end());
    sort(ysrt.begin(), ysrt.end()), ysrt.erase(unique(ysrt.begin(), ysrt.end()), ysrt.end());
    auto getx = [&](int x){return lower_bound(xsrt.begin(), xsrt.end(), x) - xsrt.begin();};
    auto gety = [&](int y){return lower_bound(ysrt.begin(), ysrt.end(), y) - ysrt.begin();};
    sx = getx(sx), ex = getx(ex), sy = gety(sy), ey = gety(ey);
    //cout << sx << ' ' << sy << ' ' << ex << ' ' << ey << endl;
    for(int i = 0; i < n; ++i){
        a[i][0] = getx(a[i][0]), a[i][1] = getx(a[i][1]);
        a[i][2] = gety(a[i][2]), a[i][3] = gety(a[i][3]);
        //cout << a[i][0] << ' ' << a[i][1] << ' ' << a[i][2] << ' ' << a[i][3] << endl;
    }
    for(int i = 0; i < n; ++i){
        for(int j = a[i][0] + 1; j < a[i][1]; ++j){
            wayx[j][a[i][2] + 1] = wayx[j][a[i][3]] = 1;
        }
        for(int j = a[i][2] + 1; j < a[i][3]; ++j){
            wayy[a[i][0] + 1][j] = wayy[a[i][1]][j] = 1;
        }
    }
    vector<set<int>> xset((int)xsrt.size()), yset((int)ysrt.size());
    for(int i = 0; i < (int)xsrt.size(); ++i){
        for(int j = 0; j < (int)ysrt.size(); ++j){
            xset[i].insert(j);
            if(j) wayx[i][j] += wayx[i][j - 1];
        }
    }
    for(int j = 0; j < (int)ysrt.size(); ++j){
        for(int i = 0; i < (int)xsrt.size(); ++i){
            yset[j].insert(i);
            if(i) wayy[i][j] += wayy[i - 1][j];
        }
    }
    auto cantx = [&](int x, int y1, int y2)->int{
        if(y1 > y2) swap(y1, y2);
        return wayx[x][y2] - wayx[x][y1];
    };
    auto canty = [&](int y, int x1, int x2)->int{
        if(x1 > x2) swap(x1, x2);
        return wayy[x2][y] - wayy[x1][y];
    };
    que[r][0] = sx, que[r++][1] = sy, dist[sx][sy] = 1;
    while(f < r){
        int x = que[f][0], y = que[f][1];
        int far = dist[x][y];
        if(x == ex && y == ey){
            cout << far - 1 << '\n';
            return 0;
        }
        auto p = xset[x].lower_bound(y);
        while(p != xset[x].end()){
            if(cantx(x, y, *p)){
                break;
            }
            if(!dist[x][*p]){
                dist[x][*p] = far + 1;
                que[r][0] = x, que[r++][1] = *p;
            }
            xset[x].erase(p++);
        }
        while(true){
            p = xset[x].lower_bound(y);
            if(p == xset[x].begin()) break;
            --p;
            if(cantx(x, y, *p)){
                break;
            }
            if(!dist[x][*p]){
                dist[x][*p] = far + 1;
                que[r][0] = x, que[r++][1] = *p;
            }
            xset[x].erase(p++);
        }
        p = yset[y].lower_bound(x);
        while(p != yset[y].end()){
            if(canty(y, x, *p)){
                break;
            }
            if(!dist[*p][y]){
                dist[*p][y] = far + 1;
                que[r][0] = *p, que[r++][1] = y;
            }
            yset[y].erase(p++);
        }
        while(true){
            p = yset[y].lower_bound(x);
            if(p == yset[y].begin()) break;
            --p;
            if(canty(y, x, *p)){
                break;
            }
            if(!dist[*p][y]){
                dist[*p][y] = far + 1;
                que[r][0] = *p, que[r++][1] = y;
            }
            yset[y].erase(p++);
        }
        ++f;
    }
    assert(0);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 6052 KB Output is correct
5 Correct 260 ms 77960 KB Output is correct
6 Correct 233 ms 81852 KB Output is correct
7 Correct 242 ms 75728 KB Output is correct
8 Correct 268 ms 84812 KB Output is correct
9 Correct 273 ms 79764 KB Output is correct
10 Correct 223 ms 82292 KB Output is correct
11 Correct 239 ms 86688 KB Output is correct
12 Correct 217 ms 78580 KB Output is correct
13 Correct 257 ms 82592 KB Output is correct
14 Correct 272 ms 84180 KB Output is correct
15 Correct 69 ms 23676 KB Output is correct
16 Correct 429 ms 83416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 6052 KB Output is correct
5 Correct 260 ms 77960 KB Output is correct
6 Correct 233 ms 81852 KB Output is correct
7 Correct 242 ms 75728 KB Output is correct
8 Correct 268 ms 84812 KB Output is correct
9 Correct 273 ms 79764 KB Output is correct
10 Correct 223 ms 82292 KB Output is correct
11 Correct 239 ms 86688 KB Output is correct
12 Correct 217 ms 78580 KB Output is correct
13 Correct 257 ms 82592 KB Output is correct
14 Correct 272 ms 84180 KB Output is correct
15 Correct 69 ms 23676 KB Output is correct
16 Correct 429 ms 83416 KB Output is correct
17 Correct 1484 ms 420512 KB Output is correct
18 Correct 1469 ms 419392 KB Output is correct
19 Correct 1370 ms 423764 KB Output is correct
20 Correct 1519 ms 428248 KB Output is correct
21 Correct 1630 ms 437456 KB Output is correct
22 Correct 1338 ms 432820 KB Output is correct
23 Correct 1308 ms 429580 KB Output is correct
24 Correct 1674 ms 437272 KB Output is correct
25 Correct 1220 ms 426140 KB Output is correct
26 Correct 1512 ms 435308 KB Output is correct
27 Correct 113 ms 32716 KB Output is correct
28 Correct 506 ms 99592 KB Output is correct
29 Correct 589 ms 102196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 6052 KB Output is correct
5 Correct 260 ms 77960 KB Output is correct
6 Correct 233 ms 81852 KB Output is correct
7 Correct 242 ms 75728 KB Output is correct
8 Correct 268 ms 84812 KB Output is correct
9 Correct 273 ms 79764 KB Output is correct
10 Correct 223 ms 82292 KB Output is correct
11 Correct 239 ms 86688 KB Output is correct
12 Correct 217 ms 78580 KB Output is correct
13 Correct 257 ms 82592 KB Output is correct
14 Correct 272 ms 84180 KB Output is correct
15 Correct 69 ms 23676 KB Output is correct
16 Correct 429 ms 83416 KB Output is correct
17 Correct 1484 ms 420512 KB Output is correct
18 Correct 1469 ms 419392 KB Output is correct
19 Correct 1370 ms 423764 KB Output is correct
20 Correct 1519 ms 428248 KB Output is correct
21 Correct 1630 ms 437456 KB Output is correct
22 Correct 1338 ms 432820 KB Output is correct
23 Correct 1308 ms 429580 KB Output is correct
24 Correct 1674 ms 437272 KB Output is correct
25 Correct 1220 ms 426140 KB Output is correct
26 Correct 1512 ms 435308 KB Output is correct
27 Correct 113 ms 32716 KB Output is correct
28 Correct 506 ms 99592 KB Output is correct
29 Correct 589 ms 102196 KB Output is correct
30 Runtime error 249 ms 77840 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -