답안 #711681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711681 2023-03-17T11:06:58 Z Cyanmond Bubble Sort 2 (JOI18_bubblesort2) C++17
60 / 100
9000 ms 43464 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>

constexpr int inf = 1 << 25;

class SegTree {
    int n, size;
    std::vector<int> data;
    public:
    SegTree() {}
    SegTree(const std::vector<int> &vec) {
        n = (int)vec.size();
        size = 1;
        while (size < n) size *= 2;
        data.assign(2 * size, 0);
        std::copy(vec.begin(), vec.end(), data.begin() + size);
        for (int i = size - 1; i >= 1; --i) data[i] = data[2 * i] + data[2 * i + 1];
    }

    void set(int i, int v) {
        i += size;
        data[i] = v;
        while (i != 1) {
            i /= 2;
            data[i] = data[2 * i] + data[2 * i + 1];
        }
    }

    int fold(int l, int r) {
        int ret = 0;
        for (l += size, r += size; l < r; l /= 2, r /= 2) {
            if (l & 1) ret += data[l++];
            if (r & 1) ret += data[--r];
        }
        return ret;
    }
};

using T = std::pair<int, int>;
struct kDTree {
    int xMn, xMx, yMn, yMx;
    int val, lazy;
    kDTree *l = nullptr, *r = nullptr;
    kDTree(std::vector<T> &vec, bool divx) {
        val = lazy = 0;
        xMn = yMn = inf, xMx = yMx = -inf;
        const int n = (int)vec.size();
        for (int i = 0; i < n; ++i) {
            const auto &[x, y] = vec[i];
            xMn = std::min(xMn, x);
            xMx = std::max(xMx, x);
            yMn = std::min(yMn, y);
            yMx = std::max(yMx, y);
        }
        if (n == 1) return;
        const int m = n / 2;
        if (divx) {
            std::nth_element(vec.begin(), vec.begin() + m, vec.end(), [&](const auto &a, const auto &b) {
                return a.first < b.first;
            });
        } else {
            std::nth_element(vec.begin(), vec.begin() + m, vec.end(), [&](const auto &a, const auto &b) {
                return a.second < b.second;
            });
        }
        std::vector<T> lVec, rVec;
        std::copy(vec.begin(), vec.begin() + m, std::back_inserter(lVec));
        std::copy(vec.begin() + m, vec.end(), std::back_inserter(rVec));
        l = new kDTree(lVec, not divx);
        r = new kDTree(rVec, not divx);
    }

    void all_apply(int f) {
        val += f;
        lazy += f;
    }

    void push() {
        if (l != nullptr) l->all_apply(lazy);
        if (r != nullptr) r->all_apply(lazy);
        lazy = 0;
    }

    void add(int x1, int x2, int y1, int y2, int v) {
        push();
        if (x2 < xMn or x1 > xMx or y2 < yMn or y1 > yMx) return;
        if (x1 <= xMn and xMx <= x2 and y1 <= yMn and yMx <= y2) {
            all_apply(v);
            return;
        }
        l->add(x1, x2, y1, y2, v);
        r->add(x1, x2, y1, y2, v);
        val = -inf;
        if (l != nullptr) val = std::max(val, l->val);
        if (r != nullptr) val = std::max(val, r->val);
    }

    int calc(int x1, int x2, int y1, int y2) {
        push();
        if (x2 < xMn or x1 > xMx or y2 < yMn or y1 > yMx) return 0;
        if (x1 <= xMn and xMx <= x2 and y1 <= yMn and yMx <= y2) {
            return val;
        }
        return std::max(l->calc(x1, x2, y1, y2), r->calc(x1, x2, y1, y2));
    }
};

std::vector<int> countScans(std::vector<int> A, std::vector<int> X, std::vector<int> V) {
    const int N = (int)A.size(), Q = (int)X.size();
    {
        std::vector<int> vals;
        std::copy(A.begin(), A.end(), std::back_inserter(vals));
        std::copy(V.begin(), V.end(), std::back_inserter(vals));
        std::sort(vals.begin(), vals.end());
        vals.erase(std::unique(vals.begin(), vals.end()), vals.end());
        for (auto &e : A) e = (int)(std::lower_bound(vals.begin(), vals.end(), e) - vals.begin());
        for (auto &e : V) e = (int)(std::lower_bound(vals.begin(), vals.end(), e) - vals.begin());
    }
    std::vector<T> vec;
    for (int i = 0; i < N; ++i) {
        vec.push_back({i, A[i]});
    }
    for (int i = 0; i < Q; ++i) {
        vec.push_back({X[i], V[i]});
    }
    std::sort(vec.begin(), vec.end());
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
    kDTree tree(vec, true);
    for (const auto &[x, y] : vec) tree.add(x, x, y, y, -inf);
    for (int i = 0; i < N; ++i) {
        tree.add(i, i, A[i], A[i], inf);
        tree.add(i + 1, N - 1, 0, A[i] - 1, 1);
    }
    std::vector<int> answer(Q);

    for (int q = 0; q < Q; ++q) {
        const int x = X[q], newVal = V[q];
        const int preVal = A[x];
        tree.add(x + 1, N - 1, 0, preVal - 1, -1);
        tree.add(x + 1, N - 1, 0, newVal - 1, 1);
        tree.add(x, x, preVal, preVal, -inf);
        tree.add(x, x, newVal, newVal, inf);
        answer[q] = tree.calc(0, N - 1, 0, inf);
        A[x] = newVal;
    }
    return answer;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 6 ms 468 KB Output is correct
3 Correct 19 ms 820 KB Output is correct
4 Correct 21 ms 852 KB Output is correct
5 Correct 17 ms 864 KB Output is correct
6 Correct 14 ms 868 KB Output is correct
7 Correct 14 ms 868 KB Output is correct
8 Correct 14 ms 852 KB Output is correct
9 Correct 15 ms 864 KB Output is correct
10 Correct 15 ms 724 KB Output is correct
11 Correct 14 ms 728 KB Output is correct
12 Correct 15 ms 808 KB Output is correct
13 Correct 13 ms 724 KB Output is correct
14 Correct 13 ms 696 KB Output is correct
15 Correct 12 ms 724 KB Output is correct
16 Correct 11 ms 728 KB Output is correct
17 Correct 11 ms 760 KB Output is correct
18 Correct 11 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 6 ms 468 KB Output is correct
3 Correct 19 ms 820 KB Output is correct
4 Correct 21 ms 852 KB Output is correct
5 Correct 17 ms 864 KB Output is correct
6 Correct 14 ms 868 KB Output is correct
7 Correct 14 ms 868 KB Output is correct
8 Correct 14 ms 852 KB Output is correct
9 Correct 15 ms 864 KB Output is correct
10 Correct 15 ms 724 KB Output is correct
11 Correct 14 ms 728 KB Output is correct
12 Correct 15 ms 808 KB Output is correct
13 Correct 13 ms 724 KB Output is correct
14 Correct 13 ms 696 KB Output is correct
15 Correct 12 ms 724 KB Output is correct
16 Correct 11 ms 728 KB Output is correct
17 Correct 11 ms 760 KB Output is correct
18 Correct 11 ms 724 KB Output is correct
19 Correct 115 ms 2356 KB Output is correct
20 Correct 148 ms 2516 KB Output is correct
21 Correct 91 ms 2488 KB Output is correct
22 Correct 109 ms 2476 KB Output is correct
23 Correct 92 ms 2236 KB Output is correct
24 Correct 95 ms 2260 KB Output is correct
25 Correct 79 ms 2132 KB Output is correct
26 Correct 83 ms 2132 KB Output is correct
27 Correct 63 ms 2132 KB Output is correct
28 Correct 63 ms 2072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 222 ms 4036 KB Output is correct
2 Correct 958 ms 8844 KB Output is correct
3 Correct 2521 ms 13864 KB Output is correct
4 Correct 2428 ms 13880 KB Output is correct
5 Correct 1967 ms 13888 KB Output is correct
6 Correct 1921 ms 13668 KB Output is correct
7 Correct 1489 ms 13604 KB Output is correct
8 Correct 1540 ms 13680 KB Output is correct
9 Correct 1491 ms 13668 KB Output is correct
10 Correct 304 ms 8912 KB Output is correct
11 Correct 316 ms 8912 KB Output is correct
12 Correct 313 ms 9020 KB Output is correct
13 Correct 253 ms 8944 KB Output is correct
14 Correct 276 ms 8964 KB Output is correct
15 Correct 293 ms 8928 KB Output is correct
16 Correct 254 ms 8952 KB Output is correct
17 Correct 272 ms 9044 KB Output is correct
18 Correct 265 ms 9044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 6 ms 468 KB Output is correct
3 Correct 19 ms 820 KB Output is correct
4 Correct 21 ms 852 KB Output is correct
5 Correct 17 ms 864 KB Output is correct
6 Correct 14 ms 868 KB Output is correct
7 Correct 14 ms 868 KB Output is correct
8 Correct 14 ms 852 KB Output is correct
9 Correct 15 ms 864 KB Output is correct
10 Correct 15 ms 724 KB Output is correct
11 Correct 14 ms 728 KB Output is correct
12 Correct 15 ms 808 KB Output is correct
13 Correct 13 ms 724 KB Output is correct
14 Correct 13 ms 696 KB Output is correct
15 Correct 12 ms 724 KB Output is correct
16 Correct 11 ms 728 KB Output is correct
17 Correct 11 ms 760 KB Output is correct
18 Correct 11 ms 724 KB Output is correct
19 Correct 115 ms 2356 KB Output is correct
20 Correct 148 ms 2516 KB Output is correct
21 Correct 91 ms 2488 KB Output is correct
22 Correct 109 ms 2476 KB Output is correct
23 Correct 92 ms 2236 KB Output is correct
24 Correct 95 ms 2260 KB Output is correct
25 Correct 79 ms 2132 KB Output is correct
26 Correct 83 ms 2132 KB Output is correct
27 Correct 63 ms 2132 KB Output is correct
28 Correct 63 ms 2072 KB Output is correct
29 Correct 222 ms 4036 KB Output is correct
30 Correct 958 ms 8844 KB Output is correct
31 Correct 2521 ms 13864 KB Output is correct
32 Correct 2428 ms 13880 KB Output is correct
33 Correct 1967 ms 13888 KB Output is correct
34 Correct 1921 ms 13668 KB Output is correct
35 Correct 1489 ms 13604 KB Output is correct
36 Correct 1540 ms 13680 KB Output is correct
37 Correct 1491 ms 13668 KB Output is correct
38 Correct 304 ms 8912 KB Output is correct
39 Correct 316 ms 8912 KB Output is correct
40 Correct 313 ms 9020 KB Output is correct
41 Correct 253 ms 8944 KB Output is correct
42 Correct 276 ms 8964 KB Output is correct
43 Correct 293 ms 8928 KB Output is correct
44 Correct 254 ms 8952 KB Output is correct
45 Correct 272 ms 9044 KB Output is correct
46 Correct 265 ms 9044 KB Output is correct
47 Execution timed out 9020 ms 43464 KB Time limit exceeded
48 Halted 0 ms 0 KB -