提出 #53571346


ソースコード 拡げる

#![allow(unused)]

fn main() {
    let n = read::<usize>();
    let mut s = reads();

    let id = |c: char| c as usize - 'a' as usize;

    let mut bits = vec![BIT::<i32>::new(n); 26];
    for i in 0..n {
        bits[id(s[i])].add(i, 1);
    }

    let mut ans = vec![];
    let q = read::<usize>();
    for qid in 0..q {
        let ask = readv::<String>();
        if ask[0] == "1" {
            let i = ask[1].parse::<usize>().unwrap() - 1;
            let c = ask[2].chars().next().unwrap();
            if s[i] != c {
                bits[id(s[i])].add(i, -1);
                s[i] = c;
                bits[id(s[i])].add(i, 1);
            }
        } else {
            let mut val = 0;
            let l = ask[1].parse::<usize>().unwrap() - 1;
            let r = ask[2].parse::<usize>().unwrap() - 1;
            for c in 0..26 {
                if bits[c].sum(l, r + 1) > 0 {
                    val += 1;
                }
            }
            ans.push(val);
        }
    }

    println!("{}", join(&ans, "\n"));
}

#[derive(Clone)]
struct BIT<T> {
    dat: Vec<T>,
}

impl<T: Clone + Default + std::ops::AddAssign + std::ops::Sub<Output = T>> BIT<T> {
    fn new(n: usize) -> Self {
        Self {
            dat: vec![T::default(); n + 1],
        }
    }

    // 0-based
    fn add(&mut self, mut i: usize, x: T) {
        i += 1; // convert to 1-based
        while i < self.dat.len() {
            self.dat[i] += x.clone();
            i += i & (!i + 1); // i & (-i)
        }
    }

    // 0..=i, 0-based
    fn pref(&self, mut i: usize) -> T {
        let mut res = T::default();
        i += 1; // convert to 1-based
        while i > 0 {
            res += self.dat[i].clone();
            i -= i & (!i + 1);
        }
        res
    }

    // l..i, 0-based
    fn sum(&self, mut l: usize, mut r: usize) -> T {
        if r == 0 {
            T::default()
        } else if l >= 1 {
            self.pref(r - 1) - self.pref(l - 1)
        } else {
            self.pref(r - 1)
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect::<_>()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 E - Simple String Queries
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 500
コード長 2782 Byte
結果 AC
実行時間 68 ms
メモリ 56008 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 43
セット名 テストケース
Sample 00-sample-00
All 00-sample-00, 01-handmade-00, 01-handmade-01, 01-handmade-02, 01-handmade-03, 01-handmade-04, 01-handmade-05, 01-handmade-06, 01-handmade-07, 01-handmade-08, 01-handmade-09, 01-handmade-10, 01-handmade-11, 02-small-00, 02-small-01, 02-small-02, 02-small-03, 02-small-04, 02-small-05, 02-small-06, 02-small-07, 02-small-08, 02-small-09, 02-small-10, 02-small-11, 02-small-12, 02-small-13, 02-small-14, 02-small-15, 02-small-16, 02-small-17, 02-small-18, 02-small-19, 03-large-00, 03-large-01, 03-large-02, 03-large-03, 03-large-04, 03-large-05, 03-large-06, 03-large-07, 03-large-08, 03-large-09
ケース名 結果 実行時間 メモリ
00-sample-00 AC 1 ms 1868 KiB
01-handmade-00 AC 49 ms 53344 KiB
01-handmade-01 AC 0 ms 1852 KiB
01-handmade-02 AC 40 ms 55072 KiB
01-handmade-03 AC 68 ms 56004 KiB
01-handmade-04 AC 39 ms 56008 KiB
01-handmade-05 AC 48 ms 53356 KiB
01-handmade-06 AC 57 ms 55284 KiB
01-handmade-07 AC 48 ms 55080 KiB
01-handmade-08 AC 35 ms 53864 KiB
01-handmade-09 AC 40 ms 53852 KiB
01-handmade-10 AC 35 ms 53864 KiB
01-handmade-11 AC 34 ms 53916 KiB
02-small-00 AC 3 ms 2292 KiB
02-small-01 AC 4 ms 2388 KiB
02-small-02 AC 3 ms 2144 KiB
02-small-03 AC 5 ms 2420 KiB
02-small-04 AC 3 ms 2180 KiB
02-small-05 AC 3 ms 2216 KiB
02-small-06 AC 5 ms 2400 KiB
02-small-07 AC 3 ms 2176 KiB
02-small-08 AC 3 ms 2180 KiB
02-small-09 AC 3 ms 2276 KiB
02-small-10 AC 4 ms 2356 KiB
02-small-11 AC 3 ms 2216 KiB
02-small-12 AC 3 ms 2200 KiB
02-small-13 AC 4 ms 2308 KiB
02-small-14 AC 5 ms 2340 KiB
02-small-15 AC 4 ms 2420 KiB
02-small-16 AC 3 ms 2336 KiB
02-small-17 AC 3 ms 2348 KiB
02-small-18 AC 4 ms 2336 KiB
02-small-19 AC 4 ms 2336 KiB
03-large-00 AC 54 ms 55216 KiB
03-large-01 AC 54 ms 55280 KiB
03-large-02 AC 55 ms 55188 KiB
03-large-03 AC 53 ms 55192 KiB
03-large-04 AC 54 ms 55136 KiB
03-large-05 AC 54 ms 55184 KiB
03-large-06 AC 53 ms 55272 KiB
03-large-07 AC 53 ms 55120 KiB
03-large-08 AC 53 ms 55276 KiB
03-large-09 AC 53 ms 55216 KiB