結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 4 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 28 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 12 ms
5,760 KB
testcase_18 AC 34 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 33 ms
5,376 KB
testcase_22 AC 63 ms
16,208 KB
testcase_23 AC 67 ms
14,604 KB
testcase_24 AC 46 ms
10,880 KB
testcase_25 AC 71 ms
23,552 KB
testcase_26 AC 57 ms
18,816 KB
testcase_27 AC 52 ms
26,216 KB
testcase_28 AC 79 ms
25,568 KB
testcase_29 AC 78 ms
24,336 KB
testcase_30 AC 68 ms
28,772 KB
testcase_31 AC 49 ms
14,436 KB
testcase_32 AC 82 ms
38,756 KB
testcase_33 AC 83 ms
40,148 KB
testcase_34 AC 84 ms
36,992 KB
testcase_35 AC 80 ms
33,152 KB
testcase_36 AC 81 ms
36,480 KB
testcase_37 AC 208 ms
5,376 KB
testcase_38 AC 209 ms
5,376 KB
testcase_39 AC 210 ms
5,376 KB
testcase_40 AC 210 ms
5,376 KB
testcase_41 AC 210 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 19 ms
43,392 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