提出 #17047530


ソースコード 拡げる

#include <iostream>
#include <vector>
#include <algorithm>
using i64 = long long;

template <typename T, typename F>
class SegmentTree {
    T id;
    F f;
    const int size;
    std::vector<T> dat;

    T query_impl(const int a, const int b, const int k, const int l, const int r) const {
        if (r <= a || b <= l) return id;
        if (a <= l && r <= b) return dat[k];
        return f(query_impl(a, b, k * 2 + 1, l, (l + r) / 2),
                 query_impl(a, b, k * 2 + 2, (l + r) / 2, r));
    }

    template <class Predicate>
    int find_leftmost(const int a, const int b, Predicate pred, const int k, const int l, const int r) {
        if (r <= a || b <= l) return size;
        if (a <= l && r <= b) {
            if (!pred(dat[k], r - l)) return size;
            if (r - l == 1) return l;
        }
        const int cand = find_leftmost(a, b, pred, k * 2 + 1, l, (l + r) / 2);
        return cand < size ? cand : find_leftmost(a, b, pred, k * 2 + 2, (l + r) / 2, r);
    }

    template <class Predicate>
    int find_rightmost(const int a, const int b, Predicate pred, const int k, const int l, const int r) {
        if (r <= a || b <= l) return -1;
        if (a <= l && r <= b) {
            if (!pred(dat[k], r - l)) return -1;
            if (r - l == 1) return l;
        }
        const int cand = find_rightmost(a, b, pred, k * 2 + 2, (l + r) / 2, r);
        return cand >= 0 ? cand : find_rightmost(a, b, pred, k * 2 + 1, l, (l + r) / 2);
    }

    static int calc_size(const int n) {
        int ret = 2;
        while (ret < n) ret *= 2;
        return ret;
    }

public:
    enum class FindDirection {
        Left,
        Right,
    };

    SegmentTree(const int n, const T &id, F f)
        : id(id)
        , f(f)
        , size(calc_size(n))
        , dat(2 * size - 1, id) {}

    template <template <class> class Container, class U>
    SegmentTree(const Container<T> &v, const U &id, F f)
        : id(id)
        , f(f)
        , size(calc_size(v.size()))
        , dat(2 * size - 1, id)
    {
        std::copy(v.begin(), v.end(), dat.begin() + size - 1);
        dig(0);
    }

    void dig(const int v) {
        if (v >= size - 1) return;
        dig(2 * v + 1);
        dig(2 * v + 2);
        dat[v] = f(dat[2 * v + 1], dat[2 * v + 2]);
    }

    void update(int i, const T x) {
        i += size - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;
            dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }

    T query(const int a, const int b) const {
        return query_impl(a, b, 0, 0, size);
    }

    template <class Predicate>
    int find(const int l, const int r, FindDirection dir, Predicate pred) {
        return dir == FindDirection::Left
            ? find_leftmost(l, r, pred, 0, 0, size)
            : find_rightmost(l, r, pred, 0, 0, size);
    }

    T operator[](const int i) const {
        return dat[i + size - 1];
    }
};

int main() {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n);
    for (auto &e : a) std::cin >> e;

    SegmentTree st(300100, 0, [](const auto lhs, const auto rhs) { return std::max(lhs, rhs); });
    for (const int e : a) {
        const int l = std::max(e - k, 0), r = std::min(300001, e + k + 1);
        st.update(e, st.query(l, r) + 1);
    }

    std::cout << st.query(0, 300001) << std::endl;

    return 0;
}

提出情報

提出日時
問題 D - Flat Subsequence
ユーザ CharlotteL
言語 C++ (GCC 9.2.1)
得点 400
コード長 3495 Byte
結果 AC
実行時間 178 ms
メモリ 8548 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 1
AC × 31
セット名 テストケース
Sample example0.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, example0.txt
ケース名 結果 実行時間 メモリ
000.txt AC 148 ms 8228 KiB
001.txt AC 47 ms 7276 KiB
002.txt AC 59 ms 7756 KiB
003.txt AC 91 ms 7836 KiB
004.txt AC 39 ms 7416 KiB
005.txt AC 70 ms 7952 KiB
006.txt AC 98 ms 7824 KiB
007.txt AC 149 ms 8232 KiB
008.txt AC 58 ms 7600 KiB
009.txt AC 57 ms 7664 KiB
010.txt AC 144 ms 8544 KiB
011.txt AC 162 ms 8308 KiB
012.txt AC 172 ms 8328 KiB
013.txt AC 178 ms 8548 KiB
014.txt AC 167 ms 8448 KiB
015.txt AC 149 ms 8452 KiB
016.txt AC 160 ms 8480 KiB
017.txt AC 166 ms 8328 KiB
018.txt AC 156 ms 8484 KiB
019.txt AC 161 ms 8472 KiB
020.txt AC 133 ms 8492 KiB
021.txt AC 145 ms 8384 KiB
022.txt AC 139 ms 8392 KiB
023.txt AC 174 ms 8436 KiB
024.txt AC 158 ms 8392 KiB
025.txt AC 134 ms 8468 KiB
026.txt AC 132 ms 8368 KiB
027.txt AC 150 ms 8364 KiB
028.txt AC 120 ms 8448 KiB
029.txt AC 120 ms 8480 KiB
example0.txt AC 7 ms 7264 KiB