結果

問題 No.1558 Derby Live
ユーザー scol_kpscol_kp
提出日時 2021-06-25 22:26:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 8,063 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 211,684 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-25 08:32:50
合計ジャッジ時間 7,329 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
6,812 KB
testcase_01 AC 106 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 66 ms
6,940 KB
testcase_04 AC 29 ms
6,940 KB
testcase_05 AC 30 ms
6,944 KB
testcase_06 AC 56 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 54 ms
6,944 KB
testcase_09 AC 58 ms
6,944 KB
testcase_10 AC 60 ms
6,940 KB
testcase_11 AC 62 ms
6,940 KB
testcase_12 AC 65 ms
6,944 KB
testcase_13 AC 70 ms
6,944 KB
testcase_14 AC 72 ms
6,944 KB
testcase_15 AC 77 ms
6,944 KB
testcase_16 AC 79 ms
6,944 KB
testcase_17 AC 84 ms
6,940 KB
testcase_18 AC 88 ms
6,940 KB
testcase_19 AC 91 ms
6,944 KB
testcase_20 AC 95 ms
6,944 KB
testcase_21 AC 89 ms
6,940 KB
testcase_22 AC 91 ms
6,940 KB
testcase_23 AC 94 ms
6,940 KB
testcase_24 AC 87 ms
6,940 KB
testcase_25 AC 93 ms
6,940 KB
testcase_26 AC 94 ms
6,940 KB
testcase_27 AC 88 ms
6,944 KB
testcase_28 AC 91 ms
6,944 KB
testcase_29 AC 96 ms
6,944 KB
testcase_30 AC 88 ms
6,944 KB
testcase_31 AC 91 ms
6,940 KB
testcase_32 AC 95 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();

template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <std::ostream& os = std::cout>
class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
#if __cplusplus >= 201703L
        auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) {
#else
        void operator()(const Args&... args) const {
#endif
            print(args...);
        }
        template <typename T>
#if __cplusplus >= 201703L
        auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
#else
        void pvec(const T& vec, size_t sz) const {
#endif
            for (size_t i = 0; i < sz; i++)
                os << vec[i] << (i == sz - 1 ? term : sep);
        }
        template <typename T>
#if __cplusplus >= 201703L
        auto pmat(const T& mat, size_t h, size_t w) const -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
#else
        void pmat(const T& mat, size_t h, size_t w) const {
#endif
            for (size_t i = 0; i < h; i++)
                for (size_t j = 0; j < w; j++)
                    os << mat[i][j] << (j == w - 1 ? term : sep);
        }

    private:
        const char *sep, *term;
        void print() const { os << term; }
        void print_rest() const { os << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints<> prints;
Prints<std::cerr> prints_err;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

template <class T, class Op>
class SegmentTree {
private:
    const int n;
    const Op op;
    const T id;
    std::vector<T> vec;
    int sz;

public:
    explicit SegmentTree(int n, Op op, T id) noexcept
        : n(n), op(op), id(id) {
        sz = 1;
        while (sz < n) sz <<= 1;
        vec.assign(sz << 1, id);
    }
    explicit SegmentTree(const std::vector<T>& vec, Op op, T id) noexcept
        : n(vec.size()), op(op), id(id) {
        sz = 1;
        while (sz < n) sz <<= 1;
        this->vec.assign(sz << 1, id);
        set_array(vec);
    }
    T& operator[](int idx) noexcept {
        return vec[idx + sz];
    }
    void set_value(int idx, T val) noexcept {
        vec[idx + sz] = val;
    }
    template <class RandomIt>
    void set_array(RandomIt _begin, RandomIt _end) noexcept {
        std::copy(_begin, _end, vec.begin() + sz);
    }
    template <class Vec>
    void set_array(const Vec& v) noexcept {
        set_array(std::begin(v), std::end(v));
    }
    void build() noexcept {
        for (int i = sz - 1; i > 0; i--) {
            vec[i] = op(vec[i << 1], vec[(i << 1) | 1]);
        }
    }
    void update(int idx, T val) noexcept {
        idx += sz;
        vec[idx] = val;
        for (idx >>= 1; idx > 0; idx >>= 1) {
            vec[idx] = op(vec[idx << 1], vec[(idx << 1) | 1]);
        }
    }
    T query(int l, int r) const noexcept {
        T l_val = id, r_val = id;
        l += sz, r += sz - 1;
        for (; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) l_val = op(l_val, vec[l++]);
            if (!(r & 1)) r_val = op(vec[r--], r_val);
        }
        return op(l_val, r_val);
    }
    T query_all() const noexcept {
        return query(0, sz);
    }
    // Return the largest x such that check(A[idx] op ... op A[x - 1]) == true
    // complexity: O(log (n))
    template <class F>
    int max_right(int idx, const F& check) const noexcept {
        T acc = id;
        return idx < n ? _max_right(idx, check, acc, 1, 0, sz) : n;
    }
    // Return the smallest x such that check(A[x] op ... op A[idx - 1]) == true
    // complexity: O(log (n))
    template <class F>
    int min_left(int idx, const F& check) const noexcept {
        T acc = id;
        return idx > 0 ? _min_left(idx, check, acc, 1, 0, sz) : 0;
    }
    void reset() noexcept {
        std::fill(vec.begin(), vec.end(), id);
    }

private:
    template <class F>
    int _max_right(int idx, const F& check, T& acc, int k, int l, int r) const noexcept {
        if (l + 1 == r) {
            acc = op(acc, vec[k]);
            return check(acc) ? n : k - sz;
        }
        const int mid = (l + r) >> 1;
        if (mid <= idx) return _max_right(idx, check, acc, (k << 1) | 1, mid, r);
        if (idx <= l) {
            T tmp = op(acc, vec[k]);
            if (check(tmp)) {
                acc = tmp;
                return n;
            }
        }
        const int vl = _max_right(idx, check, acc, k << 1, l, mid);
        if (vl < n) return vl;
        return _max_right(idx, check, acc, (k << 1) | 1, mid, r);
    }
    template <class F>
    int _min_left(int idx, const F& check, T& acc, int k, int l, int r) const noexcept {
        if (l + 1 == r) {
            acc = op(acc, vec[k]);
            return check(acc) ? 0 : k - sz + 1;
        }
        const int mid = (l + r) >> 1;
        if (mid >= idx) return _min_left(idx, check, acc, k << 1, l, mid);
        if (idx >= r) {
            T tmp = op(acc, vec[k]);
            if (check(tmp)) {
                acc = tmp;
                return 0;
            }
        }
        const int vr = _min_left(idx, check, acc, (k << 1) | 1, mid, r);
        if (vr > 0) return vr;
        return _min_left(idx, check, acc, k << 1, l, mid);
    }
};

void solve() {
    ll N, M, Q;
    cin >> N >> M >> Q;
    auto op = [&](const Vi& l, const Vi& r) -> Vi {
        Vi res(N);
        for (ll i = 0; i < N; i++) {
            res[i] = r[l[i]];
        }
        return res;
    };
    Vi e(N);
    iota(e.begin(), e.end(), 0);
    SegmentTree seg(M, op, e);
    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int k;
            cin >> k;
            --k;
            Vi p(N);
            for (ll i = 0; i < N; i++) {
                cin >> p[i];
                --p[i];
            }
            seg.update(k, p);
        } else if (type == 2) {
            int k;
            cin >> k;
            Vi q = seg.query(0, k);
            Vi res(N);
            for (ll i = 0; i < N; i++) {
                res[q[i]] = i;
            }
            for (ll i = 0; i < N; i++) {
                cout << res[i] + 1 << " ";
            }
            prints()();
        } else {
            int l, r;
            cin >> l >> r;
            --l;
            Vi q = seg.query(l, r);
            ll res = 0;
            for (ll i = 0; i < N; i++) {
                res += abs(i - q[i]);
            }
            prints()(res);
        }
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    std::ifstream ifs("./in_out/input.txt");
    std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    std::ofstream ofs("./in_out/output.txt");
    std::cout.rdbuf(ofs.rdbuf());
#endif
    std::cout << std::setprecision(18) << std::fixed;
    solve();
    std::cout << std::flush;
    return 0;
}
0