답안 #809007

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
809007 2023-08-05T13:54:14 Z jakobrs 웜뱃 (IOI13_wombats) C++17
55 / 100
20000 ms 27124 KB
#include <algorithm>
#include <numeric>
#include <queue>
#include <vector>

#ifndef EVAL
#include <iostream>
#endif

#include "wombats.h"

#define entire(a) std::begin((a)), std::end((a))

const int64_t INF = 2'000'000'000'000;

struct Matrix {
    int64_t a1 = 0, a2 = INF, b1 = INF, b2 = 0;

    Matrix operator*(const Matrix &rhs) const {
        return {
            .a1 = std::min(a1 + rhs.a1, b1 + rhs.a2),
            .a2 = std::min(b2 + rhs.a2, a2 + rhs.a1),
            .b1 = std::min(a1 + rhs.b1, b1 + rhs.b2),
            .b2 = std::min(b2 + rhs.b2, a2 + rhs.b1),
        };
    }
};

template <typename T>
struct SegmentTree {
    std::vector<T> values;
    int offset;

    SegmentTree() : values(), offset() {}
    explicit SegmentTree(int sz) : values(2 * sz), offset(sz) {}

    void update(int i, T value) {
        i += offset;

        values[i] = value;

        propagate(i);
    }

    void propagate(int i) {
        while (i /= 2) values[i] = values[2 * i] * values[2 * i + 1];
    }

    T query(int l, int r) {
        l += offset;
        r += offset;

        T left, right;
        while (l < r) {
            if (l & 1) left = left * values[l++];
            if (r & 1) right = values[--r] * right;

            l >>= 1;
            r >>= 1;
        }

        return left * right;
    }
};

std::vector<std::vector<int>> h, v;
int r, c;
SegmentTree<Matrix> st;
void init(int R, int C, int H[5000][200], int V[5000][200]) {
    h.reserve(R);
    v.reserve(R);
    for (int i = 0; i < R; i++) {
        h.emplace_back(entire(H[i]));
        v.emplace_back(entire(V[i]));
    }
    r = R;
    c = C;

    int o = 1;
    while (o < 2 * R) o *= 2;
    st = SegmentTree<Matrix>(o);

    for (int i = 0; i < R; i++) {
        st.update(2 * i, {
                             .a2 = H[i][0],
                             .b1 = H[i][0],
                         });
        if (i + 1 < R) {
            st.update(2 * i + 1, {
                                     .a1 = V[i][0],
                                     .b2 = V[i][1],
                                 });
        }
    }
}

void changeH(int P, int Q, int W) {
    h[P][Q] = W;
    st.update(2 * P, {
                         .a2 = h[P][0],
                         .b1 = h[P][0],
                     });
}

void changeV(int P, int Q, int W) {
    v[P][Q] = W;
    st.update(2 * P + 1, {
                             .a1 = v[P][0],
                             .b2 = v[P][1],
                         });
}

int escape(int V1, int V2) {
    if (c == 2) {
        auto res = st.query(0, 2 * r);

        if (V1 == 0 && V2 == 0)
            return res.a1;
        else if (V1 == 0 && V2 == 1)
            return res.b1;
        else if (V1 == 1 && V2 == 0)
            return res.a2;
        else if (V1 == 1 && V2 == 1)
            return res.b2;
    } else {
        std::vector<int> dist(c, 0);

        dist[V1] = 0;
        for (int i = V1 + 1; i < c; i++) {
            dist[i] = dist[i - 1] + h[0][i - 1];
        }
        for (int i = V1 - 1; i >= 0; i--) {
            dist[i] = dist[i + 1] + h[0][i];
        }

        std::priority_queue<std::pair<int, int>> pq;

        for (int i = 0; i + 1 < r; i++) {
            // phase 1: process vertical segments
            for (int j = 0; j < c; j++) {
                dist[j] += v[i][j];
            }

            // phase 2: process horizontal segments
            while (!pq.empty()) pq.pop();  // whyy is there no pq.clear();
            for (int j = 0; j < c; j++) {
                pq.push({-dist[j], j});
            }
            while (!pq.empty()) {
                auto [d, j] = pq.top();
                pq.pop();
                d = -d;

                if (d > dist[j]) continue;

                if (j > 0 && dist[j - 1] > d + h[i + 1][j - 1]) {
                    dist[j - 1] = d + h[i + 1][j - 1];
                    pq.push({-dist[j - 1], j - 1});
                }
                if (j + 1 < c && dist[j + 1] > d + h[i + 1][j]) {
                    dist[j + 1] = d + h[i + 1][j];
                    pq.push({-dist[j + 1], j + 1});
                }
            }
        }

        return dist[V2];
    }
}

Compilation message

grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   15 |  int res;
      |      ^~~
wombats.cpp: In function 'int escape(int, int)':
wombats.cpp:169:1: warning: control reaches end of non-void function [-Wreturn-type]
  169 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 13396 KB Output is correct
2 Correct 36 ms 13396 KB Output is correct
3 Correct 11610 ms 15000 KB Output is correct
4 Correct 36 ms 13396 KB Output is correct
5 Correct 39 ms 13396 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 8 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 4 ms 368 KB Output is correct
7 Correct 7 ms 340 KB Output is correct
8 Correct 8 ms 368 KB Output is correct
9 Correct 9 ms 364 KB Output is correct
10 Correct 8 ms 380 KB Output is correct
11 Correct 4522 ms 1500 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 624 KB Output is correct
2 Correct 55 ms 692 KB Output is correct
3 Correct 97 ms 724 KB Output is correct
4 Correct 105 ms 704 KB Output is correct
5 Correct 91 ms 704 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 296 KB Output is correct
9 Correct 65 ms 700 KB Output is correct
10 Correct 0 ms 296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17364 KB Output is correct
2 Correct 9 ms 17336 KB Output is correct
3 Correct 9 ms 17300 KB Output is correct
4 Correct 40 ms 18060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 620 KB Output is correct
2 Correct 55 ms 692 KB Output is correct
3 Correct 92 ms 656 KB Output is correct
4 Correct 94 ms 596 KB Output is correct
5 Correct 91 ms 704 KB Output is correct
6 Correct 9 ms 17364 KB Output is correct
7 Correct 10 ms 17364 KB Output is correct
8 Correct 10 ms 17328 KB Output is correct
9 Correct 41 ms 18260 KB Output is correct
10 Correct 36 ms 13396 KB Output is correct
11 Correct 36 ms 13396 KB Output is correct
12 Correct 11623 ms 16276 KB Output is correct
13 Correct 37 ms 13396 KB Output is correct
14 Correct 37 ms 13396 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 8 ms 300 KB Output is correct
19 Correct 5 ms 304 KB Output is correct
20 Correct 4 ms 292 KB Output is correct
21 Correct 6 ms 372 KB Output is correct
22 Correct 7 ms 340 KB Output is correct
23 Correct 9 ms 340 KB Output is correct
24 Correct 8 ms 292 KB Output is correct
25 Correct 4538 ms 2704 KB Output is correct
26 Correct 7 ms 340 KB Output is correct
27 Correct 65 ms 696 KB Output is correct
28 Execution timed out 20072 ms 20936 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 620 KB Output is correct
2 Correct 55 ms 696 KB Output is correct
3 Correct 94 ms 724 KB Output is correct
4 Correct 94 ms 704 KB Output is correct
5 Correct 92 ms 724 KB Output is correct
6 Correct 9 ms 17364 KB Output is correct
7 Correct 10 ms 17420 KB Output is correct
8 Correct 10 ms 17432 KB Output is correct
9 Correct 42 ms 18340 KB Output is correct
10 Correct 38 ms 13428 KB Output is correct
11 Correct 37 ms 13396 KB Output is correct
12 Correct 11624 ms 16328 KB Output is correct
13 Correct 37 ms 13396 KB Output is correct
14 Correct 38 ms 13396 KB Output is correct
15 Correct 317 ms 27124 KB Output is correct
16 Execution timed out 20004 ms 25144 KB Time limit exceeded
17 Halted 0 ms 0 KB -