結果

問題 No.674 n連勤
ユーザー hatoohatoo
提出日時 2018-04-15 16:12:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 10,886 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 163,764 KB
実行使用メモリ 9,788 KB
最終ジャッジ日時 2023-09-09 05:43:33
合計ジャッジ時間 3,786 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 15 ms
4,376 KB
testcase_14 AC 84 ms
9,628 KB
testcase_15 AC 71 ms
8,056 KB
testcase_16 AC 105 ms
9,788 KB
testcase_17 AC 112 ms
9,608 KB
testcase_18 AC 107 ms
8,416 KB
testcase_19 AC 129 ms
9,628 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `l`
   --> Main.rs:199:50
    |
199 |     fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
    |                                                  ^ help: if this is intentional, prefix it with an underscore: `_l`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `r`
   --> Main.rs:199:60
    |
199 |     fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
    |                                                            ^ help: if this is intentional, prefix it with an underscore: `_r`

warning: unused variable: `l`
   --> Main.rs:237:50
    |
237 |     fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
    |                                                  ^ help: if this is intentional, prefix it with an underscore: `_l`

warning: unused variable: `r`
   --> Main.rs:237:60
    |
237 |     fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
    |                                                            ^ help: if this is intentional, prefix it with an underscore: `_r`

warning: 4 warnings emitted

ソースコード

diff #

/**
*  _           _                 __                            _   _ _   _                                 _                    _                  _
* | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
* | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
* | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
* | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
* |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
*                                                  | |                                                                           | |   | |
*                                                  |_|                                                                           |_|   |_|
*
* https://github.com/hatoo/competitive-rust-snippets
*/
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
mod util {
    use std::fmt::Debug;
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

#[allow(dead_code)]
/// Lazy Segment Tree
pub struct SEG<T: SEGimpl> {
    n: usize,
    buf: Vec<T::Elem>,
    zero: T::Elem,
    phantom: std::marker::PhantomData<T>,
}
impl<T: SEGimpl> SEG<T> {
    #[allow(dead_code)]
    pub fn new(n: usize, zero: T::Elem) -> SEG<T> {
        let n = (1..).map(|i| 1 << i).find(|&x| x >= n).unwrap();
        SEG {
            n: n,
            buf: vec![zero.clone(); 2 * n],
            zero: zero,
            phantom: std::marker::PhantomData,
        }
    }
    #[allow(dead_code)]
    fn eval(&mut self, k: usize, l: usize, r: usize) {
        if r - l > 1 {
            let (l, r) = self.buf.split_at_mut(2 * k + 1);
            let (c1, c2) = r.split_at_mut(1);
            T::eval(&mut l[k], Some((&mut c1[0], &mut c2[0])));
        } else {
            T::eval(&mut self.buf[k], None);
        }
    }
    #[allow(dead_code)]
    pub fn update(&mut self, i: usize, x: T::Elem) {
        let mut k = i + self.n - 1;
        self.buf[k] = x;
        self.eval(k, i, i + 1);
        while k > 0 {
            k = (k - 1) / 2;
            let (l, r) = self.buf.split_at_mut(2 * k + 1);
            let (c1, c2) = r.split_at_mut(1);
            T::reduce(&mut l[k], &c1[0], &c2[0]);
        }
    }
    #[allow(dead_code)]
    pub fn get(&mut self, i: usize) -> Option<T::R> {
        self.query(i, i + 1)
    }
    #[allow(dead_code)]
    fn r(&mut self, x: &T::A, a: usize, b: usize, k: usize, l: usize, r: usize) {
        self.eval(k, l, r);
        if r <= a || b <= l {
            return;
        }
        if a <= l && r <= b {
            T::range(x, &mut self.buf[k], l, r);
            self.eval(k, l, r);
            return;
        }
        self.r(x, a, b, 2 * k + 1, l, (l + r) / 2);
        self.r(x, a, b, 2 * k + 2, (l + r) / 2, r);
        let (l, r) = self.buf.split_at_mut(2 * k + 1);
        let (c1, c2) = r.split_at_mut(1);
        T::reduce(&mut l[k], &c1[0], &c2[0]);
    }
    #[allow(dead_code)]
    pub fn range_add(&mut self, x: &T::A, a: usize, b: usize) {
        let n = self.n;
        self.r(x, a, b, 0, 0, n);
    }
    #[allow(dead_code)]
    pub fn add(&mut self, x: &T::A, i: usize) {
        self.range_add(x, i, i + 1);
    }
    #[allow(dead_code)]
    fn q(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> Option<T::Elem> {
        self.eval(k, l, r);
        if r <= a || b <= l {
            return None;
        }
        if a <= l && r <= b {
            Some(self.buf[k].clone())
        } else {
            let vl = self.q(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.q(a, b, k * 2 + 2, (l + r) / 2, r);
            match (vl, vr) {
                (Some(l), Some(r)) => {
                    let mut res = self.zero.clone();
                    T::reduce(&mut res, &l, &r);
                    Some(res)
                }
                (Some(l), None) => Some(l),
                (None, Some(r)) => Some(r),
                _ => None,
            }
        }
    }
    #[allow(dead_code)]
    pub fn query(&mut self, a: usize, b: usize) -> Option<T::R> {
        let n = self.n;
        self.q(a, b, 0, 0, n).map(T::to_result)
    }
}
pub trait SEGimpl {
    type Elem: Clone + std::fmt::Debug;
    type A;
    type R;
    fn eval(parent: &mut Self::Elem, children: Option<(&mut Self::Elem, &mut Self::Elem)>);
    fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize);
    fn reduce(parent: &mut Self::Elem, c1: &Self::Elem, c2: &Self::Elem);
    fn to_result(elem: Self::Elem) -> Self::R;
}

struct MIN;
impl SEGimpl for MIN {
    type Elem = (Option<usize>, Option<usize>);
    type A = usize;
    type R = Option<usize>;
    fn eval(parent: &mut Self::Elem, children: Option<(&mut Self::Elem, &mut Self::Elem)>) {
        if (parent.0).is_some() {
            parent.1 = parent.0;
            if let Some((c1, c2)) = children {
                c1.0 = parent.0;
                c2.0 = parent.0;
            }
            parent.0 = None;
        }
    }
    fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
        elem.0 = Some(*x);
    }
    fn reduce(parent: &mut Self::Elem, c1: &Self::Elem, c2: &Self::Elem) {
        match (c1.1, c2.1) {
            (None, None) => {
                parent.1 = None;
            }
            (Some(x), None) => {
                parent.1 = Some(x);
            }
            (None, Some(x)) => {
                parent.1 = Some(x);
            }
            (Some(x), Some(y)) => {
                parent.1 = Some(min(x, y));
            }
        }
    }
    fn to_result(elem: Self::Elem) -> Self::R {
        elem.1
    }
}
struct MAX;
impl SEGimpl for MAX {
    type Elem = (Option<usize>, Option<usize>);
    type A = usize;
    type R = Option<usize>;
    fn eval(parent: &mut Self::Elem, children: Option<(&mut Self::Elem, &mut Self::Elem)>) {
        if (parent.0).is_some() {
            parent.1 = parent.0;
            if let Some((c1, c2)) = children {
                c1.0 = parent.0;
                c2.0 = parent.0;
            }
            parent.0 = None;
        }
    }
    fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) {
        elem.0 = Some(*x);
    }
    fn reduce(parent: &mut Self::Elem, c1: &Self::Elem, c2: &Self::Elem) {
        match (c1.1, c2.1) {
            (None, None) => {
                parent.1 = None;
            }
            (Some(x), None) => {
                parent.1 = Some(x);
            }
            (None, Some(x)) => {
                parent.1 = Some(x);
            }
            (Some(x), Some(y)) => {
                parent.1 = Some(max(x, y));
            }
        }
    }
    fn to_result(elem: Self::Elem) -> Self::R {
        elem.1
    }
}

fn solve() {
    let (_d, q) = get!(u64, usize);

    let ab = get!(u64, u64; q);
    let mut cc = Vec::new();
    for &(a, b) in &ab {
        cc.push(a);
        cc.push(b + 1);
    }
    cc.sort();
    cc.dedup();

    let mut segmin: SEG<MIN> = SEG::new(cc.len() + 1, (None, None));
    let mut segmax: SEG<MAX> = SEG::new(cc.len() + 1, (None, None));

    // debug!(cc);
    util::with_bufwriter(|mut out| {
        let mut ans = 0;
        for (a, b) in ab {
            let l = cc.binary_search(&a).unwrap();
            let r = cc.binary_search(&(b + 1)).unwrap();

            let ll = segmin
                .query(l, r + 1)
                .unwrap()
                .map(|x| (min(x, l)))
                .unwrap_or(l);
            let rr = segmax
                .query(l, r + 1)
                .unwrap()
                .map(|x| max(x, r))
                .unwrap_or(r);

            ans = max(ans, cc[rr] - cc[ll]);

            segmin.range_add(&ll, ll, rr + 1);
            segmax.range_add(&rr, ll, rr + 1);

            // debug!(l, r, ll, rr);
            writeln!(out, "{}", ans).unwrap();
        }
    });
}
0