提出 #51107493


ソースコード 拡げる

#include <bits/stdc++.h>
#include <atcoder/modint>

namespace {
    using ModInt [[maybe_unused]] = atcoder::modint998244353;
    using Num [[maybe_unused]] = long long int;
    using Vec [[maybe_unused]] = std::vector<Num>;
    using Set [[maybe_unused]] = std::set<Num>;
    using Mset [[maybe_unused]] = std::multiset<Num>;
    using Edges [[maybe_unused]] = std::vector<std::vector<Num>>;

    template<typename T>
    using Q [[maybe_unused]] = std::queue<T>;

    template<typename T>
    using PQ [[maybe_unused]] = std::priority_queue<T, std::vector<T>, std::greater<T>>;
}

void solve(std::istream& is, std::ostream& os) {
    Num n {0};
    is >> n;

    constexpr Num Inf = 1000000000000000000LL;
    struct Node {
        Num prev {-Inf};
        Num next {Inf};
    };

    Vec ns(n);
    for(auto&& a : ns) {
        is >> a;
    }

    std::map<Num, Node> lst;
    Node left_most {-Inf, ns.at(0)};
    Node right_most {ns.back(), Inf};
    lst[-Inf] = left_most;
    lst[Inf] = right_most;

    if (n == 1) {
        Node node;
        lst[ns.at(0)] = node;
    } else {
        for(Num i{0}; i<n; ++i) {
            Node node;
            if (i == 0) {
                node.next = ns.at(i+1);
            } else if ((i+1) == n) {
                node.prev = ns.at(i-1);
            } else {
                node.prev = ns.at(i-1);
                node.next = ns.at(i+1);
            }
            lst[ns.at(i)] = node;
        }
    }

    Num q {0};
    is >> q;
    while(q-- > 0) {
        Num cmd;
        is >> cmd;

        if (cmd == 1) {
            Num x, y;
            is >> x >> y;

            const auto next = lst[x].next;
            Node node {x, next};
            lst[y] = node;
            lst[x].next = y;
            lst[next].prev = y;
        } else {
            Num x;
            is >> x;

            const auto prev = lst[x].prev;
            const auto next = lst[x].next;
            lst[prev].next = next;
            lst[next].prev = prev;
            lst.erase(x);
        }
    }

    Vec ans;
    Num cur = -Inf;
    for(;;) {
        if (cur == Inf) {
            break;
        }

        if (cur != -Inf) {
            ans.push_back(cur);
        }

        cur = lst[cur].next;
    }

    const auto size = ans.size();
    for(size_t i{0}; i<size; ++i) {
        os << ans.at(i) << (((i+1)==size) ? '\n' : ' ');
    }
}

int main(void) {
    solve(std::cin, std::cout);
    return 0;
}

提出情報

提出日時
問題 E - Insert or Erase
ユーザ zettsut
言語 C++ 20 (gcc 12.2)
得点 475
コード長 2550 Byte
結果 AC
実行時間 1003 ms
メモリ 33836 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 475 / 475
結果
AC × 2
AC × 24
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
min.txt AC 1 ms 3408 KiB
random_01.txt AC 1003 ms 32812 KiB
random_02.txt AC 667 ms 21324 KiB
random_03.txt AC 693 ms 27988 KiB
random_04.txt AC 215 ms 10688 KiB
random_05.txt AC 748 ms 19248 KiB
random_06.txt AC 409 ms 9952 KiB
random_07.txt AC 628 ms 19236 KiB
random_08.txt AC 235 ms 10424 KiB
random_09.txt AC 561 ms 17684 KiB
random_10.txt AC 215 ms 8892 KiB
random_11.txt AC 488 ms 17884 KiB
random_12.txt AC 43 ms 4044 KiB
random_13.txt AC 732 ms 32672 KiB
random_14.txt AC 477 ms 19340 KiB
random_15.txt AC 395 ms 17204 KiB
random_16.txt AC 712 ms 32708 KiB
random_17.txt AC 471 ms 19248 KiB
random_18.txt AC 369 ms 17124 KiB
random_19.txt AC 146 ms 11252 KiB
random_20.txt AC 305 ms 33836 KiB
random_21.txt AC 153 ms 17208 KiB
sample_01.txt AC 1 ms 3452 KiB
sample_02.txt AC 1 ms 3552 KiB