QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#179017#7224. The Imaginary Girlfrienducup-team004TL 1ms3636kbC++208.9kb2023-09-14 16:40:232023-09-14 16:40:23

Judging History

你现在查看的是最新测评结果

  • [2023-09-14 16:40:23]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3636kb
  • [2023-09-14 16:40:23]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<int> x1(n), y1(n), x2(n), y2(n);
    std::vector<int> vx, vy;
    vx.reserve(2 * n + 2);
    vy.reserve(2 * n + 2);
    for (int i = 0; i < n; i++) {
        std::cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
        vx.push_back(x1[i]);
        vy.push_back(y1[i]);
        vx.push_back(x2[i]);
        vy.push_back(y2[i]);
    }
    
    int sx, sy, tx, ty;
    std::cin >> sx >> sy >> tx >> ty;
    
    vx.push_back(sx);
    vy.push_back(sy);
    vx.push_back(tx);
    vy.push_back(ty);
    
    if (sx == tx && sy == ty) {
        std::cout << 0 << "\n";
        return 0;
    }
    
    std::sort(vx.begin(), vx.end());
    std::sort(vy.begin(), vy.end());
    vx.erase(std::unique(vx.begin(), vx.end()), vx.end());
    vy.erase(std::unique(vy.begin(), vy.end()), vy.end());
    
    const int nx = vx.size();
    const int ny = vy.size();
    for (int i = 0; i < n; i++) {
        x1[i] = std::lower_bound(vx.begin(), vx.end(), x1[i]) - vx.begin();
        y1[i] = std::lower_bound(vy.begin(), vy.end(), y1[i]) - vy.begin();
        x2[i] = std::lower_bound(vx.begin(), vx.end(), x2[i]) - vx.begin();
        y2[i] = std::lower_bound(vy.begin(), vy.end(), y2[i]) - vy.begin();
    }
    
    sx = std::lower_bound(vx.begin(), vx.end(), sx) - vx.begin();
    sy = std::lower_bound(vy.begin(), vy.end(), sy) - vy.begin();
    tx = std::lower_bound(vx.begin(), vx.end(), tx) - vx.begin();
    ty = std::lower_bound(vy.begin(), vy.end(), ty) - vy.begin();
    
    std::map<std::array<int, 2>, int> id;
    std::vector<std::array<int, 2>> ver;
    std::vector<std::vector<int>> px(nx), py(ny);
    std::vector<std::vector<std::array<int, 2>>> segx(nx), segy(ny);
    for (int i = 0; i < n; i++) {
        if (x1[i] == x2[i]) {
            if (y1[i] > y2[i]) {
                std::swap(y1[i], y2[i]);
            }
            segx[x1[i]].push_back({y1[i], y2[i]});
        } else {
            if (x1[i] > x2[i]) {
                std::swap(x1[i], x2[i]);
            }
            segy[y1[i]].push_back({x1[i], x2[i]});
        }
    }
    
    for (int x = 0; x < nx; x++) {
        std::sort(segx[x].begin(), segx[x].end());
    }
    for (int y = 0; y < ny; y++) {
        std::sort(segy[y].begin(), segy[y].end());
    }
    
    auto find = [&](auto &a, int x) {
        auto it = std::lower_bound(a.begin(), a.end(), std::array{x + 1, 0});
        if (it == a.begin()) {
            return false;
        }
        it--;
        return (*it)[1] >= x;
    };
    
    auto add = [&](int x, int y) {
        if (id.count({x, y})) {
            return;
        }
        if (!find(segx[x], y) && !find(segy[y], x)) {
            return;
        }
        int u = id[{x, y}] = ver.size();
        ver.push_back({x, y});
        px[x].push_back(u);
        py[y].push_back(u);
    };
    
    std::vector<int> dx{0, nx - 1};
    constexpr int B = 200;
    for (int i = B; i < nx; i += B) {
        dx.push_back(i);
    }
    dx.push_back(sx);
    dx.push_back(tx);
    std::sort(dx.begin(), dx.end());
    dx.erase(std::unique(dx.begin(), dx.end()), dx.end());
    
    for (auto x : dx) {
        for (int y = 0; y < ny; y++) {
            add(x, y);
        }
    }
    
    for (int i = 0; i + 1 < dx.size(); i++) {
        int xl = dx[i], xr = dx[i + 1];
        
        std::vector<int> ys;
        auto work = [&]() {
            if (ys.empty()) {
                return;
            }
            int yl = ys[0], yr = ys.back();
            for (auto y : {yl, yr}) {
                for (int x = xl; x <= xr; x++) {
                    add(x, y);
                }
            }
            std::map<int, int> px;
            for (auto y : ys) {
                px[y] = xl;
            }
            for (int x = xl; x <= xr; x++) {
                auto it = std::lower_bound(segx[x].begin(), segx[x].end(), std::array{yl + 1, 0});
                if (it != segx[x].begin()) {
                    it--;
                }
                while (it != segx[x].end() && (*it)[0] <= yr) {
                    auto [l, r] = *it;
                    auto itl = px.lower_bound(l + 1);
                    auto itr = px.lower_bound(r);
                    if (itl != itr) {
                        while (itl != itr) {
                            add(x, itl->first);
                            itl = px.erase(itl);
                        }
                        itl = px.lower_bound(l);
                        while (itl != px.end() && itl->first <= r) {
                            add(x, itl->first);
                            itl->second = x;
                            itl++;
                        }
                    }
                }
            }
            px.clear();
            for (auto y : ys) {
                px[y] = xr;
            }
            for (int x = xr; x >= xl; x--) {
                auto it = std::lower_bound(segx[x].begin(), segx[x].end(), std::array{yl + 1, 0});
                if (it != segx[x].begin()) {
                    it--;
                }
                while (it != segx[x].end() && (*it)[0] <= yr) {
                    auto [l, r] = *it;
                    auto itl = px.lower_bound(l + 1);
                    auto itr = px.lower_bound(r);
                    if (itl != itr) {
                        while (itl != itr) {
                            add(x, itl->first);
                            itl = px.erase(itl);
                        }
                        itl = px.lower_bound(l);
                        while (itl != px.end() && itl->first <= r) {
                            add(x, itl->first);
                            itl->second = x;
                            itl++;
                        }
                    }
                }
            }
            ys.clear();
        };
        for (int y = 0; y < ny; y++) {
            for (auto [l, r] : segy[y]) {
                if (r < xl || l > xr) {
                    continue;
                }
                if (l <= xl && r >= xr) {
                    ys.push_back(y);
                } else {
                    work();
                    for (int x = xl; x <= xr; x++) {
                        add(x, y);
                    }
                }
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        add(x1[i], y1[i]);
        add(x2[i], y2[i]);
    }
    add(sx, sy);
    add(tx, ty);
    
    const int V = ver.size();
    std::vector<std::vector<std::pair<int, int>>> adj(V);
    std::vector<i64> dis(V, -1);
    
    auto addEdge = [&](int u, int v) {
        int d = std::abs(vx[ver[u][0]] - vx[ver[v][0]]) + std::abs(vy[ver[u][1]] - vy[ver[v][1]]);
        adj[u].emplace_back(v, d);
        adj[v].emplace_back(u, d);
    };
    
    for (int x = 0; x < nx; x++) {
        int m = px[x].size();
        std::sort(px[x].begin(), px[x].end(),
            [&](int i, int j) {
                return ver[i][1] < ver[j][1];
            });
        for (int i = 0, j = 0; auto [l, r] : segx[x]) {
            while (i < m && ver[px[x][i]][1] < l) {
                i++;
            }
            while (j < m && ver[px[x][j]][1] <= r) {
                j++;
            }
            for (int k = i; k < j - 1; k++) {
                int u = px[x][k];
                int v = px[x][k + 1];
                addEdge(u, v);
            }
        }
    }
    
    for (int y = 0; y < ny; y++) {
        int m = py[y].size();
        std::sort(py[y].begin(), py[y].end(),
            [&](int i, int j) {
                return ver[i][0] < ver[j][0];
            });
        for (int i = 0, j = 0; auto [l, r] : segy[y]) {
            while (i < m && ver[py[y][i]][0] < l) {
                i++;
            }
            while (j < m && ver[py[y][j]][0] <= r) {
                j++;
            }
            for (int k = i; k < j - 1; k++) {
                int u = py[y][k];
                int v = py[y][k + 1];
                addEdge(u, v);
            }
        }
    }
    
    if (!id.count({sx, sy})) {
        std::cout << -1 << "\n";
        return 0;
    }
    if (!id.count({tx, ty})) {
        std::cout << -1 << "\n";
        return 0;
    }
    int S = id[{sx, sy}];
    int T = id[{tx, ty}];
    std::priority_queue<std::pair<i64, int>, std::vector<std::pair<i64, int>>, std::greater<>> q;
    q.emplace(0, S);
    while (!q.empty()) {
        auto [d, x] = q.top();
        q.pop();
        
        if (dis[x] != -1) {
            continue;
        }
        dis[x] = d;
        for (auto [y, w] : adj[x]) {
            q.emplace(d + w, y);
        }
    }
    
    std::cout << dis[T] << "\n";
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3628kb

input:

8
0 0 0 2
0 2 2 2
2 2 2 0
2 0 0 0
-1 1 1 1
1 1 1 3
1 3 -1 3
-1 1 -1 3
2 0 -1 3

output:

6

result:

ok answer is '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

10
0 0 0 3
1 0 1 3
2 0 2 3
3 0 3 3
0 0 3 0
-1 1 4 1
-1 1 -1 10000
4 1 4 10000
-1000 10000 1000 10000
0 10000 0 5000
2 3 0 5000

output:

15005

result:

ok answer is '15005'

Test #3:

score: -100
Time Limit Exceeded

input:

19
-100000 0 100000 0
-100000 0 -100000 100000
100000 0 100000 100000
-100000 100000 100000 100000
10000 0 10000 10
-10000 0 -10000 10
10000 10 -10000 10
-10000 100000 -10000 99999
-10000 99999 -10001 99999
-10001 99999 -10001 99998
-10001 99998 -10002 99998
-10002 99998 -10002 99997
-10002 99997 -1...

output:


result: