답안 #338625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338625 2020-12-23T14:06:23 Z KoD Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3095 ms 55532 KB
#include <bits/stdc++.h>
 
template <class T>
using Vec = std::vector<T>;

constexpr int INF = 1000000000;

struct Mn {
    int value;
    static Mn id() {
        return Mn { -INF };
    }
    Mn operator + (const Mn &other) const {
        return Mn { std::max(value, other.value) };
    }
};

struct Ef {
    int add;
    static Ef id() {
        return Ef { 0 };
    }
    Ef operator + (const Ef &other) const {
        return Ef { add + other.add };
    }
};

Mn operator * (const Mn &m, const Ef &e) {
    return m.value == -INF ? m : Mn { m.value + e.add };
}

struct Segtree {
    struct Node {
        Mn m;
        Ef e;
    };

    int size;
    Vec<Node> data;

    Segtree(const int n) {
        for (size = 1; size < n; size <<= 1);
        data.resize(2 * size, Node { Mn::id(), Ef::id() });
    }
    
    static int bit_lsb(const int x) {
        return __builtin_ctz(x);
    }
    static int bit_width(const int x) {
        return 32 - __builtin_clz(x);
    }

    void apply(const int k, const Ef &e) {
        data[k].m = data[k].m * e;
        data[k].e = data[k].e + e;
    }
    void fetch(const int k) {
        data[k].m = data[k << 1 | 0].m + data[k << 1 | 1].m;
    }
    void flush(const int k) {
        apply(k << 1 | 0, data[k].e);
        apply(k << 1 | 1, data[k].e);
        data[k].e = Ef::id();
    }

    void push(const int k) {
        const int lsb = bit_lsb(k);
        for (int d = bit_width(k); d != lsb; --d) {
            flush(k >> d);
        }
    }
    void pull(int k) {
        k >>= bit_lsb(k);
        while (k > 1) {
            k >>= 1;
            fetch(k);
        }
    }

    void operate(int l, int r, int x) {
        l += size;
        r += size;
        const auto lc = l;
        const auto rc = r;
        push(l);
        push(r);
        const Ef e { x };
        while (l < r) {
            if (l & 1) {
                apply(l, e);
                l += 1;
            }
            l >>= 1;
            if (r & 1) {
                r -= 1;
                apply(r, e);
            }
            r >>= 1;
        }
        pull(lc);
        pull(rc);
    }

    void assign(int i, int x) {
        i += size;
        push(i);
        push(i + 1);
        data[i].m = Mn { x };
        pull(i);
        pull(i + 1);
    }

    int fold() const {
        return data[1].m.value;
    }

    int fold(int l, int r) {
        l += size;
        r += size;
        push(l);
        push(r);
        Mn ret = Mn::id();
        while (l < r) {
            if (l & 1) {
                ret = ret + data[l].m;
                l += 1;
            }
            l >>= 1;
            if (r & 1) {
                r -= 1;
                ret = ret + data[r].m;
            }
            r >>= 1;
        }
        return ret.value;
    }
};

struct Fenwick {
    Vec<int> data;
    Fenwick(const int n): data(n + 1) { }

    void add(int i, int x) {
        for (i += 1; i < (int) data.size(); i += i & -i) {
            data[i] += x;
        }
    }

    int get(int i) {
        int ret = 0;
        for (; i > 0; i -= i & -i) {
            ret += data[i];
        }
        return ret;
    }
};

Vec<int> countScans(Vec<int> A, Vec<int> X, Vec<int> V) {
    const int N = A.size();
    const int Q = X.size();
    Vec<std::pair<int, int>> cmp;
    cmp.reserve(N + Q);
    for (int i = 0; i < N; ++i) {
        cmp.emplace_back(A[i], i);
    }
    for (int i = 0; i < Q; ++i) {
        cmp.emplace_back(V[i], X[i]);
    }
    std::sort(cmp.begin(), cmp.end());
    cmp.erase(std::unique(cmp.begin(), cmp.end()), cmp.end());
    const auto index = [&](const int x, const int i) {
        return std::lower_bound(cmp.begin(), cmp.end(), std::make_pair(x, i)) - cmp.begin();  
    };
    const int Size = cmp.size();
    Segtree seg(Size);
    Fenwick fen(Size);
    for (int i = 0; i < N; ++i) {
        fen.add(index(A[i], i), 1);
    }
    for (int i = 0; i < N; ++i) {
        seg.assign(index(A[i], i), i - fen.get(index(A[i] + 1, 0)));
    }
    Vec<int> ret(Q);
    for (int i = 0; i < Q; ++i) {
        fen.add(index(A[X[i]], X[i]), -1);
        seg.operate(index(A[X[i]], 0), Size, 1);
        seg.assign(index(A[X[i]], X[i]), -INF);
        fen.add(index(V[i], X[i]), 1);
        seg.operate(index(V[i], 0), Size, -1);
        seg.assign(index(V[i], X[i]), X[i] - fen.get(index(V[i] + 1, 0)));
        ret[i] = seg.fold() + 1;
        A[X[i]] = V[i];
    }
    return ret;
}
 
#ifdef __APPLE__
int main() {
    int N, Q;
    std::cin >> N >> Q;
    Vec<int> A(N);
    Vec<int> X(Q), V(Q);
    for (auto &x: A) {
        std::cin >> x;
    }
    for (int i = 0; i < Q; ++i) {
        std::cin >> X[i] >> V[i];
    }
    for (auto x: countScans(A, X, V)) {
        std::cout << x << '\n';
    }
    return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 6 ms 492 KB Output is correct
4 Correct 6 ms 492 KB Output is correct
5 Correct 6 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 6 ms 492 KB Output is correct
8 Correct 6 ms 492 KB Output is correct
9 Correct 6 ms 492 KB Output is correct
10 Correct 6 ms 492 KB Output is correct
11 Correct 6 ms 492 KB Output is correct
12 Correct 6 ms 492 KB Output is correct
13 Correct 6 ms 492 KB Output is correct
14 Correct 6 ms 492 KB Output is correct
15 Correct 6 ms 492 KB Output is correct
16 Correct 6 ms 492 KB Output is correct
17 Correct 6 ms 492 KB Output is correct
18 Correct 6 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 6 ms 492 KB Output is correct
4 Correct 6 ms 492 KB Output is correct
5 Correct 6 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 6 ms 492 KB Output is correct
8 Correct 6 ms 492 KB Output is correct
9 Correct 6 ms 492 KB Output is correct
10 Correct 6 ms 492 KB Output is correct
11 Correct 6 ms 492 KB Output is correct
12 Correct 6 ms 492 KB Output is correct
13 Correct 6 ms 492 KB Output is correct
14 Correct 6 ms 492 KB Output is correct
15 Correct 6 ms 492 KB Output is correct
16 Correct 6 ms 492 KB Output is correct
17 Correct 6 ms 492 KB Output is correct
18 Correct 6 ms 492 KB Output is correct
19 Correct 23 ms 876 KB Output is correct
20 Correct 26 ms 1004 KB Output is correct
21 Correct 25 ms 1132 KB Output is correct
22 Correct 25 ms 1132 KB Output is correct
23 Correct 25 ms 1132 KB Output is correct
24 Correct 25 ms 1132 KB Output is correct
25 Correct 25 ms 1132 KB Output is correct
26 Correct 25 ms 1132 KB Output is correct
27 Correct 24 ms 1132 KB Output is correct
28 Correct 24 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 1388 KB Output is correct
2 Correct 104 ms 3308 KB Output is correct
3 Correct 196 ms 5484 KB Output is correct
4 Correct 199 ms 5484 KB Output is correct
5 Correct 193 ms 5612 KB Output is correct
6 Correct 196 ms 5484 KB Output is correct
7 Correct 194 ms 5612 KB Output is correct
8 Correct 194 ms 5484 KB Output is correct
9 Correct 190 ms 5612 KB Output is correct
10 Correct 148 ms 4332 KB Output is correct
11 Correct 153 ms 4332 KB Output is correct
12 Correct 154 ms 4460 KB Output is correct
13 Correct 155 ms 4412 KB Output is correct
14 Correct 156 ms 4332 KB Output is correct
15 Correct 157 ms 4460 KB Output is correct
16 Correct 150 ms 4332 KB Output is correct
17 Correct 158 ms 4332 KB Output is correct
18 Correct 155 ms 4332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 6 ms 492 KB Output is correct
4 Correct 6 ms 492 KB Output is correct
5 Correct 6 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 6 ms 492 KB Output is correct
8 Correct 6 ms 492 KB Output is correct
9 Correct 6 ms 492 KB Output is correct
10 Correct 6 ms 492 KB Output is correct
11 Correct 6 ms 492 KB Output is correct
12 Correct 6 ms 492 KB Output is correct
13 Correct 6 ms 492 KB Output is correct
14 Correct 6 ms 492 KB Output is correct
15 Correct 6 ms 492 KB Output is correct
16 Correct 6 ms 492 KB Output is correct
17 Correct 6 ms 492 KB Output is correct
18 Correct 6 ms 492 KB Output is correct
19 Correct 23 ms 876 KB Output is correct
20 Correct 26 ms 1004 KB Output is correct
21 Correct 25 ms 1132 KB Output is correct
22 Correct 25 ms 1132 KB Output is correct
23 Correct 25 ms 1132 KB Output is correct
24 Correct 25 ms 1132 KB Output is correct
25 Correct 25 ms 1132 KB Output is correct
26 Correct 25 ms 1132 KB Output is correct
27 Correct 24 ms 1132 KB Output is correct
28 Correct 24 ms 1132 KB Output is correct
29 Correct 27 ms 1388 KB Output is correct
30 Correct 104 ms 3308 KB Output is correct
31 Correct 196 ms 5484 KB Output is correct
32 Correct 199 ms 5484 KB Output is correct
33 Correct 193 ms 5612 KB Output is correct
34 Correct 196 ms 5484 KB Output is correct
35 Correct 194 ms 5612 KB Output is correct
36 Correct 194 ms 5484 KB Output is correct
37 Correct 190 ms 5612 KB Output is correct
38 Correct 148 ms 4332 KB Output is correct
39 Correct 153 ms 4332 KB Output is correct
40 Correct 154 ms 4460 KB Output is correct
41 Correct 155 ms 4412 KB Output is correct
42 Correct 156 ms 4332 KB Output is correct
43 Correct 157 ms 4460 KB Output is correct
44 Correct 150 ms 4332 KB Output is correct
45 Correct 158 ms 4332 KB Output is correct
46 Correct 155 ms 4332 KB Output is correct
47 Correct 725 ms 19848 KB Output is correct
48 Correct 2896 ms 52460 KB Output is correct
49 Correct 3095 ms 55160 KB Output is correct
50 Correct 3053 ms 55116 KB Output is correct
51 Correct 3041 ms 55276 KB Output is correct
52 Correct 3021 ms 55144 KB Output is correct
53 Correct 3035 ms 55148 KB Output is correct
54 Correct 2741 ms 55292 KB Output is correct
55 Correct 2922 ms 55532 KB Output is correct
56 Correct 2787 ms 55528 KB Output is correct
57 Correct 2895 ms 55376 KB Output is correct
58 Correct 2758 ms 55280 KB Output is correct
59 Correct 2635 ms 53596 KB Output is correct
60 Correct 2653 ms 53476 KB Output is correct
61 Correct 2655 ms 53588 KB Output is correct
62 Correct 2537 ms 53160 KB Output is correct
63 Correct 2550 ms 53132 KB Output is correct
64 Correct 2506 ms 53204 KB Output is correct
65 Correct 2416 ms 52788 KB Output is correct
66 Correct 2393 ms 52912 KB Output is correct
67 Correct 2389 ms 52872 KB Output is correct