結果

問題 No.1496 不思議な数え上げ
ユーザー akakimidoriakakimidori
提出日時 2021-04-30 22:39:56
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 86 ms / 3,000 ms
コード長 6,108 bytes
コンパイル時間 12,399 ms
コンパイル使用メモリ 379,484 KB
実行使用メモリ 23,040 KB
最終ジャッジ日時 2024-07-19 02:21:17
合計ジャッジ時間 18,655 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 72 ms
19,840 KB
testcase_04 AC 78 ms
19,840 KB
testcase_05 AC 73 ms
19,840 KB
testcase_06 AC 78 ms
19,840 KB
testcase_07 AC 78 ms
19,840 KB
testcase_08 AC 84 ms
19,712 KB
testcase_09 AC 85 ms
23,040 KB
testcase_10 AC 86 ms
23,040 KB
testcase_11 AC 83 ms
23,040 KB
testcase_12 AC 71 ms
19,968 KB
testcase_13 AC 76 ms
19,840 KB
testcase_14 AC 75 ms
19,840 KB
testcase_15 AC 80 ms
20,864 KB
testcase_16 AC 79 ms
21,120 KB
testcase_17 AC 75 ms
19,968 KB
testcase_18 AC 75 ms
19,968 KB
testcase_19 AC 78 ms
19,968 KB
testcase_20 AC 75 ms
19,840 KB
testcase_21 AC 72 ms
19,840 KB
testcase_22 AC 73 ms
19,968 KB
testcase_23 AC 75 ms
19,840 KB
testcase_24 AC 81 ms
19,968 KB
testcase_25 AC 75 ms
19,968 KB
testcase_26 AC 71 ms
19,968 KB
testcase_27 AC 80 ms
19,328 KB
testcase_28 AC 80 ms
19,328 KB
testcase_29 AC 79 ms
19,328 KB
testcase_30 AC 82 ms
19,328 KB
testcase_31 AC 77 ms
19,328 KB
testcase_32 AC 69 ms
18,560 KB
testcase_33 AC 35 ms
10,752 KB
testcase_34 AC 34 ms
10,368 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 69 ms
18,816 KB
testcase_37 AC 37 ms
11,008 KB
testcase_38 AC 36 ms
10,496 KB
testcase_39 AC 51 ms
16,512 KB
testcase_40 AC 29 ms
9,728 KB
testcase_41 AC 55 ms
16,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin SegmentTree Point update Range query ----------
mod segment_tree {
    pub struct PURQ<T, F> {
        size: usize,
        data: Vec<T>,
        e: T,
        op: F,
    }
    #[allow(dead_code)]
    impl<T, F> PURQ<T, F>
    where
        T: Clone,
        F: Fn(&T, &T) -> T,
    {
        pub fn new(size: usize, e: T, op: F) -> PURQ<T, F> {
            let size = size.next_power_of_two();
            PURQ {
                size,
                data: vec![e.clone(); 2 * size],
                e: e,
                op: op,
            }
        }
        pub fn update(&mut self, x: usize, v: T) {
            assert!(x < self.size);
            let mut x = x + self.size;
            let data = &mut self.data;
            data[x] = v;
            x >>= 1;
            while x > 0 {
                data[x] = (self.op)(&data[2 * x], &data[2 * x + 1]);
                x >>= 1;
            }
        }
        pub fn update_tmp(&mut self, x: usize, v: T) {
            assert!(x < self.size);
            self.data[x + self.size] = v;
        }
        pub fn update_all(&mut self) {
            let data = &mut self.data;
            for k in (1..self.size).rev() {
                data[k] = (self.op)(&data[2 * k], &data[2 * k + 1]);
            }
        }
        pub fn find(&self, l: usize, r: usize) -> T {
            assert!(l <= r && r <= self.size);
            if l == r {
                return self.e.clone();
            }
            let mut p = self.e.clone();
            let mut q = self.e.clone();
            let mut l = l + self.size;
            let mut r = r + self.size;
            let data = &self.data;
            while l < r {
                if l & 1 == 1 {
                    p = (self.op)(&p, &data[l]);
                    l += 1;
                }
                if r & 1 == 1 {
                    r -= 1;
                    q = (self.op)(&data[r], &q);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.op)(&p, &q)
        }
    }
}
// ---------- end SegmentTree Point update Range query ----------
// ---------- 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],
    }
    let mut a = a;
    a.insert(0, 0);
    let mut s = p.clone();
    s.push(0);
    for i in (0..n).rev() {
        s[i] += s[i + 1];
    }
    let mut seg = segment_tree::PURQ::new(n, (n + 1, n), |a, b| std::cmp::min(*a, *b));
    for (i, p) in p.iter().enumerate() {
        seg.update_tmp(i, (*p, i));
    }
    seg.update_all();
    let mut dfs = vec![(0, n)];
    let mut ans = vec![0; n + 1];
    while let Some((l, r)) = dfs.pop() {
        if l >= r {
            continue;
        }
        let (val, x) = seg.find(l, r);
        dfs.push((l, x));
        dfs.push((x + 1, r));
        let left = x - l;
        let right = r - x - 1;
        if left <= right {
            for l in (l..=x).rev() {
                let mut ok = x + 1;
                let mut ng = r + 1;
                while ng - ok > 1 {
                    let mid = (ok + ng) / 2;
                    if s[l] - s[mid] > a[val] {
                        ng = mid;
                    } else {
                        ok = mid;
                    }
                }
                if s[l] - s[ok] <= a[val] {
                    ans[val] += ok - x;
                }
            }
        } else {
            for r in (x + 1)..=r {
                let mut ok = x;
                let mut ng = l;
                while ok - ng > 1 {
                    let mid = (ok + ng) / 2;
                    if s[mid] - s[r] > a[val] {
                        ng = mid;
                    } else {
                        ok = mid;
                    }
                }
                if ng == l && s[l] - s[r] <= a[val] {
                    ok = l;
                }
                if s[ok] - s[r] <= a[val] {
                    ans[val] += x - ok + 1;
                }
            }
        }
    }
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for a in ans[1..].iter() {
        writeln!(out, "{}", *a).ok();
    }
}

fn main() {
    run();
}
0