結果

問題 No.1435 Mmm......
ユーザー ziitaziita
提出日時 2021-03-19 22:39:06
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1,383 ms / 2,000 ms
コード長 8,000 bytes
コンパイル時間 14,467 ms
コンパイル使用メモリ 385,396 KB
実行使用メモリ 37,136 KB
最終ジャッジ日時 2024-11-18 23:41:58
合計ジャッジ時間 34,372 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 693 ms
30,596 KB
testcase_07 AC 883 ms
32,360 KB
testcase_08 AC 1,025 ms
33,712 KB
testcase_09 AC 928 ms
34,176 KB
testcase_10 AC 815 ms
31,776 KB
testcase_11 AC 781 ms
31,808 KB
testcase_12 AC 721 ms
31,408 KB
testcase_13 AC 708 ms
30,400 KB
testcase_14 AC 490 ms
18,420 KB
testcase_15 AC 1,055 ms
34,164 KB
testcase_16 AC 863 ms
32,224 KB
testcase_17 AC 567 ms
19,620 KB
testcase_18 AC 1,066 ms
34,320 KB
testcase_19 AC 776 ms
31,568 KB
testcase_20 AC 967 ms
34,468 KB
testcase_21 AC 1,383 ms
35,784 KB
testcase_22 AC 1,337 ms
34,832 KB
testcase_23 AC 1,082 ms
36,748 KB
testcase_24 AC 1,075 ms
36,624 KB
testcase_25 AC 1,084 ms
35,728 KB
testcase_26 AC 1,077 ms
35,784 KB
testcase_27 AC 1,075 ms
37,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254 より

macro_rules! eprint {
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = write!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! eprintln {
    () => { eprintln!(""); };
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = writeln!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! dbg {
    ($v:expr) => {{
        let val = $v;
        eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
        val
    }}
}

macro_rules! mat {
    ($($e:expr),*) => { Vec::from(vec![$($e),*]) };
    ($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
    ($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
    ($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
    ($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
        $a$([$i])*.$f($($t),*)
    };
    ($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
        let t = $e;
        ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
    } };
}

pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
    line
}

macro_rules! read {
    ($($t:tt),*; $n:expr) => {{
        let stdin = ::std::io::stdin();
        let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
            let line = line.unwrap();
            let mut it = line.split_whitespace();
            _read!(it; $($t),*)
        }).collect::<Vec<_>>();
        ret
    }};
    ($($t:tt),*) => {{
        let line = readln();
        let mut it = line.split_whitespace();
        _read!(it; $($t),*)
    }};
}

macro_rules! _read {
    ($it:ident; [char]) => {
        _read!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; [u8]) => {
        Vec::from(_read!($it; String).into_bytes())
    };
    ($it:ident; usize1) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:tt),+) => {
        ($(_read!($it; $t)),*)
    };
}


pub fn main() {
    let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

// const MOD: usize = 998244353;
const MOD: usize = 1_000_000_007;
const INF: i64 = std::i64::MAX/100;
// const INF: usize = std::usize::MAX;
// const INF: i128 = std::i128::MAX/100;

// https://yukicoder.me/submissions/379658 より

pub trait TE {
    type T: Clone;
    type E: Clone;
    fn fold(l:Self::T, r:Self::T) -> Self::T;
    fn eval(p:Self::T, x:Self::E) -> Self::T;
    fn merge(f:Self::E, g:Self::E) -> Self::E;
    fn e() -> Self::T;
    fn id() -> Self::E;
}

pub struct LazySegmentTree<R: TE> {
    size: usize,
    bit: usize,
    a: Vec<(R::T, R::E)>,
}

impl <R: TE> LazySegmentTree<R> {
    pub fn new(n: usize) -> LazySegmentTree<R> {
        let mut bit = 0;
        while (1 << bit) < n {
            bit += 1;
        }
        LazySegmentTree {
            size: 1 << bit,
            bit: bit,
            a: vec![(R::e(), R::id()); 2 << bit],
        }
    }
    pub fn build_by(z: &[R::T]) -> LazySegmentTree<R> {
        let n = z.len();
        let mut bit = 0;
        while (1 << bit) < n {
            bit += 1;
        }
        let mut a = vec![(R::e(), R::id()); 2 << bit];
        for (a, z) in a[(1 << bit)..].iter_mut().zip(z.iter()) {
            a.0 = z.clone();
        }
        for i in (1..(1 << bit)).rev() {
            let l = R::eval(a[2 * i].0.clone(), a[2 * i].1.clone());
            let r = R::eval(a[2 * i + 1].0.clone(), a[2 * i + 1].1.clone());
            a[i].0 = R::fold(l, r);
        }
        LazySegmentTree {
            size: 1 << bit,
            bit : bit,
            a: a,
        }
    }
    fn eval(&self, k: usize) -> R::T {
        R::eval(self.a[k].0.clone(), self.a[k].1.clone())
    }
    fn propagate(&mut self, x: usize) {
        let x = x + self.size;
        for i in (1..(self.bit + 1)).rev() {
            let k = x >> i;
            self.a[2 * k].1 = R::merge(self.a[2 * k].1.clone(), self.a[k].1.clone());
            self.a[2 * k + 1].1 = R::merge(self.a[2 * k + 1].1.clone(), self.a[k].1.clone());
            self.a[k].1 = R::id();
            self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1));
        }
    }
    fn save(&mut self, x: usize) {
        let x = x + self.size;
        for i in 1..(self.bit + 1) {
            let k = x >> i;
            self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1));
        }
    }
    pub fn update(&mut self, l: usize, r: usize, op: R::E) {
        self.propagate(l);
        self.propagate(r - 1);
        let mut x = l + self.size;
        let mut y = r + self.size;
        while x < y {
            if x & 1 == 1 {
                self.a[x].1 = R::merge(self.a[x].1.clone(), op.clone());
                x += 1;
            }
            if y & 1 == 1 {
                y -= 1;
                self.a[y].1 = R::merge(self.a[y].1.clone(), op.clone());
            }
            x >>= 1;
            y >>= 1;
        }
        self.save(l);
        self.save(r - 1);
    }
    pub fn find(&mut self, l: usize, r: usize) -> R::T {
        self.propagate(l);
        self.propagate(r - 1);
        let mut x = l + self.size;
        let mut y = r + self.size;
        let mut p = R::e();
        let mut q = R::e();
        while x < y {
            if x & 1 == 1 {
                p = R::fold(p, self.eval(x));
                x += 1;
            }
            if y & 1 == 1 {
                y -= 1;
                q = R::fold(self.eval(y), q);
            }
            x >>= 1;
            y >>= 1;
        }
        R::fold(p, q)
    }
}


struct R;
impl TE for R {
    type T = (i64,i64);
    type E = i64;
    fn fold(l: Self::T, r: Self::T) -> Self::T {
        (min(l.0,r.0), min(max(l.0,r.0),min(l.1,r.1)))
    }
    fn eval(p: Self::T, x: Self::E) -> Self::T {
        p
    }
    fn merge(f: Self::E, g: Self::E) -> Self::E {
        f
    }
    fn e() -> Self::T {
        (INF,INF)
    }
    fn id() -> Self::E {
        INF
    }
}

struct R2;
impl TE for R2 {
    type T = (i64,i64);
    type E = i64;
    fn fold(l: Self::T, r: Self::T) -> Self::T {
        (max(l.0,r.0),0)
    }
    fn eval(p: Self::T, x: Self::E) -> Self::T {
        p
    }
    fn merge(f: Self::E, g: Self::E) -> Self::E {
        f
    }
    fn e() -> Self::T {
        (0,0)
    }
    fn id() -> Self::E {
        0
    }
}

fn solve() {
    let n = read!(usize);
    let a = read!([i64]);
    let mut b = vec![];
    let mut c = vec![];
    for i in 0..n {
        b.push((a[i],INF));
        c.push((a[i],0));
    }
    let mut seg = LazySegmentTree::<R>::build_by(&b);
    let mut seg2 = LazySegmentTree::<R2>::build_by(&c);
    let mut ans = 0;
    for i in 0..n-1 {
        let mut l = i;
        let mut r = n;
        while r-l>1 {
            let mid = (l+r)/2;
            let (x,y) = seg.find(i,mid+1);
            let (z,_) = seg2.find(i,mid+1);
            if z>x+y {
                r = mid;
            }
            else {
                l = mid;
            }
        }
        // println!("{} {}",i,l);
        ans += (l-i) as i64;
    }
    println!("{}",ans);
}

fn run() {
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    solve();
}
0