結果

問題 No.924 紲星
ユーザー koba-e964koba-e964
提出日時 2021-11-22 20:10:38
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,239 ms / 4,000 ms
コード長 4,933 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 169,836 KB
実行使用メモリ 20,336 KB
最終ジャッジ日時 2023-09-06 14:48:10
合計ジャッジ時間 14,365 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1,239 ms
18,820 KB
testcase_09 AC 1,225 ms
18,884 KB
testcase_10 AC 1,228 ms
20,336 KB
testcase_11 AC 1,239 ms
18,884 KB
testcase_12 AC 1,239 ms
20,052 KB
testcase_13 AC 418 ms
9,624 KB
testcase_14 AC 383 ms
9,352 KB
testcase_15 AC 373 ms
8,928 KB
testcase_16 AC 562 ms
9,944 KB
testcase_17 AC 627 ms
13,136 KB
testcase_18 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

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

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

struct SqrtDecomp {
    bl: usize,
    a: Vec<i64>,
    b: Vec<i64>,
}

impl SqrtDecomp {
    fn new(n: usize) -> Self {
        let mut m = 1;
        while m * m < n {
            m += 1;
        }
        SqrtDecomp {
            bl: m,
            a: vec![0; n],
            b: vec![0; (n + m - 1) / m],
        }
    }
    fn add(&mut self, idx: usize, x: i64) {
        self.a[idx] += x;
        self.b[idx / self.bl] += x;
    }
    fn query(&self, l: usize, r: usize) -> i64 {
        let bl = self.bl;
        if r - l <= bl {
            let mut tot = 0;
            for i in l..r {
                tot += self.a[i];
            }
            return tot;
        }
        let lb = (l + bl - 1) / bl;
        let rb = r / bl;
        let mut tot = 0;
        for i in l..lb * bl {
            tot += self.a[i];
        }
        for i in lb..rb {
            tot += self.b[i];
        }
        for i in rb * bl..r {
            tot += self.a[i];
        }
        tot
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, q: usize,
        a: [i64; n],
        lr: [(usize1, usize); q],
    }
    let mut coo = a.clone();
    coo.sort(); coo.dedup();
    let m = coo.len();
    let a: Vec<usize> = a.iter().map(|a| coo.binary_search(a).unwrap()).collect();
    let mut lri = vec![];
    for i in 0..q {
        let (l, r) = lr[i];
        lri.push((l, r, i));
    }
    lri.sort_unstable_by_key(|&(l, r, _)| {
        let b = l / 450;
        (b, if b % 2 == 0 { r } else { n - r })
    });
    let mut x = 0;
    let mut y = 0;
    let mut sum = SqrtDecomp::new(m);
    let mut cnt = SqrtDecomp::new(m);
    let mut ans = vec![0; q];
    for (l, r, idx) in lri {
        while y < r {
            cnt.add(a[y], 1);
            sum.add(a[y], coo[a[y]]);
            y += 1;
        }
        while x > l {
            x -= 1;
            cnt.add(a[x], 1);
            sum.add(a[x], coo[a[x]]);
        }
        while y > r {
            y -= 1;
            cnt.add(a[y], -1);
            sum.add(a[y], -coo[a[y]]);
        }
        while x < l {
            cnt.add(a[x], -1);
            sum.add(a[x], -coo[a[x]]);
            x += 1;
        }
        let targ = (r - l) as i64 / 2;
        let mut now = 0;
        let mut pos = 0;
        while now <= targ {
            if now + cnt.b[pos] <= targ {
                now += cnt.b[pos];
                pos += 1;
            } else {
                break;
            }
        }
        pos *= cnt.bl;
        while now <= targ {
            if now + cnt.a[pos] <= targ {
                now += cnt.a[pos];
                pos += 1;
            } else {
                break;
            }
        }
        let val = coo[pos];
        ans[idx] = sum.query(pos, m) - sum.query(0, pos) + val * (2 * cnt.query(0, pos) - (r - l) as i64);
    }
    for a in ans {
        puts!("{}\n", a);
    }
}
0