結果

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

コンパイルメッセージ
error[E0782]: trait objects must include the `dyn` keyword
  --> src/main.rs:64:13
   |
64 |     op: Box<Fn(i64, i64) -> i64 + 'a>,
   |             ^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `dyn` keyword before this trait
   |
64 |     op: Box<dyn Fn(i64, i64) -> i64 + 'a>,
   |             +++

For more information about this error, try `rustc --explain E0782`.
error: could not compile `main` (bin "main") due to 1 previous error

ソースコード

diff #

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let q = scan.read::<usize>();
    let a = scan.readn::<i64>(n);
    let mut b = vec![];
    for i in 0..n - 1 {
        b.push(a[i+1] - a[i]);
    }
    let mut c = vec![];
    for i in 0..n - 1 {
        if b[i] == 0 {c.push(0);} else {c.push(1);}
    }
    let mut segtree = SegTree::new(c, 0, |a, b| a + b);
//    dbg!(&b);
    for _ in 0..q {
        let op = scan.read::<usize>();
        let l = scan.read::<usize>()-1;
        let r = scan.read::<usize>()-1;
        if op == 1 {
            let x = scan.read::<i64>();
            if l >= 1 {b[l-1] += x;}
            if r < n-1 {b[r] -= x;}
//            dbg!(&b);
            if l>=1 {
                if b[l-1] == 0 {segtree.set(l-1, 0);} else {segtree.set(l-1, 1);}
            }
            if r < n-1 {
                if b[r] == 0 {segtree.set(r, 0);} else {segtree.set(r, 1);}
            }
        } else {
            println!("{}", segtree.get(l..r)+1);
        }
    }
}


use std::ops::Range;

#[allow(dead_code)]
fn contains(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.start <= q.start && q.end <= p.end
}

#[allow(dead_code)]
fn separates(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.end <= q.start || q.end <= p.start
}

#[allow(dead_code)]
fn size(p: &Range<usize>) -> usize {
    p.end - p.start
}

#[allow(dead_code)]
struct SegTree<'a> {
    node: Vec<i64>,
    zero: i64,
    op: Box<Fn(i64, i64) -> i64 + 'a>,
    n: usize,
}

#[allow(dead_code)]
impl <'a> SegTree<'a> {
    fn new<B>(v: Vec<i64>, zero: i64, op: B) -> SegTree<'a> where B: Fn(i64, i64) -> i64 + 'a {
        let size = v.len();
        let mut n = 1usize;
        while n < size {n *= 2}
        let mut node = vec![zero; 2*n-1];
        for i in 0..size {node[i+n-1] = v[i]}
        for i in (0..n - 1).rev() {node[i] = op(node[2*i+1], node[2*i+2])}
        SegTree {node: node, zero: zero, op: Box::new(op), n: n}
    }

    fn set(&mut self, mut x: usize, value: i64) {
        x += self.n - 1;
        self.node[x] = value;
        while x > 0 {
            x = (x - 1) / 2;
            self.node[x] = (self.op)(self.node[2*x+1], self.node[2*x+2]);
        }
    }

    fn get_one(&self, i: usize) -> i64 {self.getrec(i..i+1, 0, 0..self.n)}
    fn get(&self, query: Range<usize>) -> i64 {self.getrec(query, 0, 0..self.n)}

    fn getrec(&self, query: Range<usize>, k: usize, range: Range<usize>) -> i64 {
        if separates(&query, &range) {return self.zero}
        if contains(&query, &range) {return self.node[k]}
        let vl = self.getrec(query.clone(), 2*k+1, range.start..(range.start + range.end)/2);
        let vr = self.getrec(query.clone(), 2*k+2, (range.start + range.end)/2..range.end);
        (self.op)(vl, vr)
    }
}

// region template
#[allow(dead_code)]
fn iu(i: isize) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> isize { i as isize }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion
0