結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー koba-e964koba-e964
提出日時 2021-10-29 22:00:12
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 198 ms / 4,000 ms
コード長 3,824 bytes
コンパイル時間 13,533 ms
コンパイル使用メモリ 388,448 KB
実行使用メモリ 43,480 KB
最終ジャッジ日時 2024-10-07 11:14:42
合計ジャッジ時間 17,420 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 1 ms
6,820 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 4 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 3 ms
6,816 KB
testcase_09 AC 1 ms
6,816 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 1 ms
6,820 KB
testcase_12 AC 5 ms
6,820 KB
testcase_13 AC 5 ms
6,820 KB
testcase_14 AC 26 ms
6,816 KB
testcase_15 AC 6 ms
6,824 KB
testcase_16 AC 7 ms
6,816 KB
testcase_17 AC 11 ms
6,816 KB
testcase_18 AC 31 ms
6,816 KB
testcase_19 AC 9 ms
6,820 KB
testcase_20 AC 5 ms
6,816 KB
testcase_21 AC 29 ms
6,820 KB
testcase_22 AC 58 ms
16,048 KB
testcase_23 AC 58 ms
14,588 KB
testcase_24 AC 40 ms
10,828 KB
testcase_25 AC 67 ms
23,584 KB
testcase_26 AC 49 ms
18,964 KB
testcase_27 AC 44 ms
26,204 KB
testcase_28 AC 71 ms
25,540 KB
testcase_29 AC 71 ms
24,404 KB
testcase_30 AC 63 ms
28,796 KB
testcase_31 AC 43 ms
14,356 KB
testcase_32 AC 75 ms
38,816 KB
testcase_33 AC 76 ms
39,964 KB
testcase_34 AC 76 ms
37,024 KB
testcase_35 AC 73 ms
33,072 KB
testcase_36 AC 77 ms
36,524 KB
testcase_37 AC 197 ms
6,820 KB
testcase_38 AC 195 ms
6,816 KB
testcase_39 AC 198 ms
6,820 KB
testcase_40 AC 195 ms
6,820 KB
testcase_41 AC 196 ms
6,816 KB
testcase_42 AC 1 ms
6,820 KB
testcase_43 AC 19 ms
43,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($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)*}
    };
}

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)*}
    };
}

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:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn dfs(k: usize, l: i64, r: i64, c: usize,
       len: &[i64], x: &[[i64; 26]], y: &[[i64; 26]]) -> i64 {
    if l == r {
        return 0;
    }
    if k == 1 {
        return x[r as usize][c] - x[l as usize][c];
    }
    assert!(r <= len[k]);
    assert!(l <= r);
    if (l, r) == (0, len[k]) {
        let mut a = y[y.len() - 1][c];
        let b = dfs(k - 1, 0, len[k - 1], c, len, x, y);
        a += 2 * b;
        return a;
    }
    let mut a = 0;
    let mid1 = len[k - 1];
    let mid2 = len[k] - len[k - 1];
    if l < mid1 {
        let b = dfs(k - 1, l, min(r, mid1), c, &len, x, y);
        a += b;
    }
    if l < mid2 && r > mid1 {
        let lidx = (max(l, mid1) - mid1) as usize;
        let ridx = (min(r, mid2) - mid1) as usize;
        a += y[ridx][c] - y[lidx][c];
    }
    if r > mid2 {
        let b = dfs(k - 1,
                    len[k - 1] - (r - mid2),
                    len[k - 1] - (max(l, mid2) - mid2), c, &len, x, y);
        a += b;
    }
    a
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        x: chars,
        y: chars,
        q: usize,
        lrc: [(i64, i64, char); q],
    }
    fn prepare(x: &[char]) -> Vec<[i64; 26]> {
        let n = x.len();
        let mut dp = vec![[0; 26]; n + 1];
        for i in 0..n {
            dp[i + 1] = dp[i];
            dp[i + 1][(x[i] as u8 - b'a') as usize] += 1;
        }
        dp
    }
    let dpx = prepare(&x);
    let dpy = prepare(&y);
    let mut len = vec![0, x.len() as i64];
    let mut cur = x.len() as i64;
    let mut idx = 1;
    while cur < 1i64 << 30 {
        cur = 2 * cur + y.len() as i64;
        idx += 1;
        len.push(cur);
    }
    for (l, r, c) in lrc {
        puts!("{}\n", dfs(idx, l - 1, r, (c as u8 - b'a') as _, &len, &dpx, &dpy));
    }
}
0