結果

問題 No.318 学学学学学
ユーザー ziitaziita
提出日時 2019-10-14 16:47:52
言語 Rust
(1.77.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 7,081 bytes
コンパイル時間 7,815 ms
コンパイル使用メモリ 168,212 KB
実行使用メモリ 15,972 KB
最終ジャッジ日時 2023-08-25 18:29:56
合計ジャッジ時間 9,405 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
4,376 KB
testcase_01 AC 24 ms
4,380 KB
testcase_02 AC 29 ms
4,376 KB
testcase_03 AC 17 ms
4,384 KB
testcase_04 AC 22 ms
4,380 KB
testcase_05 AC 140 ms
15,972 KB
testcase_06 AC 136 ms
12,444 KB
testcase_07 AC 111 ms
11,904 KB
testcase_08 AC 91 ms
10,172 KB
testcase_09 AC 78 ms
9,796 KB
testcase_10 AC 64 ms
8,676 KB
testcase_11 AC 143 ms
15,608 KB
testcase_12 AC 103 ms
12,268 KB
testcase_13 AC 93 ms
11,764 KB
testcase_14 AC 83 ms
10,100 KB
testcase_15 AC 80 ms
9,388 KB
testcase_16 AC 68 ms
8,704 KB
testcase_17 AC 103 ms
12,652 KB
testcase_18 AC 88 ms
12,484 KB
testcase_19 AC 104 ms
12,312 KB
testcase_20 AC 54 ms
8,584 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `l`
   --> Main.rs:101:19
    |
101 |             (Some(l), Some(r)) => Some(r),
    |                   ^ help: if this is intentional, prefix it with an underscore: `_l`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `l`
   --> Main.rs:102:19
    |
102 |             (Some(l), None) => None,
    |                   ^ help: if this is intentional, prefix it with an underscore: `_l`

warning: unused variable: `l`
   --> Main.rs:110:19
    |
110 |             (Some(l), Some(r)) => Some(r),
    |                   ^ help: if this is intentional, prefix it with an underscore: `_l`

warning: unused variable: `l`
   --> Main.rs:111:19
    |
111 |             (Some(l), None) => None,
    |                   ^ help: if this is intentional, prefix it with an underscore: `_l`

warning: variable does not need to be mutable
   --> Main.rs:140:13
    |
140 |         let mut lazy_data = vec![None; size_p2 * 2];
    |             ----^^^^^^^^^
    |             |
    |             help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: 5 warnings emitted

ソースコード

diff #

#![allow(unused_imports)]
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;
use std::io::{Write, BufWriter};

static MOD: usize = 998244353;
// static MOD: usize = 1000000007;

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp;
use std::marker::PhantomData;

pub trait Monoid<T> {
    fn id() -> Option<T> {
        None
    }
    // TxT->T
    fn tt_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
    // SxS->S
    fn ss_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
    // TxS->T
    fn ts_op(l: &Option<T>, r: &Option<T>) -> Option<T>;
}

pub struct SegOp<T: Ord> {
    phantom: PhantomData<T>,
}

impl<T: Ord + Clone + Add<Output=T>> Monoid<T> for SegOp<T> {
    #[inline]
    // add
    fn tt_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(l+r),
            (Some(l), None) => Some(l),
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
    // overwrite
    fn ss_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(r),
            (Some(l), None) => None,
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
    // overwrite
    fn ts_op(l: &Option<T>, r: &Option<T>) -> Option<T> {
        match (l.clone(), r.clone()) {
            (Some(l), Some(r)) => Some(r),
            (Some(l), None) => None,
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
}

pub struct SegmentTree<M: Monoid<T>, T: Clone> {
    phantom: PhantomData<M>,
    data: Vec<Option<T>>,
    lazy_data: Vec<Option<T>>,
    size: usize,
    size_p2: usize,
}

impl<M: Monoid<T>, T: Clone> SegmentTree<M, T> {
    pub fn from_vec(v: Vec<T>) -> SegmentTree<M, T> {
        let size = v.len();
        let mut size_p2 = 1;
        while size_p2 < v.len() {
            size_p2 *= 2;
        }
        let mut data = vec![None; size_p2 * 2];
        for (i, x) in v.into_iter().enumerate() {
            data[size_p2 + i] = Some(x);
        }
        for i in (0..size_p2).rev() {
            data[i] = M::tt_op(&data[i * 2 + 0], &data[i * 2 + 1]);
        }
        let mut lazy_data = vec![None; size_p2 * 2];
        SegmentTree {
            phantom: PhantomData,
            data: data,
            lazy_data: lazy_data,
            size: size,
            size_p2: size_p2,
        }
    }

    pub fn size(&self) -> usize {
        self.size
    }

    pub fn eval(&mut self, l: usize, r: usize, k: usize){
        if self.lazy_data[k].is_some() {
            self.data[k] = M::ts_op(&self.data[k], &self.lazy_data[k]);
            if r-l>1 {
                self.lazy_data[2*k+0] = M::ss_op(&self.data[2*k+0], &self.lazy_data[k]);
                self.lazy_data[2*k+1] = M::ss_op(&self.data[2*k+1], &self.lazy_data[k]);
            }
            self.lazy_data[k] = None;
        }
    }

    pub fn lazy_update(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize, value: T) {
        // assert!(l <= r && r <= self.size);
        self.eval(l,r,k);
        if b<=l || r<=a {return;}
        if a<=l && r<=b {
            self.lazy_data[k] = M::ss_op(&self.lazy_data[k], &Some(value));
            self.eval(l,r,k);
        }
        else{
            self.lazy_update(a,b,l,(l+r)/2,2*k+0,value.clone());
            self.lazy_update(a,b,(l+r)/2,r,2*k+1,value.clone());
            self.data[k] = M::tt_op(&self.data[2*k+0], &self.data[2*k+1]);
        }
    }

    pub fn update(&mut self, a: usize, b:usize, value: T){
        self.lazy_update(a,b,0,self.size_p2,1,value);
    }

    pub fn lazy_query(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize) -> Option<T> {
        self.eval(l,r,k);
        if a<=l && r<=b {return self.data[k].clone();}
        if b<=l || r<=a {return None;}
        let res1 = self.lazy_query(a, b, l, (l+r)/2, 2*k+0);
        let res2 = self.lazy_query(a, b, (l+r)/2, r, 2*k+1);
        M::tt_op(&res1, &res2)
    }

    pub fn query(&mut self, l: usize, r: usize) -> Option<T> {
        self.lazy_query(l,r,0,self.size_p2,1)
    }
}

#[derive(Copy, Clone, Eq, PartialEq)]
struct Tuple {
    x: i64,
    l: usize,
    r: usize,
}

impl Ord for Tuple{
    fn cmp(&self, other: &Tuple) -> Ordering {
        other.x.cmp(&self.x)
    }
}

impl PartialOrd for Tuple {
    fn partial_cmp(&self, other: &Tuple) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn solve() {
    input! {
        n: usize,
        a: [i64; n],
    }  
    let mut book_l: HashMap<i64,usize> = HashMap::new();
    let mut book_r: HashMap<i64,usize> = HashMap::new();
    for i in 0..n {
        let num = a[i];
        if book_l.contains_key(&num) {
            book_r.insert(num, i);
        }
        else{
            book_l.insert(num, i);
            book_r.insert(num, i);
        }
    }
    let mut heap: BinaryHeap<Tuple> = BinaryHeap::new();
    for i in book_l.keys() {
        let l = book_l.get(&i).unwrap();
        let r = book_r.get(&i).unwrap();
        heap.push(Tuple{x:*i,l:*l,r:*r});
    }
    pub type Seg<T> = SegmentTree<SegOp<T>, T>;
    let mut seg = Seg::from_vec(a.clone());
    while let Some(Tuple{x,l,r}) = heap.pop() {
        seg.update(l,r+1,x);
    }
    for i in 0..n {
        print!("{} ", seg.query(i,i+1).unwrap());
    }
    println!();
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0