QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#378181#8576. Symphony in c++ majorucup-team635#ML 0ms2156kbRust7.1kb2024-04-06 09:12:552024-04-06 09:12:56

Judging History

你现在查看的是最新测评结果

  • [2024-04-06 09:12:56]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:2156kb
  • [2024-04-06 09:12:55]
  • 提交

answer

// ---------- begin segment tree Point Update Range Query ----------
pub struct SegmentTreePURQ<T, F> {
    n: usize,
    size: usize,
    data: Vec<T>,
    e: T,
    op: F,
}

impl<T, F> SegmentTreePURQ<T, F>
where
    T: Clone,
    F: Fn(&T, &T) -> T,
{
    pub fn new(n: usize, e: T, op: F) -> Self {
        assert!(n > 0);
        let size = n.next_power_of_two();
        let data = vec![e.clone(); 2 * size];
        SegmentTreePURQ {
            n,
            size,
            data,
            e,
            op,
        }
    }
    pub fn update_tmp(&mut self, x: usize, v: T) {
        assert!(x < self.n);
        self.data[x + self.size] = v;
    }
    pub fn update_all(&mut self) {
        for i in (1..self.size).rev() {
            self.data[i] = (self.op)(&self.data[2 * i], &self.data[2 * i + 1]);
        }
    }
    pub fn update(&mut self, x: usize, v: T) {
        assert!(x < self.n);
        let mut x = x + self.size;
        self.data[x] = v;
        x >>= 1;
        while x > 0 {
            self.data[x] = (self.op)(&self.data[2 * x], &self.data[2 * x + 1]);
            x >>= 1;
        }
    }
    pub fn find(&self, l: usize, r: usize) -> T {
        assert!(l <= r && r <= self.n);
        if l == r {
            return self.e.clone();
        }
        let mut l = self.size + l;
        let mut r = self.size + r;
        let mut x = self.e.clone();
        let mut y = self.e.clone();
        while l < r {
            if l & 1 == 1 {
                x = (self.op)(&x, &self.data[l]);
                l += 1;
            }
            if r & 1 == 1 {
                r -= 1;
                y = (self.op)(&self.data[r], &y);
            }
            l >>= 1;
            r >>= 1;
        }
        (self.op)(&x, &y)
    }
    pub fn max_right<P>(&self, l: usize, f: P) -> usize
    where
        P: Fn(&T) -> bool,
    {
        assert!(l <= self.n);
        assert!(f(&self.e));
        if l == self.n {
            return self.n;
        }
        let mut l = l + self.size;
        let mut sum = self.e.clone();
        while {
            l >>= l.trailing_zeros();
            let v = (self.op)(&sum, &self.data[l]);
            if !f(&v) {
                while l < self.size {
                    l <<= 1;
                    let v = (self.op)(&sum, &self.data[l]);
                    if f(&v) {
                        sum = v;
                        l += 1;
                    }
                }
                return l - self.size;
            }
            sum = v;
            l += 1;
            l.count_ones() > 1
        } {}
        self.n
    }
    pub fn min_left<P>(&self, r: usize, f: P) -> usize
    where
        P: Fn(&T) -> bool,
    {
        assert!(r <= self.n);
        assert!(f(&self.e));
        if r == 0 {
            return 0;
        }
        let mut r = r + self.size;
        let mut sum = self.e.clone();
        while {
            r -= 1;
            while r > 1 && r & 1 == 1 {
                r >>= 1;
            }
            let v = (self.op)(&self.data[r], &sum);
            if !f(&v) {
                while r < self.size {
                    r = 2 * r + 1;
                    let v = (self.op)(&self.data[r], &sum);
                    if f(&v) {
                        sum = v;
                        r -= 1;
                    }
                }
                return r + 1 - self.size;
            }
            sum = v;
            (r & (!r + 1)) != r
        } {}
        0
    }
}
// ---------- end segment tree Point Update Range Query ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::collections::*;
use std::io::Write;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let n: usize = sc.next();
    let q: usize = sc.next();
    let c = "aieo".bytes().collect::<Vec<_>>();
    let e = vec!["do", "do", "re", "mi", "fa", "so", "la", "ti"];
    let e = e
        .into_iter()
        .map(|e| {
            let e = e.bytes().collect::<Vec<_>>();
            [e[0], e[1]]
        })
        .collect::<Vec<_>>();
    let mut trans = vec![];
    let mut id = [(0, 0); 16];
    for i in 0..16 {
        id[i].0 = i;
    }
    let id = id;
    for i in 0..26 {
        let mut map = id;
        let x = b'a' + i;
        if let Some(k) = c.iter().position(|c| *c == x) {
            for j in 0..16 {
                if j >> k & 1 == 1 {
                    map[j ^ (1 << k)].0 = j;
                }
            }
        } else {
            for (bit, map) in map.iter_mut().enumerate() {
                for e in e.iter().filter(|e| e[0] == x) {
                    let k = c.iter().position(|c| *c == e[1]).unwrap();
                    if bit >> k & 1 == 1 {
                        map.0 = 0;
                        map.1 = 1;
                    }
                }
            }
        }
        trans.push(map);
    }
    type T = [(usize, i32); 16];
    let merge = |l: &T, r: &T| -> T {
        let mut ans = id;
        for (ans, r) in ans.iter_mut().zip(r.iter()) {
            let p = l[r.0];
            *ans = (p.0, p.1 + r.1);
        }
        ans
    };
    let s = sc.next_bytes();
    let mut seg = SegmentTreePURQ::new(s.len(), id, merge);
    for (i, s) in s.iter().enumerate() {
        seg.update_tmp(i, trans[(*s - b'a') as usize]);
    }
    seg.update_all();
    for _ in 0..q {
        let op = sc.next::<String>();
        let l = sc.next::<usize>() - 1;
        let r = sc.next::<usize>();
        if op == "?" {
            let dp = seg.find(l, r);
            let ans = r - l - dp[0].1 as usize * 2;
            writeln!(out, "{}", ans).ok();
        } else {
            let s = sc.next_bytes();
            for (i, s) in s.iter().enumerate() {
                seg.update(l + i, trans[(*s - b'a') as usize]);
            }
        }
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 2156kb

input:

8 10
eldorado
? 1 3
? 1 8
# 6 7 it
? 1 8
# 3 3 t
? 1 8
# 1 8 streamer
? 1 8
# 1 8 symphony
? 1 8

output:

3
4
6
6
6
6

result:

ok 6 numbers

Test #2:

score: -100
Memory Limit Exceeded

input:

500000 300000
rfsraltzititrofomloldlaefikdemaddaafmimiioiuaueiaedifacmxamttiiitaaltiiireexeafsaaraedosotlaioootaiosizlifiioadhnxiofsasaflleaiisstaotdlliulilxatrpdaraaraaoiaeoooiumwuumarlifesroloistedoaaieolisaoatamsredorrfiifaaidfeassfioaiasmstomasallsftsrfaiiirteomaeiirefldmlaoaofrxlaeuilioirafotoa...

output:

122151
133262
96922
55212
91547
148150
73505
4097
300798
54037
56741
265921
127608
170707
79236
209443
84732
83219
184042
77169
94062
172946
202750
97798
92449
67243
171524
145772
53856
165837
104913
179165
35068
55893
17287
74510
319355
244761
118810
162815
175172
136079
43107
237581
112894
48610
1...

result: