結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー koba-e964koba-e964
提出日時 2021-10-29 22:01:06
言語 Rust
(1.77.0)
結果
AC  
実行時間 237 ms / 4,000 ms
コード長 3,075 bytes
コンパイル時間 1,382 ms
コンパイル使用メモリ 169,844 KB
実行使用メモリ 43,392 KB
最終ジャッジ日時 2024-04-16 14:52:50
合計ジャッジ時間 5,636 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 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 5 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 30 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 12 ms
5,632 KB
testcase_18 AC 36 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 36 ms
5,376 KB
testcase_22 AC 73 ms
16,128 KB
testcase_23 AC 74 ms
14,464 KB
testcase_24 AC 52 ms
10,752 KB
testcase_25 AC 84 ms
23,424 KB
testcase_26 AC 70 ms
18,816 KB
testcase_27 AC 65 ms
26,368 KB
testcase_28 AC 94 ms
25,472 KB
testcase_29 AC 90 ms
24,320 KB
testcase_30 AC 83 ms
28,800 KB
testcase_31 AC 56 ms
14,336 KB
testcase_32 AC 102 ms
38,760 KB
testcase_33 AC 106 ms
40,004 KB
testcase_34 AC 102 ms
36,992 KB
testcase_35 AC 101 ms
33,152 KB
testcase_36 AC 101 ms
36,440 KB
testcase_37 AC 236 ms
5,376 KB
testcase_38 AC 237 ms
5,376 KB
testcase_39 AC 237 ms
5,376 KB
testcase_40 AC 236 ms
5,376 KB
testcase_41 AC 236 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 35 ms
43,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
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, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

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() {
    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