제출 #938751

#제출 시각아이디문제언어결과실행 시간메모리
938751PanndaSightseeing in Kyoto (JOI22_kyoto)C++17
100 / 100
23 ms10452 KiB
#include <bits/stdc++.h>
using namespace std;

typedef complex<long long> point;
long long cross(point a, point b) {
    return (conj(a) * b).imag();
}

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

    int n, m;
    cin >> n >> m;

    vector<point> a(n), b(m);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a[i] = point(i, x);
    }
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        b[i] = point(i, x);
    }

    auto f = [](vector<point> a, bool is_a) -> vector<array<int, 4>> {
        vector<point> hull;
        for (point p : a) {
            while (hull.size() >= 2 && cross(p - hull.end()[-2], p - hull.end()[-1]) <= 0) hull.pop_back();
            hull.push_back(p);
        }
        vector<array<int, 4>> res;
        point last(-1, -1);
        for (point p : hull) {
            if (last != point(-1, -1)) {
                int length = p.real() - last.real();
                int delta = p.imag() - last.imag();
                int di = length, dj = 0;
                if (!is_a) swap(di, dj);
                res.push_back({length, delta, di, dj});
            }
            last = p;
        }
        return res;
    };

    vector<array<int, 4>> fa = f(a, true);
    vector<array<int, 4>> fb = f(b, false);
    vector<array<int, 4>> fab(fa.size() + fb.size());
    merge(fa.begin(), fa.end(), fb.begin(), fb.end(), fab.begin(), [&](array<int, 4> x, array<int, 4> y) {
        return 1LL * x[1] * y[0] < 1LL * y[1] * x[0];
    });
    int i = 0, j = 0;
    long long answer = 0;
    for (auto [length, delta, di, dj] : fab) {
        answer += 1LL * di * b[j].imag() + 1LL * dj * a[i].imag();
        i += di;
        j += dj;
    }
    cout << answer << '\n';

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...