Submission #5448466


Source Code Expand

#[allow(unused_macros)]
macro_rules! input {
    ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; let mut next = || { iter . next ( ) . unwrap ( ) } ; input_inner ! { next , $ ( $ r ) * } };
    ( $ ( $ 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 ) * } };
}
#[allow(unused_macros)]
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 ) * } };
}
#[allow(unused_macros)]
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 : ty ) => { $ next ( ) . parse ::<$ t > ( ) . expect ( "Parse error" ) };
}

fn main() {
    input! {
        n: usize, m: usize, q: usize,
        trains: [(usize1, usize1); m],
        queries: [(usize1, usize1); q]
    }

    let mut qt = vec![];
    qt.extend(trains.into_iter().map(|(l,r)| (l,r,None)));
    qt.extend(queries.into_iter().enumerate().map(|(i, (l,r))| (l,r,Some(i))));
    qt.sort_by_key(|&(_,r,qi)| (r, qi));

    let mut sq = SqDecomposition::new(n);
    let mut ret = vec![0u64;q];
    for i in 0..qt.len() {
        let (l,r,qi) = qt[i];
        if let Some(src) = qi {
            ret[src] = sq.sum(r+1) - sq.sum(l);
        } else {
            sq.all[l] += 1;
            sq.sq[l/sq.d] += 1;
        }
    }
    for i in 0..q {
        println!("{}", ret[i]);
    }
}

#[derive(Debug)]
#[allow(dead_code)]
struct SqDecomposition {
    all: Vec<u64>,
    sq: Vec<u64>,
    d: usize,
}
#[allow(dead_code)]
impl SqDecomposition {
    fn new(n: usize) -> SqDecomposition {
        let d = (n as f64).sqrt().ceil() as usize;
        SqDecomposition {
            all: vec![0; n],
            sq: vec![0; d],
            d: d,
        }
    }
    #[doc = " [0,n)"]
    fn sum(&self, n: usize) -> u64 {
        let mut ret = 0;
        for i in 0..n / self.d {
            ret += self.sq[i];
        }
        for i in 0..n % self.d {
            ret += self.all[n - n % self.d + i];
        }
        ret
    }
}

Submission Info

Submission Time
Task D - AtCoder Express 2
User henoc
Language Rust (1.15.1)
Score 400
Code Size 2936 Byte
Status AC
Exec Time 270 ms
Memory 22692 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
in01.txt AC 2 ms 4352 KB
in02.txt AC 2 ms 4352 KB
in03.txt AC 2 ms 4352 KB
in04.txt AC 2 ms 4352 KB
in05.txt AC 268 ms 22692 KB
in06.txt AC 270 ms 22692 KB
in07.txt AC 267 ms 22692 KB
in08.txt AC 269 ms 22692 KB
in09.txt AC 242 ms 22692 KB
in10.txt AC 253 ms 22692 KB
sample_01.txt AC 2 ms 4352 KB
sample_02.txt AC 2 ms 4352 KB
sample_03.txt AC 2 ms 4352 KB