結果

問題 No.1496 不思議な数え上げ
ユーザー akakimidoriakakimidori
提出日時 2021-04-30 22:20:03
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 4,184 bytes
コンパイル時間 5,249 ms
コンパイル使用メモリ 162,224 KB
実行使用メモリ 8,768 KB
最終ジャッジ日時 2023-09-26 06:25:02
合計ジャッジ時間 8,007 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,768 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 TLE -
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 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

fn run() {
    input! {
        n: usize,
        p: [usize; n],
        a: [usize; n],
    }
    type BIT = std::collections::HashMap<usize, i64>;
    let max = *a.iter().max().unwrap();
    let add = |bit: &mut BIT, mut x: usize, v: i64| {
        while x <= max {
            *bit.entry(x).or_insert(0) += v;
            x += x & (!x + 1);
        }
    };
    let sum = |bit: &BIT, mut x: usize| -> i64 {
        let mut res = 0;
        while x > 0 {
            res += bit.get(&x).map_or(0, |p| *p);
            x -= x & (!x + 1);
        }
        res
    };
    let mut a = a;
    a.insert(0, 0);
    let mut p = p;
    let mut ans = vec![0; n + 1];
    let mut dfs = vec![(0, n)];
    while let Some((l, r)) = dfs.pop() {
        if l + 1 == r {
            let p = p[l];
            if p <= a[p] {
                ans[p] += 1;
            }
            continue;
        }
        let m = (l + r) / 2;
        dfs.push((l, m));
        dfs.push((m, r));
        p[l..m].reverse();
        let (l, r) = (&p[l..m], &p[m..r]);
        let mut left = vec![];
        let mut l_bit = std::collections::HashMap::new();
        let mut pre = (0, n);
        for p in l.iter() {
            pre.0 += *p;
            pre.1.chmin(*p);
            left.push(pre);
            add(&mut l_bit, pre.0, 1);
        }
        let mut right = vec![];
        let mut r_bit = std::collections::HashMap::new();
        let mut pre = (0, n);
        for p in r.iter() {
            pre.0 += *p;
            pre.1.chmin(*p);
            right.push(pre);
            add(&mut r_bit, pre.0, 1);
        }
        while left.len() > 0 && right.len() > 0 {
            let (x, y) = *left.last().unwrap();
            let (z, w) = *right.last().unwrap();
            if y <= w {
                left.pop();
                if x < a[y] {
                    ans[y] += sum(&r_bit, a[y] - x);
                }
                add(&mut l_bit, x, -1);
            } else {
                right.pop();
                let (x, y) = (z, w);
                if x < a[y] {
                    ans[y] += sum(&l_bit, a[y] - x);
                }
                add(&mut r_bit, x, -1);
            }
        }
    }
    for a in ans[1..].iter() {
        println!("{}", *a);
    }
}

fn main() {
    run();
}
0