답안 #222649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
222649 2020-04-13T14:06:23 Z VEGAnn Sky Walking (IOI19_walk) C++14
57 / 100
4000 ms 511252 KB
#include <bits/stdc++.h>
#include "walk.h"
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pii pair<int,int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 100100;
const ll OO = 1e18;
unordered_map<int, int> MEM[N];
set<array<ll, 3> > pq;
vector<ll> Ans[N];
vector<int> vc, real_y, tch[N], tct[N];
vector<array<int, 3> > lines;
vector<pii> events[N];
int n, m, pos, H[N], nm[N], Y[N];
bool fd;
ll st[4 * N], psh[4 * N], ans = OO, gt_vl, bad;

void build(int v, int l, int r){
    psh[v] = 0;
    st[v] = OO;

    if (l == r) return;

    int md = (l + r) >> 1;
    build(v + v, l, md);
    build(v + v + 1, md + 1, r);
}

void bluid(int v, int l, int r){
    if (l == r){
        st[v] = -H[l];
        return;
    }

    int md = (l + r) >> 1;

    bluid(v + v, l, md);
    bluid(v + v + 1, md + 1, r);

    st[v] = min(st[v + v], st[v + v + 1]);
}

void push(int v){
    if (psh[v] != 0){
        st[v] += psh[v];

        if (v + v + 1 < 4 * N){
            psh[v + v] += psh[v];
            psh[v + v + 1] += psh[v];
        }

        psh[v] = 0;
    }
}

void update(int v, int l, int r, int ps, ll vl){
    push(v);

    if (l == r){
        st[v] = vl;
        return;
    }

    int md = (l + r) >> 1;

    if (ps <= md)
        update(v + v, l, md, ps, vl);
    else update(v + v + 1, md + 1, r, ps, vl);

    push(v + v);
    push(v + v + 1);

    st[v] = min(st[v + v], st[v + v + 1]);
}

void add(int v, int tl, int tr, int l, int r, ll vl){
    push(v);

    if (l > r) return;

    if (tl == l && tr == r){
        psh[v] += vl;
        push(v);
        return;
    }

    int md = (tl + tr) >> 1;
    add(v + v, tl, md, l, min(r, md), vl);
    add(v + v + 1, md + 1, tr, max(md + 1, l), r, vl);

    st[v] = min(st[v + v], st[v + v + 1]);
}

void real_search_left(int v, int l, int r){
    push(v);

    if (l == r){
        fd = 1;
        pos = l;
        gt_vl = st[v];
        return;
    }

    push(v + v);
    push(v + v + 1);

    int md = (l + r) >> 1;

    if (st[v + v + 1] < bad)
        real_search_left(v + v + 1, md + 1, r);
    else real_search_left(v + v, l, md);
}

void search_left(int v, int tl, int tr, int l, int r){
    push(v);

    if (fd || l > r) return;

    if (tl == l && tr == r){
        if (st[v] < bad)
            real_search_left(v, l, r);
        return;
    }

    int md = (tl + tr) >> 1;
    search_left(v + v + 1, md + 1, tr, max(md + 1, l), r);
    search_left(v + v, tl, md, l, min(r, md));
}


void real_search_right(int v, int l, int r){
    push(v);

    if (l == r){
        fd = 1;
        pos = l;
        gt_vl = st[v];
        return;
    }

    push(v + v);
    push(v + v + 1);

    int md = (l + r) >> 1;

    if (st[v + v] < bad)
        real_search_right(v + v, l, md);
    else real_search_right(v + v + 1, md + 1, r);
}

void search_right(int v, int tl, int tr, int l, int r){
    push(v);

    if (fd || l > r) return;

    if (tl == l && tr == r){
        if (st[v] < bad)
            real_search_right(v, l, r);
        return;
    }

    int md = (tl + tr) >> 1;
    search_right(v + v, tl, md, l, min(r, md));
    search_right(v + v + 1, md + 1, tr, max(md + 1, l), r);
}

void get_ans(int v, int l, int r){
    push(v);
    if (l == r){
        if (st[v] < OO)
            ans = min(ans, st[v] + ll(real_y[l]));
        return;
    }

    int md = (l + r) >> 1;
    get_ans(v + v, l, md);
    get_ans(v + v + 1, md + 1, r);
}

bool cmp(int _x, int _y){
    return Y[_x] < Y[_y];
}

long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
    lines.clear();

    n = sz(x); m = sz(l);

    for (int i = 0; i < m; i++) {
        real_y.PB(y[i]);
        nm[i] = i;
        Y[i] = y[i];
    }

    sort(all(real_y));
    real_y.resize(unique(all(real_y)) - real_y.begin());

    for (int i = 0; i < m; i++){
        y[i] = lower_bound(all(real_y), y[i]) - real_y.begin();
    }

    if (s != 0 || g != n - 1){
//    if (1){
        for (int i = 0; i < n; i++)
            H[i] = h[i];

        bluid(1, 0, n - 1);

        sort(nm, nm + m, cmp);

        for (int it = 0; it < m; it++){
            int i = nm[it];
            tch[i].PB(l[i]);

            MEM[l[i]][i] = sz(tct[l[i]]);
            tct[l[i]].PB(i);
            Ans[l[i]].PB(OO);
            int cur = l[i];
            bad = -(Y[i] - 1);

            while (1){
                fd = 0; pos = -1;
                search_right(1, 0, n - 1, cur + 1, r[i]);

                tch[i].PB(pos);

                MEM[pos][i] = sz(tct[pos]);
                tct[pos].PB(i);
                Ans[pos].PB(OO);
                cur = pos;

                if (pos == r[i])
                    break;
            }
        }

        for (int it = 0; it < sz(tct[s]); it++){
            Ans[s][it] = Y[tct[s][it]];
//            cerr << tct[s][it] << '\n';

            pq.insert({Ans[s][it], s, it});
        }

        while (sz(pq)){
            array<ll, 3> CR = (*pq.begin());
            pq.erase(pq.begin());

            int ps = CR[1], it = CR[2];
            int id = tct[ps][it];

            if (it > 0){
                ll nw = CR[0] + ll(Y[id]) - ll(Y[tct[ps][it - 1]]);

                if (Ans[ps][it - 1] > nw){
                    pq.erase({Ans[ps][it - 1], ps, it - 1});
                    Ans[ps][it - 1] = nw;
                    pq.insert({Ans[ps][it - 1], ps, it - 1});
                }
            }

            if (it + 1 < sz(tct[ps])){
                ll nw = CR[0] - ll(Y[id]) + ll(Y[tct[ps][it + 1]]);

                if (Ans[ps][it + 1] > nw){
                    pq.erase({Ans[ps][it + 1], ps, it + 1});
                    Ans[ps][it + 1] = nw;
                    pq.insert({Ans[ps][it + 1], ps, it + 1});
                }
            }

            int lc = -1;

            for (int ii = 0; ii < sz(tch[id]); ii++)
                if (tch[id][ii] == ps){
                    lc = ii;
                    break;
                }

            assert(lc != -1);

            if (lc > 0){
                int nps = tch[id][lc - 1];
                int nit = MEM[nps][id];

                ll nw = CR[0] + ll(x[ps]) - ll(x[nps]);

                if (Ans[nps][nit] > nw){
                    pq.erase({Ans[nps][nit], nps, nit});
                    Ans[nps][nit] = nw;
                    pq.insert({Ans[nps][nit], nps, nit});
                }
            }

            if (lc + 1 < sz(tch[id])){
                int nps = tch[id][lc + 1];
                int nit = MEM[nps][id];

                ll nw = CR[0] - ll(x[ps]) + ll(x[nps]);

                if (Ans[nps][nit] > nw){
                    pq.erase({Ans[nps][nit], nps, nit});
                    Ans[nps][nit] = nw;
                    pq.insert({Ans[nps][nit], nps, nit});
                }
            }
        }

        for (int it = 0; it < sz(tct[g]); it++)
            if (Ans[g][it] < OO)
                ans = min(ans, Ans[g][it] + ll(Y[tct[g][it]]));

        return (ans == OO ? -1 : ans);
    }

    bad = OO;

    for (int i = 0; i < m; i++)
        lines.PB({y[i], l[i], r[i]});

    sort(all(lines));

    for (int i = 0; i < m; ){
        int j = i;
        while (j < m && lines[j][0] == lines[i][0])
            j++;

        pii seg = MP(lines[i][1], lines[i][2]);
        int ht = lines[i][0];

        for (int I = i + 1; I < j; I++)
            if (seg.sd < lines[I][1]){
                events[seg.ft].PB(MP(-1, ht));
                events[seg.sd].PB(MP(+1, ht));

                seg = MP(lines[I][1], lines[I][2]);
            } else seg.sd = max(lines[I][2], seg.sd);

        events[seg.ft].PB(MP(-1, ht));
        events[seg.sd].PB(MP(+1, ht));

        i = j;
    }

    build(1, 0, sz(real_y) - 1);

    for (pii cr : events[0]){
        int ht = cr.sd;

        update(1, 0, sz(real_y) - 1, ht, real_y[ht]);
    }

    add(1, 0, sz(real_y) - 1, 0, sz(real_y) - 1, x[1] - x[0]);

    for (int loc = 1; loc < n - 1; loc++){
        sort(all(events[loc]));

        for (pii cr : events[loc]){
            int ht = cr.sd;

            if (cr.ft > 0){
                update(1, 0, sz(real_y) - 1, ht, OO);
            } else {
                push(1);
                if (st[1] == OO) return -1;

                ll best = OO;

                fd = 0; pos = -1; gt_vl = -1;

                search_left(1, 0, sz(real_y) - 1, 0, ht - 1);

                if (fd){
                    best = min(best, gt_vl + ll(real_y[ht]) - ll(real_y[pos]));
                }

                fd = 0; pos = -1; gt_vl = -1;

                search_right(1, 0, sz(real_y) - 1, ht + 1, sz(real_y) - 1);

                if (fd){
                    best = min(best, gt_vl - ll(real_y[ht]) + ll(real_y[pos]));
                }

                if (best == OO) return -1;

                update(1, 0, sz(real_y) - 1, ht, best);
            }
        }

        add(1, 0, sz(real_y) - 1, 0, sz(real_y) - 1, x[loc + 1] - x[loc]);
    }

    get_ans(1, 0, sz(real_y) - 1);

    return (ans == OO ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 15232 KB Output is correct
2 Correct 14 ms 15232 KB Output is correct
3 Correct 14 ms 15232 KB Output is correct
4 Correct 17 ms 15232 KB Output is correct
5 Correct 15 ms 15232 KB Output is correct
6 Correct 15 ms 15232 KB Output is correct
7 Correct 14 ms 15232 KB Output is correct
8 Correct 15 ms 15232 KB Output is correct
9 Correct 14 ms 15232 KB Output is correct
10 Correct 14 ms 15232 KB Output is correct
11 Correct 14 ms 15232 KB Output is correct
12 Correct 14 ms 15232 KB Output is correct
13 Correct 14 ms 15232 KB Output is correct
14 Correct 14 ms 15232 KB Output is correct
15 Correct 15 ms 15232 KB Output is correct
16 Correct 14 ms 15232 KB Output is correct
17 Correct 14 ms 15232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 15232 KB Output is correct
2 Correct 14 ms 15232 KB Output is correct
3 Correct 1259 ms 68440 KB Output is correct
4 Correct 1119 ms 79600 KB Output is correct
5 Correct 597 ms 70256 KB Output is correct
6 Correct 521 ms 65136 KB Output is correct
7 Correct 589 ms 70128 KB Output is correct
8 Correct 1497 ms 82800 KB Output is correct
9 Correct 712 ms 70384 KB Output is correct
10 Correct 1486 ms 99824 KB Output is correct
11 Correct 630 ms 49648 KB Output is correct
12 Correct 290 ms 33768 KB Output is correct
13 Correct 323 ms 34024 KB Output is correct
14 Correct 285 ms 44788 KB Output is correct
15 Correct 306 ms 44836 KB Output is correct
16 Correct 377 ms 45300 KB Output is correct
17 Correct 323 ms 43888 KB Output is correct
18 Correct 136 ms 32272 KB Output is correct
19 Correct 25 ms 16768 KB Output is correct
20 Correct 124 ms 29812 KB Output is correct
21 Correct 112 ms 27112 KB Output is correct
22 Correct 114 ms 27752 KB Output is correct
23 Correct 193 ms 31080 KB Output is correct
24 Correct 167 ms 27880 KB Output is correct
25 Correct 122 ms 27116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 18684 KB Output is correct
2 Correct 231 ms 28264 KB Output is correct
3 Correct 245 ms 28796 KB Output is correct
4 Correct 285 ms 31336 KB Output is correct
5 Correct 278 ms 31208 KB Output is correct
6 Correct 267 ms 31208 KB Output is correct
7 Correct 167 ms 25452 KB Output is correct
8 Correct 285 ms 30952 KB Output is correct
9 Correct 257 ms 31336 KB Output is correct
10 Correct 184 ms 28312 KB Output is correct
11 Correct 28 ms 16764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 18684 KB Output is correct
2 Correct 231 ms 28264 KB Output is correct
3 Correct 245 ms 28796 KB Output is correct
4 Correct 285 ms 31336 KB Output is correct
5 Correct 278 ms 31208 KB Output is correct
6 Correct 267 ms 31208 KB Output is correct
7 Correct 167 ms 25452 KB Output is correct
8 Correct 285 ms 30952 KB Output is correct
9 Correct 257 ms 31336 KB Output is correct
10 Correct 184 ms 28312 KB Output is correct
11 Correct 28 ms 16764 KB Output is correct
12 Correct 245 ms 28880 KB Output is correct
13 Correct 151 ms 30440 KB Output is correct
14 Correct 279 ms 31492 KB Output is correct
15 Correct 207 ms 29544 KB Output is correct
16 Correct 226 ms 29672 KB Output is correct
17 Correct 216 ms 29776 KB Output is correct
18 Correct 211 ms 29416 KB Output is correct
19 Correct 220 ms 29672 KB Output is correct
20 Correct 177 ms 25452 KB Output is correct
21 Correct 53 ms 17784 KB Output is correct
22 Correct 226 ms 29800 KB Output is correct
23 Correct 253 ms 30184 KB Output is correct
24 Correct 264 ms 30312 KB Output is correct
25 Correct 259 ms 29928 KB Output is correct
26 Correct 231 ms 30440 KB Output is correct
27 Correct 278 ms 31336 KB Output is correct
28 Correct 148 ms 30440 KB Output is correct
29 Correct 279 ms 31464 KB Output is correct
30 Correct 191 ms 25452 KB Output is correct
31 Correct 275 ms 31592 KB Output is correct
32 Correct 172 ms 26472 KB Output is correct
33 Correct 192 ms 26984 KB Output is correct
34 Correct 192 ms 28520 KB Output is correct
35 Correct 209 ms 26984 KB Output is correct
36 Correct 196 ms 26984 KB Output is correct
37 Correct 113 ms 25064 KB Output is correct
38 Correct 128 ms 25448 KB Output is correct
39 Correct 193 ms 28904 KB Output is correct
40 Correct 167 ms 25832 KB Output is correct
41 Correct 124 ms 25192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 15232 KB Output is correct
2 Correct 14 ms 15232 KB Output is correct
3 Correct 14 ms 15232 KB Output is correct
4 Correct 17 ms 15232 KB Output is correct
5 Correct 15 ms 15232 KB Output is correct
6 Correct 15 ms 15232 KB Output is correct
7 Correct 14 ms 15232 KB Output is correct
8 Correct 15 ms 15232 KB Output is correct
9 Correct 14 ms 15232 KB Output is correct
10 Correct 14 ms 15232 KB Output is correct
11 Correct 14 ms 15232 KB Output is correct
12 Correct 14 ms 15232 KB Output is correct
13 Correct 14 ms 15232 KB Output is correct
14 Correct 14 ms 15232 KB Output is correct
15 Correct 15 ms 15232 KB Output is correct
16 Correct 14 ms 15232 KB Output is correct
17 Correct 14 ms 15232 KB Output is correct
18 Correct 14 ms 15232 KB Output is correct
19 Correct 14 ms 15232 KB Output is correct
20 Correct 1259 ms 68440 KB Output is correct
21 Correct 1119 ms 79600 KB Output is correct
22 Correct 597 ms 70256 KB Output is correct
23 Correct 521 ms 65136 KB Output is correct
24 Correct 589 ms 70128 KB Output is correct
25 Correct 1497 ms 82800 KB Output is correct
26 Correct 712 ms 70384 KB Output is correct
27 Correct 1486 ms 99824 KB Output is correct
28 Correct 630 ms 49648 KB Output is correct
29 Correct 290 ms 33768 KB Output is correct
30 Correct 323 ms 34024 KB Output is correct
31 Correct 285 ms 44788 KB Output is correct
32 Correct 306 ms 44836 KB Output is correct
33 Correct 377 ms 45300 KB Output is correct
34 Correct 323 ms 43888 KB Output is correct
35 Correct 136 ms 32272 KB Output is correct
36 Correct 25 ms 16768 KB Output is correct
37 Correct 124 ms 29812 KB Output is correct
38 Correct 112 ms 27112 KB Output is correct
39 Correct 114 ms 27752 KB Output is correct
40 Correct 193 ms 31080 KB Output is correct
41 Correct 167 ms 27880 KB Output is correct
42 Correct 122 ms 27116 KB Output is correct
43 Correct 42 ms 18684 KB Output is correct
44 Correct 231 ms 28264 KB Output is correct
45 Correct 245 ms 28796 KB Output is correct
46 Correct 285 ms 31336 KB Output is correct
47 Correct 278 ms 31208 KB Output is correct
48 Correct 267 ms 31208 KB Output is correct
49 Correct 167 ms 25452 KB Output is correct
50 Correct 285 ms 30952 KB Output is correct
51 Correct 257 ms 31336 KB Output is correct
52 Correct 184 ms 28312 KB Output is correct
53 Correct 28 ms 16764 KB Output is correct
54 Correct 245 ms 28880 KB Output is correct
55 Correct 151 ms 30440 KB Output is correct
56 Correct 279 ms 31492 KB Output is correct
57 Correct 207 ms 29544 KB Output is correct
58 Correct 226 ms 29672 KB Output is correct
59 Correct 216 ms 29776 KB Output is correct
60 Correct 211 ms 29416 KB Output is correct
61 Correct 220 ms 29672 KB Output is correct
62 Correct 177 ms 25452 KB Output is correct
63 Correct 53 ms 17784 KB Output is correct
64 Correct 226 ms 29800 KB Output is correct
65 Correct 253 ms 30184 KB Output is correct
66 Correct 264 ms 30312 KB Output is correct
67 Correct 259 ms 29928 KB Output is correct
68 Correct 231 ms 30440 KB Output is correct
69 Correct 278 ms 31336 KB Output is correct
70 Correct 148 ms 30440 KB Output is correct
71 Correct 279 ms 31464 KB Output is correct
72 Correct 191 ms 25452 KB Output is correct
73 Correct 275 ms 31592 KB Output is correct
74 Correct 172 ms 26472 KB Output is correct
75 Correct 192 ms 26984 KB Output is correct
76 Correct 192 ms 28520 KB Output is correct
77 Correct 209 ms 26984 KB Output is correct
78 Correct 196 ms 26984 KB Output is correct
79 Correct 113 ms 25064 KB Output is correct
80 Correct 128 ms 25448 KB Output is correct
81 Correct 193 ms 28904 KB Output is correct
82 Correct 167 ms 25832 KB Output is correct
83 Correct 124 ms 25192 KB Output is correct
84 Correct 127 ms 24824 KB Output is correct
85 Execution timed out 4103 ms 511252 KB Time limit exceeded
86 Halted 0 ms 0 KB -