結果

問題 No.1558 Derby Live
ユーザー fukafukatanifukafukatani
提出日時 2021-06-25 22:32:41
言語 Rust
(1.77.0 + proconio)
結果
TLE  
実行時間 -
コード長 3,984 bytes
コンパイル時間 13,085 ms
コンパイル使用メモリ 406,076 KB
実行使用メモリ 104,424 KB
最終ジャッジ日時 2024-06-25 08:36:34
合計ジャッジ時間 20,174 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type `mat` should have an upper camel case name
  --> src/main.rs:71:6
   |
71 | type mat = [[i64; 18]; 18];
   |      ^^^ help: convert the identifier to upper camel case: `Mat`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: unused variable: `i`
  --> src/main.rs:22:9
   |
22 |     for i in 0..q {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `n`
  --> src/main.rs:73:14
   |
73 | fn mat_zeros(n: usize, m: usize) -> mat {
   |              ^ help: if this is intentional, prefix it with an underscore: `_n`

warning: unused variable: `m`
  --> src/main.rs:73:24
   |
73 | fn mat_zeros(n: usize, m: usize) -> mat {
   |                        ^ help: if this is intentional, prefix it with an underscore: `_m`

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m, q) = (v[0], v[1], v[2]);
    let mut queries = vec![];
    for i in 0..q {
        queries.push(read_vec::<usize>());
    }
    let mut init = mat_zeros(n, n);
    for i in 0..n {
        init[i][i] = 1;
    }
    let mut seg = SegTree::new(m, init, matmul);

    for ref query in queries {
        match query[0] {
            1 => {
                let d = query[1] - 1;
                let p = query[2..].iter().map(|&x| x - 1).collect::<Vec<_>>();
                let mut mat = mat_zeros(n, n);
                for i in 0..n {
                    mat[i][p[i]] = 1;
                }
                seg.update(d, mat);
            }
            2 => {
                let s = query[1] - 1;
                let m = seg.query(0, s + 1);
                for i in 0..n {
                    for j in 0..n {
                        if m[j][i] == 1 {
                            print!("{} ", j + 1);
                        }
                    }
                }
                println!("");
            }
            _ => {
                let (l, r) = (query[1] - 1, query[2] - 1);
                let m = seg.query(l, r + 1);
                let mut ans = 0;
                for i in 0..n {
                    for j in 0..n {
                        if m[j][i] == 1 {
                            ans += (i as i64 - j as i64).abs();
                        }
                    }
                }
                println!("{}", ans);
            }
        }
    }
}

type mat = [[i64; 18]; 18];

fn mat_zeros(n: usize, m: usize) -> mat {
    [[0i64; 18]; 18]
}

fn matmul(a: &mat, b: &mat) -> mat {
    let mut c = mat_zeros(a.len(), b[0].len());
    for i in 0..a.len() {
        for k in 0..b.len() {
            for j in 0..b[0].len() {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    c
}

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 read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[derive(Clone)]
struct SegTree<T, F>
where
    F: Fn(&T, &T) -> T,
    T: std::clone::Clone,
{
    n: usize,
    dat: Vec<T>,
    init: T,
    functor: F,
}

impl<T, F> SegTree<T, F>
where
    F: Fn(&T, &T) -> T,
    T: std::clone::Clone,
{
    fn new(n: usize, init: T, f: F) -> SegTree<T, F> {
        let mut m = 1;
        // For simplicity, we use 2 ** n sized SegTree.
        while m < n {
            m *= 2;
        }
        SegTree {
            n: m,
            dat: vec![init.clone(); 2 * m - 1],
            init: init.clone(),
            functor: f,
        }
    }

    // dat[k] = a;
    fn update(&mut self, k: usize, a: T) {
        let mut k = k;
        k += self.n - 1;
        self.dat[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.functor)(&self.dat[k * 2 + 1], &self.dat[k * 2 + 2]);
        }
    }

    // [a, b)
    fn query(&self, a: usize, b: usize) -> T {
        self.query_inner(a, b, 0, 0, self.n)
    }

    fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.init.clone();
        }
        if a <= l && r <= b {
            return self.dat[k].clone();
        }

        let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2);
        let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r);
        (self.functor)(&vl, &vr)
    }
}
0