Submission #402276

# Submission time Handle Problem Language Result Execution time Memory
402276 2021-05-11T13:56:44 Z KoD Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
2836 ms 73188 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T, T Div = 2> constexpr T INFTY = std::numeric_limits<T>::max() / Div;

class revrep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr revrep(const usize first, const usize last) noexcept
        : first(last - 1), last(std::min(first, last) - 1) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr u64 ceil_log2(const u64 x) {
    u64 e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
}

constexpr u64 bit_rzeros(const u64 x) { return x == 0 ? 64 : __builtin_ctzll(x); }

template <class Monoid, class Effector> class LazySegmentTree {
    using M = Monoid;
    using E = Effector;
    usize internal_size, logn, seg_size;
    std::vector<M> data;
    std::vector<E> lazy;

    void fetch(const usize k) { data[k] = data[2 * k] + data[2 * k + 1]; }
    void apply(const usize k, const E& e) {
        data[k] = data[k] * e;
        if (k < seg_size) lazy[k] = lazy[k] * e;
    }
    void flush(const usize k) {
        apply(2 * k, lazy[k]);
        apply(2 * k + 1, lazy[k]);
        lazy[k] = E::one();
    }

    void push(const usize k) {
        for (const usize d : revrep(bit_rzeros(k) + 1, logn + 1)) flush(k >> d);
    }
    void pull(usize k) {
        for (k >>= bit_rzeros(k); k > 1;) fetch(k >>= 1);
    }

  public:
    explicit LazySegmentTree(const usize size = 0, const M& value = M::zero())
        : LazySegmentTree(std::vector<M>(size, value)) {}
    explicit LazySegmentTree(const std::vector<M>& vec) : internal_size(vec.size()) {
        logn = ceil_log2(internal_size);
        seg_size = 1 << logn;
        data = std::vector<M>(2 * seg_size, M::zero());
        lazy = std::vector<E>(seg_size, E::one());
        for (const usize i : rep(0, internal_size)) data[seg_size + i] = vec[i];
        for (const usize i : revrep(1, seg_size)) fetch(i);
    }

    usize size() const { return internal_size; }

    void assign(usize i, const M& value) {
        assert(i < internal_size);
        i += seg_size;
        for (const usize d : revrep(1, logn + 1)) flush(i >> d);
        data[i] = value;
        for (const usize d : rep(1, logn + 1)) fetch(i >> d);
    }
    void operate(usize l, usize r, const E& e) {
        assert(l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        push(l);
        push(r);
        for (usize l0 = l, r0 = r; l0 < r0; l0 >>= 1, r0 >>= 1) {
            if (l0 & 1) apply(l0++, e);
            if (r0 & 1) apply(--r0, e);
        }
        pull(l);
        pull(r);
    }

    M fold() const { return data[1]; }
    M fold(usize l, usize r) {
        assert(l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        push(l);
        push(r);
        M ret_l = M::zero(), ret_r = M::zero();
        while (l < r) {
            if (l & 1) ret_l = ret_l + data[l++];
            if (r & 1) ret_r = data[--r] + ret_r;
            l >>= 1;
            r >>= 1;
        }
        return ret_l + ret_r;
    }

    template <class F> usize max_right(usize l, const F& f) {
        assert(l <= internal_size);
        assert(f(M::zero()));
        if (l == internal_size) return internal_size;
        l += seg_size;
        for (const usize d : revrep(1, logn + 1)) flush(l >> d);
        M sum = M::zero();
        do {
            while (!(l & 1)) l >>= 1;
            if (!f(sum + data[l])) {
                while (l < seg_size) {
                    flush(l);
                    l = 2 * l;
                    if (f(sum + data[l])) sum = sum + data[l++];
                }
                return l - seg_size;
            }
            sum = sum + data[l++];
        } while ((l & -l) != l);
        return internal_size;
    }

    template <class F> usize min_left(usize r, const F& f) {
        assert(r <= internal_size);
        assert(f(M::zero()));
        if (r == 0) return 0;
        r += seg_size;
        for (const usize d : revrep(1, logn + 1)) flush((r - 1) >> d);
        M sum = M::zero();
        do {
            r -= 1;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!f(data[r] + sum)) {
                while (r < seg_size) {
                    flush(r);
                    r = 2 * r + 1;
                    if (f(data[r] + sum)) sum = data[r--] + sum;
                }
                return r + 1 - seg_size;
            }
            sum = data[r] + sum;
        } while ((r & -r) != r);
        return 0;
    }
};

template <class T> class FenwickTree {
    usize logn;
    std::vector<T> data;

  public:
    explicit FenwickTree(const usize size = 0) {
        logn = ceil_log2(size + 1) - 1;
        data = std::vector<T>(size + 1, T(0));
    }

    usize size() const { return data.size() - 1; }

    void add(usize i, const T& x) {
        assert(i < size());
        i += 1;
        while (i < data.size()) {
            data[i] += x;
            i += i & -i;
        }
    }
    void subtract(usize i, const T& x) {
        assert(i < size());
        i += 1;
        while (i < data.size()) {
            data[i] -= x;
            i += i & -i;
        }
    }

    T fold(usize l, usize r) const {
        assert(l <= r and r <= size());
        T ret(0);
        while (l < r) {
            ret += data[r];
            r -= r & -r;
        }
        while (r < l) {
            ret -= data[l];
            l -= l & -l;
        }
        return ret;
    }

    template <class F> usize max_right(const F& f) const {
        assert(f(T(0)));
        usize i = 0;
        T sum(0);
        for (usize k = (1 << logn); k > 0; k >>= 1) {
            if (i + k <= size() && f(sum + data[i + k])) {
                i += k;
                sum += data[i];
            }
        }
        return i;
    }
};

template <class T> using Vec = std::vector<T>;

template <class T> isize lowb(const Vec<T>& vec, const T& x) {
    return std::lower_bound(vec.cbegin(), vec.cend(), x) - vec.cbegin();
}

struct Mn {
    isize max;
    static Mn zero() { return Mn{-INFTY<isize>}; }
    Mn operator+(const Mn other) const { return Mn{std::max(max, other.max)}; }
};

struct Ef {
    isize add;
    static Ef one() { return Ef{0}; }
    Ef operator*(const Ef& other) const { return Ef{add + other.add}; }
};

Mn operator*(const Mn& m, const Ef& e) { return Mn{m.max + e.add}; }

Vec<int> countScans(Vec<int> A, Vec<int> X, Vec<int> V) {
    const auto N = A.size();
    const auto Q = X.size();
    Vec<std::pair<int, int>> cmp;
    cmp.reserve(N + Q);
    for (const auto i : rep(0, N)) {
        cmp.emplace_back(A[i], i);
    }
    for (const auto i : rep(0, Q)) {
        cmp.emplace_back(V[i], X[i]);
    }
    std::sort(cmp.begin(), cmp.end());
    const auto L = cmp.size();
    FenwickTree<isize> fen(L);
    for (const auto i : rep(0, N)) {
        fen.add(lowb(cmp, std::pair<int, int>(A[i], i)), 1);
    }
    LazySegmentTree<Mn, Ef> seg(L);
    for (const auto i : rep(0, N)) {
        const auto c = fen.fold(0, lowb(cmp, std::pair<int, int>(A[i] + 1, 0)));
        seg.assign(lowb(cmp, std::pair<int, int>(A[i], i)), Mn{(int)i + 1 - c});
    }
    Vec<int> ret(Q);
    for (const auto q : rep(0, Q)) {
        const auto i = X[q];
        const auto x = V[q];
        fen.add(lowb(cmp, std::pair<int, int>(A[i], i)), -1);
        seg.operate(lowb(cmp, std::pair<int, int>(A[i], 0)), L, Ef{1});
        seg.assign(lowb(cmp, std::pair<int, int>(A[i], i)), Mn::zero());
        A[i] = x;
        fen.add(lowb(cmp, std::pair<int, int>(A[i], i)), 1);
        const auto c = fen.fold(0, lowb(cmp, std::pair<int, int>(A[i] + 1, 0)));
        seg.operate(lowb(cmp, std::pair<int, int>(A[i], 0)), L, Ef{-1});
        seg.assign(lowb(cmp, std::pair<int, int>(A[i], i)), Mn{i + 1 - c});
        ret[q] = seg.fold().max;
    }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 5 ms 472 KB Output is correct
5 Correct 5 ms 572 KB Output is correct
6 Correct 5 ms 460 KB Output is correct
7 Correct 6 ms 560 KB Output is correct
8 Correct 5 ms 524 KB Output is correct
9 Correct 6 ms 560 KB Output is correct
10 Correct 5 ms 460 KB Output is correct
11 Correct 5 ms 460 KB Output is correct
12 Correct 5 ms 460 KB Output is correct
13 Correct 7 ms 568 KB Output is correct
14 Correct 5 ms 460 KB Output is correct
15 Correct 5 ms 460 KB Output is correct
16 Correct 5 ms 460 KB Output is correct
17 Correct 5 ms 460 KB Output is correct
18 Correct 5 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 5 ms 472 KB Output is correct
5 Correct 5 ms 572 KB Output is correct
6 Correct 5 ms 460 KB Output is correct
7 Correct 6 ms 560 KB Output is correct
8 Correct 5 ms 524 KB Output is correct
9 Correct 6 ms 560 KB Output is correct
10 Correct 5 ms 460 KB Output is correct
11 Correct 5 ms 460 KB Output is correct
12 Correct 5 ms 460 KB Output is correct
13 Correct 7 ms 568 KB Output is correct
14 Correct 5 ms 460 KB Output is correct
15 Correct 5 ms 460 KB Output is correct
16 Correct 5 ms 460 KB Output is correct
17 Correct 5 ms 460 KB Output is correct
18 Correct 5 ms 460 KB Output is correct
19 Correct 19 ms 1228 KB Output is correct
20 Correct 21 ms 1448 KB Output is correct
21 Correct 20 ms 1452 KB Output is correct
22 Correct 21 ms 1448 KB Output is correct
23 Correct 20 ms 1356 KB Output is correct
24 Correct 25 ms 1356 KB Output is correct
25 Correct 20 ms 1412 KB Output is correct
26 Correct 20 ms 1356 KB Output is correct
27 Correct 20 ms 1324 KB Output is correct
28 Correct 20 ms 1412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1904 KB Output is correct
2 Correct 87 ms 4292 KB Output is correct
3 Correct 183 ms 7448 KB Output is correct
4 Correct 159 ms 7432 KB Output is correct
5 Correct 155 ms 7388 KB Output is correct
6 Correct 164 ms 7396 KB Output is correct
7 Correct 173 ms 7384 KB Output is correct
8 Correct 175 ms 7492 KB Output is correct
9 Correct 150 ms 7364 KB Output is correct
10 Correct 130 ms 7528 KB Output is correct
11 Correct 126 ms 7532 KB Output is correct
12 Correct 145 ms 7492 KB Output is correct
13 Correct 128 ms 7460 KB Output is correct
14 Correct 132 ms 7524 KB Output is correct
15 Correct 127 ms 7512 KB Output is correct
16 Correct 129 ms 7540 KB Output is correct
17 Correct 129 ms 7492 KB Output is correct
18 Correct 135 ms 7488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 5 ms 472 KB Output is correct
5 Correct 5 ms 572 KB Output is correct
6 Correct 5 ms 460 KB Output is correct
7 Correct 6 ms 560 KB Output is correct
8 Correct 5 ms 524 KB Output is correct
9 Correct 6 ms 560 KB Output is correct
10 Correct 5 ms 460 KB Output is correct
11 Correct 5 ms 460 KB Output is correct
12 Correct 5 ms 460 KB Output is correct
13 Correct 7 ms 568 KB Output is correct
14 Correct 5 ms 460 KB Output is correct
15 Correct 5 ms 460 KB Output is correct
16 Correct 5 ms 460 KB Output is correct
17 Correct 5 ms 460 KB Output is correct
18 Correct 5 ms 460 KB Output is correct
19 Correct 19 ms 1228 KB Output is correct
20 Correct 21 ms 1448 KB Output is correct
21 Correct 20 ms 1452 KB Output is correct
22 Correct 21 ms 1448 KB Output is correct
23 Correct 20 ms 1356 KB Output is correct
24 Correct 25 ms 1356 KB Output is correct
25 Correct 20 ms 1412 KB Output is correct
26 Correct 20 ms 1356 KB Output is correct
27 Correct 20 ms 1324 KB Output is correct
28 Correct 20 ms 1412 KB Output is correct
29 Correct 24 ms 1904 KB Output is correct
30 Correct 87 ms 4292 KB Output is correct
31 Correct 183 ms 7448 KB Output is correct
32 Correct 159 ms 7432 KB Output is correct
33 Correct 155 ms 7388 KB Output is correct
34 Correct 164 ms 7396 KB Output is correct
35 Correct 173 ms 7384 KB Output is correct
36 Correct 175 ms 7492 KB Output is correct
37 Correct 150 ms 7364 KB Output is correct
38 Correct 130 ms 7528 KB Output is correct
39 Correct 126 ms 7532 KB Output is correct
40 Correct 145 ms 7492 KB Output is correct
41 Correct 128 ms 7460 KB Output is correct
42 Correct 132 ms 7524 KB Output is correct
43 Correct 127 ms 7512 KB Output is correct
44 Correct 129 ms 7540 KB Output is correct
45 Correct 129 ms 7492 KB Output is correct
46 Correct 135 ms 7488 KB Output is correct
47 Correct 617 ms 26656 KB Output is correct
48 Correct 2556 ms 69124 KB Output is correct
49 Correct 2754 ms 72984 KB Output is correct
50 Correct 2777 ms 73016 KB Output is correct
51 Correct 2735 ms 72980 KB Output is correct
52 Correct 2727 ms 73036 KB Output is correct
53 Correct 2670 ms 73016 KB Output is correct
54 Correct 2499 ms 73188 KB Output is correct
55 Correct 2836 ms 73144 KB Output is correct
56 Correct 2616 ms 73116 KB Output is correct
57 Correct 2729 ms 73124 KB Output is correct
58 Correct 2505 ms 73128 KB Output is correct
59 Correct 2278 ms 71744 KB Output is correct
60 Correct 2291 ms 71856 KB Output is correct
61 Correct 2276 ms 71764 KB Output is correct
62 Correct 2182 ms 71620 KB Output is correct
63 Correct 2148 ms 71756 KB Output is correct
64 Correct 2119 ms 71628 KB Output is correct
65 Correct 2001 ms 71508 KB Output is correct
66 Correct 2017 ms 71536 KB Output is correct
67 Correct 2045 ms 71508 KB Output is correct