結果

問題 No.924 紲星
ユーザー akakimidoriakakimidori
提出日時 2020-02-08 03:08:02
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 329 ms / 4,000 ms
コード長 4,810 bytes
コンパイル時間 18,027 ms
コンパイル使用メモリ 398,664 KB
実行使用メモリ 40,540 KB
最終ジャッジ日時 2024-09-25 08:36:23
合計ジャッジ時間 21,897 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 319 ms
36,492 KB
testcase_09 AC 315 ms
35,768 KB
testcase_10 AC 320 ms
36,784 KB
testcase_11 AC 326 ms
40,540 KB
testcase_12 AC 329 ms
35,312 KB
testcase_13 AC 148 ms
22,188 KB
testcase_14 AC 135 ms
23,420 KB
testcase_15 AC 128 ms
20,724 KB
testcase_16 AC 158 ms
18,252 KB
testcase_17 AC 201 ms
29,620 KB
testcase_18 AC 0 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//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, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

use std::io::Write;
use std::cmp::max;

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    input! {
        n: usize,
        q: usize,
        a: [i64; n],
        ask: [(usize1, usize); q],
    }
    let mut b: Vec<(usize, i64)> = a.iter().cloned().enumerate().collect();
    b.sort_by_key(|b| b.1);
    //L, R, cnt, sum, id
    let query: Vec<_> = ask.iter().cloned().enumerate().map(|(k, p)| {
        (p.0, p.1, (p.1 - p.0) / 2, 0, k)
    }).collect();
    let mut res = vec![];
    let mut stack = vec![(b.as_mut_slice(), query)];
    while let Some((b, ask)) = stack.pop() {
        if b.len() == 1 {
            let mid = b[0].1;
            for ask in ask {
                res.push((mid, ask.3, ask.4));
            }
            continue;
        }
        let mut m = 0;
        for b in b.iter() {
            m = max(m, b.0 + 1);
        }
        for p in ask.iter() {
            m = max(m, p.1 + 1);
        }
        let mid = b.len() / 2;
        let (l, r) = b.split_at_mut(mid);
        let mut cnt = vec![0; m + 1];
        let mut sum = vec![0; m + 1];
        for l in l.iter() {
            cnt[l.0] += 1;
            sum[l.0] += l.1;
        }
        let mut prev = (0, 0);
        for (c, s) in cnt.iter_mut().zip(sum.iter_mut()).take(m).rev() {
            *c += prev.0;
            *s += prev.1;
            prev = (*c, *s);
        }
        let mut left = vec![];
        let mut right = vec![];
        for (l, r, c, s, k) in ask.into_iter() {
            if c < cnt[l] - cnt[r] {
                left.push((l, r, c, s, k));
            } else {
                right.push((l, r, c - (cnt[l] - cnt[r]), s + sum[l] - sum[r], k));
            }
        }
        cnt.clear();
        cnt.resize(m, 0);
        for p in l.iter().chain(r.iter()) {
            cnt[p.0] += 1;
        }
        for p in left.iter().chain(right.iter()) {
            cnt[p.0] += 1;
            cnt[p.1] += 1;
        }
        let mut cnt_left = vec![0; m];
        for l in l.iter() {
            cnt_left[l.0] += 1;
        }
        for p in left.iter() {
            cnt_left[p.0] += 1;
            cnt_left[p.1] += 1;
        }
        for (c, cl) in cnt.iter_mut().zip(cnt_left.iter()) {
            *c -= *cl;
        }
        let mut id_l = vec![0; m];
        let mut p = 0;
        for (id, c) in id_l.iter_mut().zip(cnt_left.iter()) {
            *id = p;
            if *c > 0 {
                p += 1;
            }
        }
        let mut id_r = vec![0; m];
        let mut p = 0;
        for (id, c) in id_r.iter_mut().zip(cnt.iter()) {
            *id = p;
            if *c > 0 {
                p += 1;
            }
        }
        for l in l.iter_mut() {
            l.0 = id_l[l.0];
        }
        for r in r.iter_mut() {
            r.0 = id_r[r.0];
        }
        for left in left.iter_mut() {
            left.0 = id_l[left.0];
            left.1 = id_l[left.1];
        }
        for right in right.iter_mut() {
            right.0 = id_r[right.0];
            right.1 = id_r[right.1];
        }
        stack.push((l, left));
        stack.push((r, right));
    }
    let mut sum = a.clone();
    sum.push(0);
    for i in (0..n).rev() {
        sum[i] += sum[i + 1];
    }
    res.sort_by_key(|p| p.2);
    for (i, (mid, s, _)) in res.into_iter().enumerate() {
        let (l, r) = ask[i];
        let c = (r - l) / 2;
        let ans = sum[l] - sum[r] - s - (r - l - c) as i64 * mid + mid * c as i64 - s;
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}
0