結果

問題 No.1170 Never Want to Walk
ユーザー kcvlexkcvlex
提出日時 2020-08-21 14:58:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 823 ms / 2,000 ms
コード長 9,259 bytes
コンパイル時間 2,028 ms
コンパイル使用メモリ 163,152 KB
実行使用メモリ 296,148 KB
最終ジャッジ日時 2024-10-15 00:32:10
合計ジャッジ時間 12,405 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,824 KB
testcase_12 AC 3 ms
6,816 KB
testcase_13 AC 3 ms
6,816 KB
testcase_14 AC 3 ms
6,820 KB
testcase_15 AC 3 ms
6,820 KB
testcase_16 AC 3 ms
6,816 KB
testcase_17 AC 4 ms
6,820 KB
testcase_18 AC 3 ms
6,820 KB
testcase_19 AC 3 ms
6,816 KB
testcase_20 AC 4 ms
6,816 KB
testcase_21 AC 3 ms
6,816 KB
testcase_22 AC 5 ms
6,816 KB
testcase_23 AC 4 ms
6,816 KB
testcase_24 AC 4 ms
6,816 KB
testcase_25 AC 4 ms
6,816 KB
testcase_26 AC 4 ms
6,816 KB
testcase_27 AC 422 ms
116,416 KB
testcase_28 AC 405 ms
113,496 KB
testcase_29 AC 383 ms
105,100 KB
testcase_30 AC 389 ms
111,752 KB
testcase_31 AC 388 ms
113,640 KB
testcase_32 AC 761 ms
283,776 KB
testcase_33 AC 823 ms
244,716 KB
testcase_34 AC 769 ms
247,092 KB
testcase_35 AC 807 ms
296,148 KB
testcase_36 AC 774 ms
272,884 KB
testcase_37 AC 821 ms
273,788 KB
testcase_38 AC 770 ms
241,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define CPP17
#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#ifdef CPP17
#include <variant>
#endif

#define endl codeforces

#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
using size_type = ssize_t;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

template <typename T, std::size_t Head, std::size_t... Tail> 
struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };

template <typename T, std::size_t Head> 
struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };

template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

#ifdef CPP17
template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { 
    if constexpr (std::is_invocable<F, Args...>::value) { 
        t = f(args...); 
    } else { 
        for (size_type i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); 
    } 
}
#endif

template <typename T> vec<T> make_v(size_type sz) { return vec<T>(sz); }

template <typename T, typename... Tail> 
auto make_v(size_type hs, Tail&&... ts) { 
    auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); 
    return vec<decltype(v)>(hs, v); 
}

namespace init__ { 
struct InitIO { 
    InitIO() { 
        std::cin.tie(nullptr); 
        std::ios_base::sync_with_stdio(false); 
        std::cout << std::fixed << std::setprecision(30); 
    } 
} init_io; 
}
template <typename T>
T ceil_pow2(T bound) {
    T ret = 1;
    while (ret < bound) ret *= 2;
    return ret;
}
template <typename T>
T ceil_div(T a, T b) { return a / b + !!(a % b); }
#define CPP17


namespace graph {

using Node = ll;
using Weight = ll;
using Edge = std::pair<Node, Weight>;

template <bool Directed>
struct Graph : public vvec<Edge> {
    using vvec<Edge>::vvec;

    void add_edge(Node f, Node t, Weight w = 1) {
        (*this)[f].emplace_back(t, w);
        if (!Directed) (*this)[t].emplace_back(f, w);
    }

    Graph<Directed> build_inv() const {
        Graph<Directed> ret(this->size());
        for (Node i = 0; i < this->size(); i++) {
            for (const Edge &e : (*this)[i]) {
                Node j;
                Weight w;
                std::tie(j, w) = e;
                if (!Directed && j < i) continue;
                ret.add_edge(j, i, w);
            }
        }

        return ret;
    }
};

template <typename Iterator>
class dst_iterator {
    Iterator ite;

public:
    dst_iterator(Iterator ite) : ite(ite) { }

    bool operator ==(const dst_iterator<Iterator> &oth) const {
        return ite == oth.ite;
    }

    bool operator !=(const dst_iterator<Iterator> &oth) const {
        return !(*this == oth);
    }

    bool operator <(const dst_iterator<Iterator> &oth) const {
        return ite < oth.ite;
    }

    bool operator >(const dst_iterator<Iterator> &oth) const {
        return ite > oth.ite;
    }

    bool operator <=(const dst_iterator<Iterator> &oth) const {
        return ite <= oth.ite;
    }

    bool operator >=(const dst_iterator<Iterator> &oth) const {
        return ite >= oth.ite;
    }

    const Node& operator *() {
        return ite->first;
    }

    const Node& operator *() const {
        return ite->first;
    }

    dst_iterator operator ++() {
        ++ite;
        return ite;
    }
};

class dst_iteration {
    using ite_type = vec<Edge>::const_iterator;
    const vec<Edge> &edges;

public:
    dst_iteration(const vec<Edge> &edges) : edges(edges) { }

    auto begin() const {
        return dst_iterator<ite_type>(edges.cbegin());
    }

    auto end() const {
        return dst_iterator<ite_type>(edges.cend());
    }
};

class dst_reverse_iteration {
    using ite_type = vec<Edge>::const_reverse_iterator;
    const vec<Edge> &edges;

public:
    dst_reverse_iteration(const vec<Edge> &edges) : edges(edges) { }

    auto begin() const {
        return dst_iterator<ite_type>(edges.crbegin());
    }

    auto end() const {
        return dst_iterator<ite_type>(edges.crend());
    }
};

dst_iteration dst(const vec<Edge> &edges) {
    return dst_iteration(edges);
}

dst_reverse_iteration rdst(const vec<Edge> &edges) {
    return dst_reverse_iteration(edges);
}

}

struct SegmentTreeGraph : graph::Graph<true> {
    
    SegmentTreeGraph(size_type nsz) : Graph(), nsz(nsz), ceiled_nsz(ceil_pow2(nsz)) {
        this->resize(3 * ceiled_nsz);

        for (size_type i = 1; i < ceiled_nsz; i++) {
            this->add_edge(i, 2 * i + 0);
            this->add_edge(i, 2 * i + 1);
        }

        size_type offset = 2 * ceiled_nsz;
        auto get_idx = [&](size_type i) {
            if (i + offset < this->size()) return i + offset;
            return i;
        };
        for (size_type i = 1; i < ceiled_nsz; i++) {
            this->add_edge(get_idx(2 * i + 0), get_idx(i));
            this->add_edge(get_idx(2 * i + 1), get_idx(i));
        }
    }

    size_type nodes() const noexcept {
        return nsz;
    }

    size_type node2idx(size_type node) const noexcept {
        return node + ceiled_nsz;
    }

    size_type idx2node(size_type idx) const noexcept {
        return idx - ceiled_nsz;
    }
    
    void range_add_edge(size_type lsrc, size_type rsrc, size_type ldst, size_type rdst) {
        range_add_edge(lsrc, rsrc, ldst, rdst, graph::Weight());
    }

    void range_add_edge(size_type lsrc, size_type rsrc, size_type ldst, size_type rdst, graph::Weight w) {
        size_type src = this->size();
        size_type dst = src + 1;
        this->emplace_back();
        this->emplace_back();
        this->add_edge(src, dst, w);
        range_add_edge_aux(lsrc, rsrc, src, true);
        range_add_edge_aux(ldst, rdst, dst, false);
    }
   
private:
    const size_type nsz, ceiled_nsz;

    void range_add_edge_aux(size_type l, size_type r, size_type super, bool is_src) {
        size_type nl = l + ceiled_nsz, nr = r + ceiled_nsz;

        auto update = [&](size_type i) {
            if (is_src) this->add_edge(i, super);
            else this->add_edge(super, i);
        };

        bool init = true;

        auto offset = [&]() -> ll {
            if (init) return 0;
            if (!is_src) return 0;
            return 2 * ceiled_nsz;
        };

        while (nl < nr) {
            if (nl & 1) {
                update(nl + offset());
                nl++;
            }
            if (nr & 1) {
                nr--;
                update(nr + offset());
            }
            nl /= 2;
            nr /= 2;
            init = false;
        }
    }
};

int main() {
    const ll inf = 5e15;
    ll n, a, b;
    std::cin >> n >> a >> b;
    vec<ll> xv(n);
    for (ll &e : xv) std::cin >> e;
    std::map<ll, ll> d2n;
    for (ll i = 0; i < n; i++) d2n[xv[i]] = i;
    d2n[inf] = n;

    SegmentTreeGraph g(n);

    for (ll i = 0; i < n; i++) {
        ll d = xv[i];
        ll ln = d2n.lower_bound(d + a)->second;
        ll un = d2n.upper_bound(d + b)->second;
        if (!(ln < un)) continue;
        g.range_add_edge(ln, un, ln, un);
        ll idx1 = g.node2idx(i), idx2 = g.node2idx(ln);
        g.add_edge(idx1, idx2);
        g.add_edge(idx2, idx1);
    }

    vec<ll> ans(n);
    vec<bool> visited(g.size());
    auto dfs = [&](ll idx) {
        const ll ln = g.node2idx(0), un = g.node2idx(n);
        ll sz = 0;
        vec<ll> idxv;

        auto rec = [&](ll cur, auto f) -> void {
            visited[cur] = true;
            if (ln <= cur && cur < un) {
                sz++;
                idxv.push_back(g.idx2node(cur));
            }
            for (ll nxt : graph::dst(g[cur])) if (!visited[nxt]) f(nxt, f);
        };

        rec(idx, rec);
        for (ll e : idxv) ans[e] = sz;
    };

    for (ll i = 0; i < n; i++) {
        ll idx = g.node2idx(i);
        if (!visited[idx]) dfs(idx);
    }

    for (ll e : ans) std::cout << e << "\n";
    return 0;
}
0