結果

問題 No.1030 だんしんぐぱーりない
ユーザー cotton_fn_cotton_fn_
提出日時 2020-04-18 20:37:30
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 8,039 bytes
コンパイル時間 13,351 ms
コンパイル使用メモリ 390,240 KB
実行使用メモリ 13,316 KB
最終ジャッジ日時 2024-10-04 00:15:40
合計ジャッジ時間 18,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 1 ms
6,820 KB
testcase_05 AC 222 ms
10,980 KB
testcase_06 AC 91 ms
10,028 KB
testcase_07 AC 55 ms
6,820 KB
testcase_08 AC 55 ms
7,412 KB
testcase_09 AC 83 ms
11,892 KB
testcase_10 AC 30 ms
6,816 KB
testcase_11 AC 86 ms
8,288 KB
testcase_12 AC 74 ms
9,720 KB
testcase_13 AC 67 ms
9,176 KB
testcase_14 AC 95 ms
10,052 KB
testcase_15 AC 41 ms
6,816 KB
testcase_16 AC 69 ms
7,616 KB
testcase_17 AC 71 ms
11,280 KB
testcase_18 AC 102 ms
10,364 KB
testcase_19 AC 45 ms
6,816 KB
testcase_20 AC 54 ms
6,820 KB
testcase_21 AC 64 ms
8,864 KB
testcase_22 AC 53 ms
6,888 KB
testcase_23 AC 68 ms
6,860 KB
testcase_24 AC 57 ms
6,820 KB
testcase_25 AC 67 ms
7,948 KB
testcase_26 AC 37 ms
6,816 KB
testcase_27 AC 45 ms
6,816 KB
testcase_28 AC 82 ms
8,524 KB
testcase_29 AC 56 ms
9,396 KB
testcase_30 AC 49 ms
6,992 KB
testcase_31 AC 52 ms
6,816 KB
testcase_32 AC 83 ms
9,980 KB
testcase_33 AC 77 ms
10,272 KB
testcase_34 AC 31 ms
6,820 KB
testcase_35 AC 125 ms
13,312 KB
testcase_36 AC 130 ms
13,176 KB
testcase_37 AC 134 ms
13,312 KB
testcase_38 AC 126 ms
13,316 KB
testcase_39 AC 134 ms
13,180 KB
testcase_40 AC 1 ms
6,816 KB
testcase_41 AC 1 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused)]

use kyoproio::*;
use std::{
    collections::*,
    io::{self, prelude::*},
    iter,
    mem::{replace, swap},
};

fn main() -> io::Result<()> {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024)
        .spawn(solve)?
        .join()
        .unwrap();
    Ok(())
}

fn solve() {
    let stdin = io::stdin();
    let mut kin = KInput::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    macro_rules! output {($($args:expr),+) => { write!(&mut out, $($args),+) }; }
    macro_rules! outputln {
        ($($args:expr),+) => { output!($($args),+); outputln!(); };
        () => { output!("\n"); if cfg!(debug_assertions) { out.flush(); } }
    }

    let (n, k, q): (usize, usize, usize) = kin.input();
    let mut c: Vec<u32> = kin.seq(n);
    c.insert(0, 0);
    let mut a: Vec<usize> = kin.seq(k);
    a.insert(0, 0);
    let mut g = vec![vec![]; n + 1];
    for _ in 0..n - 1 {
        let (u, v): (usize, usize) = kin.input();
        g[v].push(u);
    }
    dfs(&g, &mut c, 1, 0);
    let hld = Hld::new(&g, 1);
    let mut st = SegmentTree::new(k + 1, || 0, |&u, &v| match (u, v) {
        (u, 0) => u,
        (0, v) => v,
        (u, v) => hld.lca(u, v)
    });
    for i in 1..=k {
        st.set(i, a[i]);
    }
    for _ in 0..q {
        match kin.input() {
            '1' => {
                let (x, y): (usize, usize) = kin.input();
                st.set(x, y);
            }
            '2' => {
                let (l, r): (usize, usize) = kin.input();
                outputln!("{}", c[st.fold(l, r + 1)]);
            }
            _ => {}
        }
    }
}
fn dfs(g: &[Vec<usize>], c: &mut [u32], u: usize, p: usize) {
    for &v in g[u].iter().filter(|&&v| v != p) {
        c[v] = c[v].max(c[u]);
        dfs(g, c, v, u);
    }
}

#[derive(Debug)]
pub struct Hld {
    head: Vec<usize>,
    par: Vec<usize>,
    tab: Vec<usize>,
}
pub type Graph = [Vec<usize>];
impl Hld {
    pub fn new(g: &Graph, root: usize) -> Self {
        let mut heavy = vec![!0; g.len()];
        Self::dfs_heavy(g, &mut heavy, &mut vec![1; g.len()], root, !0);
        let mut hld = Hld {
            head: Vec::with_capacity(g.len()),
            par: Vec::with_capacity(g.len()),
            tab: vec![0; g.len()],
        };
        hld.dfs_build(g, &heavy, root, !0, root);
        hld
    }
    fn dfs_heavy(g: &Graph, heavy: &mut [usize], size: &mut [usize], u: usize, p: usize) {
        let mut max = 0;
        for &v in g[u].iter().filter(|&&v| v != p) {
            Self::dfs_heavy(g, heavy, size, v, u);
            if size[v] > max {
                max = size[v];
                heavy[u] = v;
            }
            size[u] += size[v];
        }
    }
    fn dfs_build(&mut self, g: &Graph, heavy: &[usize], u: usize, p: usize, h: usize) {
        self.tab[u] = self.head.len();
        self.head.push(h);
        self.par.push(p);
        if heavy[u] == !0 {
            return;
        }
        self.dfs_build(g, heavy, heavy[u], u, h);
        for &v in g[u].iter().filter(|&&v| v != p && v != heavy[u]) {
            self.dfs_build(g, heavy, v, u, v);
        }
    }
    pub fn lca(&self, mut u: usize, mut v: usize) -> usize {
        loop {
            if self.tab[u] > self.tab[v] {
                swap(&mut u, &mut v);
            }
            if self.head[self.tab[u]] == self.head[self.tab[v]] {
                return u;
            }
            v = self.par[self.tab[self.head[self.tab[v]]]];
        }
    }
}

pub struct SegmentTree<T, Z, F> {
    zero: Z,
    f: F,
    a: Vec<T>,
}
impl<T, Z: Fn() -> T, F: Fn(&T, &T) -> T> SegmentTree<T, Z, F> {
    pub fn new(n: usize, zero: Z, f: F) -> Self {
        let n = n.next_power_of_two() << 1;
        Self {
            a: std::iter::repeat_with(|| zero()).take(n).collect(),
            zero,
            f,
        }
    }
    pub fn len(&self) -> usize {
        self.a.len() >> 1
    }
    pub fn set(&mut self, i: usize, value: T) {
        let mut i = self.len() + i;
        self.a[i] = value;
        while (i >> 1) > 0 {
            i >>= 1;
            self.a[i] = (self.f)(&self.a[i << 1], &self.a[(i << 1) + 1]);
        }
    }
    pub fn update(&mut self, i: usize, value: &T) -> &T {
        let j = self.len() + i;
        self.set(i, (self.f)(&value, &self.a[j]));
        &self.a[j]
    }
    // [l, r)
    pub fn fold(&self, l: usize, r: usize) -> T {
        let mut l = self.len() + l;
        let mut r = self.len() + r;
        let mut x = (self.zero)();
        let mut y = (self.zero)();
        while l != r {
            if l & 1 == 1 {
                x = (self.f)(&x, &self.a[l]);
                l += 1;
            }
            if r & 1 == 1 {
                r -= 1;
                y = (self.f)(&self.a[r], &y);
            }
            l >>= 1;
            r >>= 1;
        }
        (self.f)(&x, &y)
    }
}


// -----------------------------------------------------------------------------
pub mod kyoproio {
    #![warn(unused)]
    use std::io::prelude::*;
    pub trait Input {
        fn str(&mut self) -> &str;
        fn bytes(&mut self) -> &[u8] {
            self.str().as_ref()
        }
        fn input<T: InputParse>(&mut self) -> T {
            T::input(self)
        }
        fn iter<T: InputParse>(&mut self) -> Iter<T, Self> {
            Iter(self, std::marker::PhantomData)
        }
        fn seq<T: InputParse, B: std::iter::FromIterator<T>>(&mut self, n: usize) -> B {
            self.iter().take(n).collect()
        }
    }
    pub struct KInput<R> {
        src: R,
        buf: String,
        pos: usize,
    }
    impl<R: BufRead> KInput<R> {
        pub fn new(src: R) -> Self {
            Self {
                src,
                buf: String::with_capacity(1024),
                pos: 0,
            }
        }
    }
    impl<R: BufRead> Input for KInput<R> {
        fn str(&mut self) -> &str {
            loop {
                if self.pos >= self.buf.len() {
                    self.pos = 0;
                    self.buf.clear();
                    if self.src.read_line(&mut self.buf).expect("io error") == 0 {
                        return &self.buf;
                    }
                }
                let range = self.pos
                    ..self.buf[self.pos..]
                        .find(|c: char| c.is_ascii_whitespace())
                        .map(|i| i + self.pos)
                        .unwrap_or_else(|| self.buf.len());
                self.pos = range.end + 1;
                if range.end > range.start {
                    return &self.buf[range];
                }
            }
        }
    }
    pub struct Iter<'a, T, I: ?Sized>(&'a mut I, std::marker::PhantomData<*const T>);
    impl<'a, T: InputParse, I: Input + ?Sized> Iterator for Iter<'a, T, I> {
        type Item = T;
        fn next(&mut self) -> Option<Self::Item> {
            Some(self.0.input())
        }
    }
    pub trait InputParse: Sized {
        fn input<I: Input + ?Sized>(input: &mut I) -> Self;
    }
    impl InputParse for Vec<u8> {
        fn input<I: Input + ?Sized>(input: &mut I) -> Self {
            input.bytes().to_owned()
        }
    }
    macro_rules! from_str_impl {
        { $($T:ty)* } => {
            $(impl InputParse for $T {
                fn input<I: Input + ?Sized>(input: &mut I) -> Self {
                    input.str().parse::<$T>().expect("parse error")
                }
            })*
        };
    }
    from_str_impl! {
        String char bool f32 f64 isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128
    }
    macro_rules! tuple_impl {
        ($H:ident $($T:ident)*) => {
            impl<$H: InputParse, $($T: InputParse),*> InputParse for ($H, $($T),*) {
                fn input<I: Input + ?Sized>(input: &mut I) -> Self {
                    ($H::input(input), $($T::input(input)),*)
                }
            }
            tuple_impl!($($T)*);
        };
        () => {};
    }
    tuple_impl!(A B C D E F G);
}
0