結果

問題 No.230 Splarraay スプラレェーイ
ユーザー akakimidoriakakimidori
提出日時 2019-09-15 16:58:16
言語 Rust
(1.77.0 + proconio)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,843 bytes
コンパイル時間 12,181 ms
コンパイル使用メモリ 387,312 KB
最終ジャッジ日時 2024-11-14 21:39:57
合計ジャッジ時間 13,061 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `,`
 --> src/main.rs:5:20
  |
5 |     fn fold(Self::T, Self::T) -> Self::T;
  |                    ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
5 |     fn fold(_: Self::T, Self::T) -> Self::T;
  |             ~~~~~~~~~~

error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> src/main.rs:5:29
  |
5 |     fn fold(Self::T, Self::T) -> Self::T;
  |                             ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
5 |     fn fold(Self::T, _: Self::T) -> Self::T;
  |                      ~~~~~~~~~~

error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `,`
 --> src/main.rs:6:20
  |
6 |     fn eval(Self::T, Self::E) -> Self::T;
  |                    ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
6 |     fn eval(_: Self::T, Self::E) -> Self::T;
  |             ~~~~~~~~~~

error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> src/main.rs:6:29
  |
6 |     fn eval(Self::T, Self::E) -> Self::T;
  |                             ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
6 |     fn eval(Self::T, _: Self::E) -> Self::T;
  |                      ~~~~~~~~~~

error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `,`
 --> src/main.rs:7:21
  |
7 |     fn merge(Self::E, Self::E) -> Self::E;
  |                     ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed i

ソースコード

diff #

// ---------- begin Lazy Segment Tree ----------
pub trait TE {
    type T: Clone;
    type E: Clone;
    fn fold(Self::T, Self::T) -> Self::T;
    fn eval(Self::T, Self::E) -> Self::T;
    fn merge(Self::E, Self::E) -> Self::E;
    fn e() -> Self::T;
    fn id() -> Self::E;
}

pub struct LazySegmentTree<R: TE> {
    size: usize,
    bit: usize,
    a: Vec<(R::T, R::E)>,
}

impl <R: TE> LazySegmentTree<R> {
    pub fn new(n: usize) -> LazySegmentTree<R> {
        let mut bit = 0;
        while (1 << bit) < n {
            bit += 1;
        }
        LazySegmentTree {
            size: 1 << bit,
            bit: bit,
            a: vec![(R::e(), R::id()); 2 << bit],
        }
    }
    pub fn build_by(z: &[R::T]) -> LazySegmentTree<R> {
        let n = z.len();
        let mut bit = 0;
        while (1 << bit) < n {
            bit += 1;
        }
        let mut a = vec![(R::e(), R::id()); 2 << bit];
        for (a, z) in a[(1 << bit)..].iter_mut().zip(z.iter()) {
            a.0 = z.clone();
        }
        for i in (1..(1 << bit)).rev() {
            let l = R::eval(a[2 * i].0.clone(), a[2 * i].1.clone());
            let r = R::eval(a[2 * i + 1].0.clone(), a[2 * i + 1].1.clone());
            a[i].0 = R::fold(l, r);
        }
        LazySegmentTree {
            size: 1 << bit,
            bit : bit,
            a: a,
        }
    }
    fn eval(&self, k: usize) -> R::T {
        R::eval(self.a[k].0.clone(), self.a[k].1.clone())
    }
    fn propagate(&mut self, x: usize) {
        let x = x + self.size;
        for i in (1..(self.bit + 1)).rev() {
            let k = x >> i;
            self.a[2 * k].1 = R::merge(self.a[2 * k].1.clone(), self.a[k].1.clone());
            self.a[2 * k + 1].1 = R::merge(self.a[2 * k + 1].1.clone(), self.a[k].1.clone());
            self.a[k].1 = R::id();
            self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1));
        }
    }
    fn save(&mut self, x: usize) {
        let x = x + self.size;
        for i in 1..(self.bit + 1) {
            let k = x >> i;
            self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1));
        }
    }
    pub fn update(&mut self, l: usize, r: usize, op: R::E) {
        self.propagate(l);
        self.propagate(r - 1);
        let mut x = l + self.size;
        let mut y = r + self.size;
        while x < y {
            if x & 1 == 1 {
                self.a[x].1 = R::merge(self.a[x].1.clone(), op.clone());
                x += 1;
            }
            if y & 1 == 1 {
                y -= 1;
                self.a[y].1 = R::merge(self.a[y].1.clone(), op.clone());
            }
            x >>= 1;
            y >>= 1;
        }
        self.save(l);
        self.save(r - 1);
    }
    pub fn find(&mut self, l: usize, r: usize) -> R::T {
        self.propagate(l);
        self.propagate(r - 1);
        let mut x = l + self.size;
        let mut y = r + self.size;
        let mut p = R::e();
        let mut q = R::e();
        while x < y {
            if x & 1 == 1 {
                p = R::fold(p, self.eval(x));
                x += 1;
            }
            if y & 1 == 1 {
                y -= 1;
                q = R::fold(self.eval(y), q);
            }
            x >>= 1;
            y >>= 1;
        }
        R::fold(p, q)
    }
}

// ---------- end Lazy Segment Tree ----------
//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
//

struct R;
impl TE for R {
    type T = (u32, u32, u32);
    type E = u8;
    fn fold(l: Self::T, r: Self::T) -> Self::T {
        (l.0 + r.0, l.1 + r.1, l.2 + r.2)
    }
    fn eval(p: Self::T, x: Self::E) -> Self::T {
        if x == 0 {
            p
        } else if x == 1 {
            (p.2, 0, p.2)
        } else {
            (0, p.2, p.2)
        }
    }
    fn merge(f: Self::E, g: Self::E) -> Self::E {
        if g != 0 {g} else {f}
    }
    fn e() -> Self::T {
        (0, 0, 0)
    }
    fn id() -> Self::E {
        0
    }
}

fn run() {
    input! {
        n: usize,
        q: usize,
        x: [(u8, usize, usize); q],
    }
    let mut a = 0u64;
    let mut b = 0u64;
    let mut s = LazySegmentTree::<R>::build_by(&vec![(0, 0, 1); n]);
    for (x, l, r) in x {
        let r = r + 1;
        match x {
            0 => {
                let (x, y, _) = s.find(l, r);
                if x > y {
                    a += x as u64;
                } else if x < y {
                    b += y as u64;
                }
            },
            1 => s.update(l, r, 1),
            _ => s.update(l, r, 2),
        }
    }
    let (x, y, _) = s.find(0, n);
    a += x as u64;
    b += y as u64;
    println!("{} {}", a, b);
}

fn main() {
    run();
}
0