답안 #446707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446707 2021-07-23T07:04:34 Z baluteshih Cultivation (JOI17_cultivation) C++14
100 / 100
254 ms 928 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int INF = 2e9;
pii arr[305];

int umx, dmx;
vector<pii> smx; // [l, INF)

int lmi_a = 0;
vector<pii> lmi_u, lmi_d; // [0, r)
vector<pair<pii, int>> lmi_m; // [l, r)

int rmi_a = 0;
vector<pii> rmi_u, rmi_d; // [0, r)
vector<pair<pii, int>> rmi_m; // [l, r)

int smi_a = 0;
vector<pii> smi_u, smi_d; // [0, r)
vector<pair<pii, int>> smi_m; // [l, r)

vector<int> L, M, R;
vector<int> val;

void modify(vector<int> &seg, int ql, int qr, int l, int r, int rt, int v) {
    if (ql <= l && qr >= r)
        return seg[rt] = max(seg[rt], v), void();
    int mid = (l + r) >> 1;
    if (ql <= mid)
        modify(seg, ql, qr, l, mid, rt << 1, v);
    if (qr > mid)
        modify(seg, ql, qr, mid + 1, r, rt << 1 | 1, v);
}

void modify(vector<int> &seg, int ql, int qr, int v) { //[ql, qr)
    if (ql < qr)
        modify(seg, ql, qr - 1, 0, SZ(val) - 1, 1, v);
}

void dfs(vector<int> &seg, int l, int r, int rt, int v, vector<int> &res) {
    v = max(v, seg[rt]);
    if (l == r)
        return res[l] = v, void();
    int mid = (l + r) >> 1;
    dfs(seg, l, mid, rt << 1, v, res);
    dfs(seg, mid + 1, r, rt << 1 | 1, v, res);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int r, c, n, ans;
    cin >> r >> c >> n, ans = r + c - 2;
    for (int i = 1; i <= n; ++i)
        cin >> arr[i].X >> arr[i].Y;
    sort(arr + 1, arr + n + 1, [](pii a, pii b){ return a.Y < b.Y; });
    lmi_a = arr[1].Y - 1;
    rmi_a = c - arr[n].Y;
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && arr[i].Y == arr[j].Y)
            ++j;
        if (j <= n)
            smi_a = max(smi_a, arr[j].Y - arr[i].Y - 1);
    }
    smi_a = max(smi_a, lmi_a + rmi_a);
    sort(arr + 1, arr + n + 1);
    // U
    for (int i = 1; i <= n; ++i) {
        int lft = -1, rgt = INF;
        for (int j = 1; arr[j].X < arr[i].X; ++j) {
            if (arr[j].Y >= arr[i].Y)
                rgt = min(rgt, arr[j].Y);
            if (arr[j].Y <= arr[i].Y)
                lft = max(lft, arr[j].Y);
        }
        if (lft == -1 && rgt == INF)
            umx = max(umx, arr[i].X - 1);
        else if (lft == -1)
            lmi_u.pb(pii(rgt - 1, arr[i].X - 1));
        else if (rgt == INF)
            rmi_u.pb(pii(c - lft, arr[i].X - 1));
        else if (rgt - lft - 1 > 0)
            smi_u.pb(pii(rgt - lft - 1, arr[i].X - 1));
    }
            
    // M
    for (int i = 1; i <= n; ++i)
        for (int j = 1; arr[j].X < arr[i].X; ++j) {
            int lft = -1, rgt = INF;
            for (int k = j + 1; k < i; ++k) {
                if (arr[k].X == arr[j].X || arr[k].X == arr[i].X)
                    continue;
                if (arr[k].Y >= arr[i].Y || arr[k].Y >= arr[j].Y)
                    rgt = min(rgt, arr[k].Y);
                if (arr[k].Y <= arr[i].Y || arr[k].Y <= arr[j].Y)
                    lft = max(lft, arr[k].Y);
            }
            if (lft == -1 && rgt == INF)
                smx.pb(pii(abs(arr[i].Y - arr[j].Y), arr[i].X - arr[j].X - 1));
            else if (lft == -1)
                lmi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), rgt - 1), arr[i].X - arr[j].X - 1));
            else if (rgt == INF)
                rmi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), c - lft), arr[i].X - arr[j].X - 1));
            else if (rgt - lft - 1 > abs(arr[i].Y - arr[i].Y))
                smi_m.pb(make_pair(pii(abs(arr[i].Y - arr[j].Y), rgt - lft - 1), arr[i].X - arr[j].X - 1));
        }

    // D
    for (int i = 1; i <= n; ++i) {
        int lft = -1, rgt = INF;
        for (int j = n; arr[j].X > arr[i].X; --j) {
            if (arr[j].Y >= arr[i].Y)
                rgt = min(rgt, arr[j].Y);
            if (arr[j].Y <= arr[i].Y)
                lft = max(lft, arr[j].Y);
        }
        if (lft == -1 && rgt == INF)
            dmx = max(dmx, r - arr[i].X);
        else if (lft == -1)
            lmi_d.pb(pii(rgt - 1, r - arr[i].X));
        else if (rgt == INF)
            rmi_d.pb(pii(c - lft, r - arr[i].X));
        else if (rgt - lft - 1 > 0)
            smi_d.pb(pii(rgt - lft - 1, r - arr[i].X));
    }

    /*L.pb(lmi_a);
    for (auto p : lmi_u)
        L.pb(p.X);
    for (auto p : lmi_m) {
        L.pb(p.X.X);
        L.pb(p.X.Y);
    }
    for (auto p : lmi_d)
        L.pb(p.X);*/
    for (int i = 1; i <= n; ++i)
        L.pb(arr[i].Y - 1);
    sort(ALL(L)), L.resize(unique(ALL(L)) - L.begin());

    M.pb(smi_a);
    for (auto p : smi_u)
        M.pb(p.X);
    for (auto p : smi_m) {
        M.pb(p.X.X);
        M.pb(p.X.Y);
    }
    for (auto p : smi_d)
        M.pb(p.X);
    sort(ALL(M)), M.resize(unique(ALL(M)) - M.begin());

    /*R.pb(rmi_a);
    for (auto p : rmi_u)
        R.pb(p.X);
    for (auto p : rmi_m) {
        R.pb(p.X.X);
        R.pb(p.X.Y);
    }
    for (auto p : rmi_d)
        R.pb(p.X);*/
    for (int i = 1; i <= n; ++i)
        R.pb(c - arr[i].Y);
    sort(ALL(R)), R.resize(unique(ALL(R)) - R.begin());

    for (int lft : L) {
        if (lft < lmi_a)
            continue;
        val.clear();
        if (smi_a >= lft)
            val.pb(smi_a);
        val.pb(lft + rmi_a);
        for (int sum : M)
            if (sum >= lft)
                val.pb(sum);
        for (int rgt : R)
            val.pb(rgt + lft);
        sort(ALL(val)), val.resize(unique(ALL(val)) - val.begin());
        vector<int> seg_u(4 * SZ(val)), res_u(SZ(val));
        vector<int> seg_m(4 * SZ(val)), res_m(SZ(val));
        vector<int> seg_d(4 * SZ(val)), res_d(SZ(val));

        modify(seg_u, 0, SZ(val), umx);
        for (auto p : smx)
            modify(seg_m, lower_bound(ALL(val), p.X) - val.begin(), SZ(val), p.Y);
        modify(seg_d, 0, SZ(val), dmx);

        for (auto p : lmi_u)
            if (lft < p.X)
                modify(seg_u, 0, SZ(val), p.Y);
        for (auto p : lmi_m)
            if (lft < p.X.Y)
                modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), SZ(val), p.Y);
        for (auto p : lmi_d)
            if (lft < p.X)
                modify(seg_d, 0, SZ(val), p.Y);

        for (auto p : smi_u)
            modify(seg_u, 0, lower_bound(ALL(val), p.X) - val.begin(), p.Y);
        for (auto p : smi_m)
            modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), lower_bound(ALL(val), p.X.Y) - val.begin(), p.Y);
        for (auto p : smi_d)
            modify(seg_d, 0, lower_bound(ALL(val), p.X) - val.begin(), p.Y);

        for (auto p : rmi_u)
            modify(seg_u, 0, lower_bound(ALL(val), p.X + lft) - val.begin(), p.Y);
        for (auto p : rmi_m)
            modify(seg_m, lower_bound(ALL(val), p.X.X) - val.begin(), lower_bound(ALL(val), p.X.Y + lft) - val.begin(), p.Y);
        for (auto p : rmi_d)
            modify(seg_d, 0, lower_bound(ALL(val), p.X + lft) - val.begin(), p.Y);
        
        dfs(seg_u, 0, SZ(val) - 1, 1, 0, res_u);
        dfs(seg_m, 0, SZ(val) - 1, 1, 0, res_m);
        dfs(seg_d, 0, SZ(val) - 1, 1, 0, res_d);

        for (int i = 0; i < SZ(val); ++i) {
            if (val[i] < smi_a || val[i] - lft < rmi_a)
                continue;
            ans = min((ll)ans, (ll)val[i] + max((ll)res_m[i], (ll)res_u[i] + (ll)res_d[i]));
        }
    }

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 12 ms 344 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 23 ms 332 KB Output is correct
27 Correct 23 ms 344 KB Output is correct
28 Correct 9 ms 332 KB Output is correct
29 Correct 24 ms 344 KB Output is correct
30 Correct 29 ms 364 KB Output is correct
31 Correct 25 ms 348 KB Output is correct
32 Correct 24 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 12 ms 344 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 23 ms 332 KB Output is correct
27 Correct 23 ms 344 KB Output is correct
28 Correct 9 ms 332 KB Output is correct
29 Correct 24 ms 344 KB Output is correct
30 Correct 29 ms 364 KB Output is correct
31 Correct 25 ms 348 KB Output is correct
32 Correct 24 ms 332 KB Output is correct
33 Correct 254 ms 716 KB Output is correct
34 Correct 31 ms 372 KB Output is correct
35 Correct 60 ms 412 KB Output is correct
36 Correct 89 ms 544 KB Output is correct
37 Correct 86 ms 540 KB Output is correct
38 Correct 85 ms 536 KB Output is correct
39 Correct 91 ms 544 KB Output is correct
40 Correct 88 ms 500 KB Output is correct
41 Correct 157 ms 928 KB Output is correct
42 Correct 32 ms 336 KB Output is correct
43 Correct 102 ms 572 KB Output is correct
44 Correct 49 ms 444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 7 ms 392 KB Output is correct
22 Correct 8 ms 332 KB Output is correct
23 Correct 6 ms 332 KB Output is correct
24 Correct 4 ms 332 KB Output is correct
25 Correct 7 ms 408 KB Output is correct
26 Correct 5 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 8 ms 332 KB Output is correct
29 Correct 7 ms 332 KB Output is correct
30 Correct 9 ms 400 KB Output is correct
31 Correct 8 ms 400 KB Output is correct
32 Correct 7 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 4 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 7 ms 396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 12 ms 344 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 23 ms 332 KB Output is correct
27 Correct 23 ms 344 KB Output is correct
28 Correct 9 ms 332 KB Output is correct
29 Correct 24 ms 344 KB Output is correct
30 Correct 29 ms 364 KB Output is correct
31 Correct 25 ms 348 KB Output is correct
32 Correct 24 ms 332 KB Output is correct
33 Correct 254 ms 716 KB Output is correct
34 Correct 31 ms 372 KB Output is correct
35 Correct 60 ms 412 KB Output is correct
36 Correct 89 ms 544 KB Output is correct
37 Correct 86 ms 540 KB Output is correct
38 Correct 85 ms 536 KB Output is correct
39 Correct 91 ms 544 KB Output is correct
40 Correct 88 ms 500 KB Output is correct
41 Correct 157 ms 928 KB Output is correct
42 Correct 32 ms 336 KB Output is correct
43 Correct 102 ms 572 KB Output is correct
44 Correct 49 ms 444 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 1 ms 316 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 320 KB Output is correct
60 Correct 1 ms 316 KB Output is correct
61 Correct 1 ms 332 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 324 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 7 ms 392 KB Output is correct
66 Correct 8 ms 332 KB Output is correct
67 Correct 6 ms 332 KB Output is correct
68 Correct 4 ms 332 KB Output is correct
69 Correct 7 ms 408 KB Output is correct
70 Correct 5 ms 332 KB Output is correct
71 Correct 2 ms 332 KB Output is correct
72 Correct 8 ms 332 KB Output is correct
73 Correct 7 ms 332 KB Output is correct
74 Correct 9 ms 400 KB Output is correct
75 Correct 8 ms 400 KB Output is correct
76 Correct 7 ms 332 KB Output is correct
77 Correct 2 ms 332 KB Output is correct
78 Correct 4 ms 332 KB Output is correct
79 Correct 3 ms 332 KB Output is correct
80 Correct 7 ms 396 KB Output is correct
81 Correct 38 ms 516 KB Output is correct
82 Correct 38 ms 484 KB Output is correct
83 Correct 62 ms 520 KB Output is correct
84 Correct 16 ms 332 KB Output is correct
85 Correct 92 ms 580 KB Output is correct
86 Correct 101 ms 628 KB Output is correct
87 Correct 68 ms 500 KB Output is correct
88 Correct 47 ms 332 KB Output is correct
89 Correct 56 ms 384 KB Output is correct
90 Correct 52 ms 332 KB Output is correct
91 Correct 85 ms 664 KB Output is correct
92 Correct 94 ms 632 KB Output is correct
93 Correct 63 ms 400 KB Output is correct
94 Correct 26 ms 348 KB Output is correct
95 Correct 87 ms 640 KB Output is correct
96 Correct 62 ms 632 KB Output is correct
97 Correct 45 ms 376 KB Output is correct
98 Correct 113 ms 436 KB Output is correct