結果

問題 No.876 Range Compress Query
ユーザー akiradeveloperakiradeveloper
提出日時 2019-09-06 22:53:30
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 10,582 bytes
コンパイル時間 13,618 ms
コンパイル使用メモリ 378,292 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-06-24 20:28:32
合計ジャッジ時間 17,233 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 266 ms
8,960 KB
testcase_12 AC 228 ms
7,936 KB
testcase_13 AC 225 ms
8,192 KB
testcase_14 AC 270 ms
8,832 KB
testcase_15 AC 188 ms
8,064 KB
testcase_16 AC 261 ms
9,472 KB
testcase_17 AC 260 ms
9,728 KB
testcase_18 AC 278 ms
9,728 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type `RMQ_RAQ` should have an upper camel case name
   --> src/main.rs:306:8
    |
306 | struct RMQ_RAQ;
    |        ^^^^^^^ help: convert the identifier to upper camel case: `RmqRaq`
    |
    = note: `#[warn(non_camel_case_types)]` on by default

warning: variable `N` should have a snake case name
   --> src/main.rs:108:9
    |
108 |         N:usize,Q:usize, a:[usize;N],
    |         ^ help: convert the identifier to snake case: `n`
    |
    = note: `#[warn(non_snake_case)]` on by default

warning: variable `Q` should have a snake case name
   --> src/main.rs:108:17
    |
108 |         N:usize,Q:usize, a:[usize;N],
    |                 ^ help: convert the identifier to snake case: `q`

ソースコード

diff #

#[doc = " 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;
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[macro_export]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; }
use std::io;
use std::io::BufRead;
use std::str;
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}
impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}
impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                if len == 0 {
                    break;
                }
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
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();
    }
}
#[derive(Debug)]
enum T {
    U(usize,usize,usize),
    Q(usize,usize),
}
fn solve() {
    input!{
        new_stdin_parser = parser,
        N:usize,Q:usize, a:[usize;N],
    }
    let mut q=vec![];
    for _ in 0..Q {
        input!{
            parser=parser,
            t:usize,
        }
        if t==1 {
            input!{
                parser=parser,
                l:usize,r:usize,x:usize,
            }
            q.push(T::U(l,r,x));
        }
        else if t==2 {
            input!{
                parser=parser,
                l:usize,r:usize,
            }
            q.push(T::Q(l,r));
        }
    }
    let mut group = BIT::new(N);
    let mut value: SEG<RMQ_RAQ> = SEG::new(0,N+1);
    let mut acc=0;
    for i in 0..N {
        let ai=a[i];
        if ai!=acc {
            group.add(i+1, 1);
            acc=ai;
        }
        value.update(i+1, i+2, ai);
    }
    for qi in q {
        match qi {
            T::U(l,r,x) => {
                let mut left_eq = true;
                if l>=2 {
                    let al=value.query(l, l+1);
                    let all=value.query(l-1, l);
                    // dbg!(al,all);
                    if al!=all {
                        left_eq=false;
                    }
                }
                let mut right_eq = true;
                if r+1<=N {
                    let ar=value.query(r, r+1);
                    let arr=value.query(r+1, r+2);
                    if ar!=arr {
                        right_eq=false;
                    }
                }
                value.update(l, r+1, x);
                // dbg!(left_eq,right_eq);
                if l>=2 {
                    let al=value.query(l, l+1);
                    let all=value.query(l-1, l);
                    if left_eq && al!=all {
                        group.add(l,1);
                    }
                    else if !left_eq && al==all {
                        group.add(l,-1);
                    }
                }
                if r+1<=N {
                    let ar=value.query(r, r+1);
                    let arr=value.query(r+1, r+2);
                    if right_eq && ar!=arr {
                        group.add(r+1,1);
                    }
                    else if !right_eq && ar==arr {
                        group.add(r+1,-1);
                    }
                }
            },
            T::Q(l,r) => {
                let vl=group.sum(l);
                let vr=group.sum(r);
                // dbg!((vl,vr));
                println!("{}",vr-vl+1);
            }
        }
    }
}
#[allow(dead_code)]
#[doc = " Binary Indexed Tree of usize"]
pub struct BIT {
    buf: Vec<i64>,
}
#[allow(dead_code)]
impl BIT {
    pub fn new(n: usize) -> BIT {
        BIT {
            buf: vec![0; n + 1],
        }
    }
    pub fn sum(&self, i: usize) -> i64 {
        let mut i = i;
        let mut s = 0;
        while i > 0 {
            s += self.buf[i];
            i &= i - 1;
        }
        s
    }
    pub fn add(&mut self, i: usize, x: i64) {
        let mut i = i as i64;
        while i < self.buf.len() as i64 {
            self.buf[i as usize] += x;
            i += i & -i;
        }
    }
}
#[doc = " https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html"]
trait SEGImpl {
    type Monoid: Copy;
    type OperatorMonoid: Copy + PartialEq;
    fn m0() -> Self::Monoid;
    fn om0() -> Self::OperatorMonoid;
    fn f(x: Self::Monoid, y: Self::Monoid) -> Self::Monoid;
    fn g(x: Self::Monoid, y: Self::OperatorMonoid, len: usize) -> Self::Monoid;
    fn h(x: Self::OperatorMonoid, y: Self::OperatorMonoid) -> Self::OperatorMonoid;
}
struct SEG<T: SEGImpl> {
    n: usize,
    data: Vec<T::Monoid>,
    lazy: Vec<T::OperatorMonoid>,
}
impl<T: SEGImpl> SEG<T> {
    fn new(init: T::Monoid, n: usize) -> SEG<T> {
        let mut m = 1;
        while m < n {
            m *= 2;
        }
        SEG {
            n: m,
            data: vec![init; m * 2],
            lazy: vec![T::om0(); m * 2],
        }
    }
    fn propagate(&mut self, k: usize, len: usize) {
        if self.lazy[k] != T::om0() {
            if k < self.n {
                self.lazy[2 * k + 0] = T::h(self.lazy[2 * k + 0], self.lazy[k]);
                self.lazy[2 * k + 1] = T::h(self.lazy[2 * k + 1], self.lazy[k]);
            }
            self.data[k] = T::g(self.data[k], self.lazy[k], len);
            self.lazy[k] = T::om0();
        }
    }
    fn do_update(
        &mut self,
        a: usize,
        b: usize,
        x: T::OperatorMonoid,
        k: usize,
        l: usize,
        r: usize,
    ) -> T::Monoid {
        self.propagate(k, r - l);
        if r <= a || b <= l {
            self.data[k]
        } else if a <= l && r <= b {
            self.lazy[k] = T::h(self.lazy[k], x);
            self.propagate(k, r - l);
            self.data[k]
        } else {
            self.data[k] = T::f(
                self.do_update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                self.do_update(a, b, x, 2 * k + 1, (l + r) >> 1, r),
            );
            self.data[k]
        }
    }
    fn update(&mut self, a: usize, b: usize, x: T::OperatorMonoid) -> T::Monoid {
        let n = self.n;
        self.do_update(a, b, x, 1, 0, n)
    }
    fn do_query(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T::Monoid {
        self.propagate(k, r - l);
        if r <= a || b <= l {
            T::m0()
        } else if a <= l && r <= b {
            self.data[k]
        } else {
            T::f(
                self.do_query(a, b, 2 * k + 0, l, (l + r) >> 1),
                self.do_query(a, b, 2 * k + 1, (l + r) >> 1, r),
            )
        }
    }
    fn query(&mut self, a: usize, b: usize) -> T::Monoid {
        let n = self.n;
        self.do_query(a, b, 1, 0, n)
    }
}
struct RMQ_RAQ;
impl SEGImpl for RMQ_RAQ { 
    type Monoid = usize;
    type OperatorMonoid = usize;
    fn m0() -> Self::Monoid {
        (1<<60)-1
    }
    fn om0() -> Self::OperatorMonoid {
        0
    }
    fn f(x: Self::Monoid, y: Self::Monoid) -> Self::Monoid {
        std::cmp::min(x, y)
    }
    fn g(x: Self::Monoid, y: Self::OperatorMonoid, len: usize) -> Self::Monoid {
        x + len * y
    }
    fn h(x: Self::OperatorMonoid, y: Self::OperatorMonoid) -> Self::OperatorMonoid {
        x + y
    }
}
0