結果

問題 No.2171 OR Assignment
ユーザー koba-e964koba-e964
提出日時 2023-06-22 11:30:28
言語 Rust
(1.77.0)
結果
AC  
実行時間 871 ms / 3,500 ms
コード長 4,728 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 161,120 KB
実行使用メモリ 72,376 KB
最終ジャッジ日時 2023-09-12 06:11:52
合計ジャッジ時間 14,568 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 134 ms
32,776 KB
testcase_13 AC 135 ms
32,760 KB
testcase_14 AC 135 ms
32,768 KB
testcase_15 AC 57 ms
28,436 KB
testcase_16 AC 64 ms
28,552 KB
testcase_17 AC 85 ms
28,460 KB
testcase_18 AC 403 ms
50,900 KB
testcase_19 AC 372 ms
50,636 KB
testcase_20 AC 735 ms
66,516 KB
testcase_21 AC 770 ms
68,032 KB
testcase_22 AC 871 ms
71,796 KB
testcase_23 AC 597 ms
58,892 KB
testcase_24 AC 863 ms
72,376 KB
testcase_25 AC 864 ms
72,016 KB
testcase_26 AC 850 ms
72,368 KB
testcase_27 AC 776 ms
66,768 KB
testcase_28 AC 787 ms
66,764 KB
testcase_29 AC 705 ms
63,348 KB
testcase_30 AC 610 ms
59,608 KB
testcase_31 AC 845 ms
72,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `BufWriter`
 --> Main.rs:5:22
  |
5 | use std::io::{Write, BufWriter};
  |                      ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Write`
 --> Main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^

warning: 2 warnings emitted

ソースコード

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

/**
 * Sparse Table.
 * BiOp should be the type of a binary operator which is
 * associative, commutative and idempotent.
 * (For example, both min and gcd satisfy these properties.)
 * Verified by: AtCoder CODE FESTIVAL 2016 Tournament Round 3 (Parallel) B
 * (http://cf16-tournament-round3-open.contest.atcoder.jp/submissions/1026294)
 */
 struct SparseTable<T, BiOp> {
    biop: BiOp,
    st: Vec<Vec<T>>,
}

impl<T, BiOp> SparseTable<T, BiOp>
    where BiOp: Fn(T, T) -> T,
          T: Copy {
    pub fn new(ary: &[T], biop: BiOp) -> Self {
        let n = ary.len();
        let mut h = 1;
        while 1 << h < n {
            h += 1;
        }
        let mut st: Vec<Vec<T>> = vec![Vec::from(ary); h + 1];
        for i in 0 .. n {
            st[0][i] = ary[i];
        }
        for b in 1 .. (h + 1) {
            if n + 1 < 1 << b {
                break;
            }
            for i in 0 .. (n + 1 - (1 << b)) {
                let next_idx = (1 << (b - 1)) + i;
                st[b][i] = biop(st[b - 1][i], st[b - 1][next_idx]);
            }
        }
        SparseTable {biop: biop, st: st}
    }
    fn top_bit(t: usize) -> usize {
        8 * std::mem::size_of::<usize>() - 1 - t.leading_zeros() as usize
    }
    pub fn query(&self, range: std::ops::Range<usize>) -> T {
        let (f, s) = (range.start, range.end - 1);
        assert!(f <= s);
        let b = Self::top_bit(s + 1 - f);
        let endpoint = s + 1 - (1 << b);
        (self.biop)(self.st[b][f], self.st[b][endpoint])
    }
}

// https://yukicoder.me/problems/no/2171 (3)
// 各 i に対して最終的な A_i の値として考えられるのは or(A_j, ..., A_i) の形の値なので 30 通り程度。それぞれに対してどこを左端とするかをあらかじめ計算しておく。
// 特定のパターンがあり得ることと、A_i の値が or(A_j, ..., A_i) であるような最大の j を L_i としたとき L_i <= L_{i+1} が成立することが同値である。よって DP でできる。
// W = 30 として O(NW^2)-time である。
fn main() {
    input! {
        n: usize,
        a: [u32; n],
    }
    let mut pts = vec![vec![]; n];
    let mut last = vec![];
    let spt = SparseTable::new(&a, |a, b| a | b);
    for i in 0..n {
        last.push(i);
        for val in &mut last {
            // right-shift val as much as possible
            while *val < i && spt.query(*val..i + 1) == spt.query(*val + 1..i + 1) {
                *val += 1;
            }
        }
        last.dedup();
        pts[i] = last.clone();
    }
    const MOD: i64 = 998_244_353;
    let mut dp = vec![1];
    for i in 1..n {
        let mut ep = vec![0; pts[i].len()];
        for j in 0..pts[i - 1].len() {
            for k in 0..pts[i].len() {
                if pts[i - 1][j] <= pts[i][k] {
                    ep[k] += dp[j];
                    if ep[k] >= MOD {
                        ep[k] -= MOD;
                    }
                }
            }
        }
        dp = ep;
    }
    println!("{}", dp.iter().sum::<i64>() % MOD);
}
0