結果

問題 No.1234 典型RMQ
ユーザー fukafukatanifukafukatani
提出日時 2020-09-18 22:10:17
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 3,128 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 148,844 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-27 06:36:32
合計ジャッジ時間 3,724 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:33:9
   |
33 |     for i in 0..q {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

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 n = read::<usize>();
    let mut a = read_vec::<i64>();

    let num_buckets = 1000;
    let bucket_width = 3;
    let mut bucket_added = vec![0; num_buckets];
    let mut bucket_min = vec![std::i64::MAX; num_buckets];

    for i in 0..n {
        bucket_min[i / bucket_width] = min(a[i], bucket_min[i / bucket_width]);
    }

    let q = read::<usize>();
    let mut queries = vec![];
    for i in 0..q {
        let v = read_vec::<i64>();
        let (k, l, r, c) = (v[0], v[1] as usize - 1, v[2] as usize - 1, v[3]);
        queries.push((k, l, r, c));
    }

    for (k, l, r, c) in queries {
        if k == 1 {
            let mut tl = 0;
            let mut tr = bucket_width - 1;
            for bi in 0..num_buckets {
                if r < tl || l > tr {
                    // Nothing
                } else if l <= tl && r >= tr {
                    bucket_added[bi] += c;
                    bucket_min[bi] += c;
                } else {
                    for i in 0..bucket_width {
                        if i + tl >= l && i + tl <= r {
                            a[i + tl] += c;
                        }
                    }
                    bucket_min[bi] = std::i64::MAX;
                    for i in 0..bucket_width {
                        if i + tl >= n {
                            break;
                        }
                        bucket_min[bi] = min(a[i + tl] + bucket_added[bi], bucket_min[bi]);
                    }
                }
                tl += bucket_width;
                tr += bucket_width;
            }
        } else {
            let mut tl = 0;
            let mut tr = bucket_width - 1;
            let mut ans = std::i64::MAX;
            for bi in 0..num_buckets {
                if r < tl || l > tr {
                    // Nothing
                } else if l <= tl && r >= tr {
                    ans = min(ans, bucket_min[bi]);
                } else {
                    for i in 0..bucket_width {
                        if i + tl >= l && i + tl <= r {
                            if i + tl >= n {
                                break;
                            }

                            ans = min(ans, a[i + tl] + bucket_added[bi]);
                        }
                    }
                }
                tl += bucket_width;
                tr += bucket_width;
            }
            println!("{}", ans);
        }
    }
}

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()
}
0