提出 #18718882


ソースコード 拡げる

use std::cmp::{max, min};

const MAX: usize = 300000;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let k: usize = sc.read();
    let a: Vec<usize> = sc.vec(n);
    let mut seg = SegmentTree::new(MAX + 1, 0, max);
    for i in 0..n {
        let from = max(k, a[i]) - k;
        let to = min(MAX, a[i] + k);
        let t = seg.query(from..(to + 1));
        seg.update(a[i], t + 1);
    }
    let ans = seg.query(0..(MAX + 1));
    println!("{}", ans);
}

/// Segment Tree for range queries
pub struct SegmentTree<T, F> {
    seg: Vec<T>,
    n: usize,
    f: F,
    initial_value: T,
}

impl<T: Copy, F> SegmentTree<T, F>
where
    F: Fn(T, T) -> T,
{
    pub fn new(size: usize, initial_value: T, f: F) -> SegmentTree<T, F> {
        let mut m = 1;
        while m <= size {
            m <<= 1;
        }
        SegmentTree {
            seg: vec![initial_value; m * 2],
            n: m,
            f,
            initial_value,
        }
    }

    pub fn update(&mut self, k: usize, value: T) {
        let mut k = k;
        k += self.n - 1;
        self.seg[k] = value;
        while k > 0 {
            k = (k - 1) >> 1;
            self.seg[k] = (self.f)(self.seg[k * 2 + 1], self.seg[k * 2 + 2]);
        }
    }

    /// Get the minimum value in the array in the range
    pub fn query(&self, range: std::ops::Range<usize>) -> T {
        self.query_range(range, 0, 0..self.n)
    }

    fn query_range(
        &self,
        range: std::ops::Range<usize>,
        k: usize,
        seg_range: std::ops::Range<usize>,
    ) -> T {
        if seg_range.end <= range.start || range.end <= seg_range.start {
            self.initial_value
        } else if range.start <= seg_range.start && seg_range.end <= range.end {
            self.seg[k]
        } else {
            let mid = (seg_range.start + seg_range.end) >> 1;
            let x = self.query_range(range.clone(), k * 2 + 1, seg_range.start..mid);
            let y = self.query_range(range, k * 2 + 2, mid..seg_range.end);
            (self.f)(x, y)
        }
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}

提出情報

提出日時
問題 D - Flat Subsequence
ユーザ kenkoooo
言語 Rust (1.42.0)
得点 400
コード長 3359 Byte
結果 AC
実行時間 192 ms
メモリ 6740 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 169 ms 6260 KiB
001.txt AC 46 ms 4560 KiB
002.txt AC 64 ms 5028 KiB
003.txt AC 104 ms 5596 KiB
004.txt AC 37 ms 4632 KiB
005.txt AC 74 ms 5428 KiB
006.txt AC 104 ms 5548 KiB
007.txt AC 161 ms 6048 KiB
008.txt AC 60 ms 5116 KiB
009.txt AC 66 ms 4828 KiB
010.txt AC 153 ms 6668 KiB
011.txt AC 178 ms 6524 KiB
012.txt AC 191 ms 6700 KiB
013.txt AC 191 ms 6696 KiB
014.txt AC 177 ms 6544 KiB
015.txt AC 163 ms 6708 KiB
016.txt AC 179 ms 6532 KiB
017.txt AC 178 ms 6544 KiB
018.txt AC 162 ms 6740 KiB
019.txt AC 176 ms 6588 KiB
020.txt AC 137 ms 6568 KiB
021.txt AC 158 ms 6580 KiB
022.txt AC 153 ms 6716 KiB
023.txt AC 192 ms 6536 KiB
024.txt AC 171 ms 6536 KiB
025.txt AC 145 ms 6552 KiB
026.txt AC 139 ms 6604 KiB
027.txt AC 169 ms 6664 KiB
028.txt AC 121 ms 6740 KiB
029.txt AC 123 ms 6516 KiB
example0.txt AC 2 ms 2084 KiB