結果
問題 | No.924 紲星 |
ユーザー | koba-e964 |
提出日時 | 2021-11-22 20:10:38 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1,498 ms / 4,000 ms |
コード長 | 4,933 bytes |
コンパイル時間 | 15,529 ms |
コンパイル使用メモリ | 404,068 KB |
実行使用メモリ | 20,524 KB |
最終ジャッジ日時 | 2024-06-24 09:12:48 |
合計ジャッジ時間 | 30,414 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,812 KB |
testcase_03 | AC | 3 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 3 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 1,498 ms
19,848 KB |
testcase_09 | AC | 1,382 ms
19,852 KB |
testcase_10 | AC | 1,364 ms
19,976 KB |
testcase_11 | AC | 1,341 ms
20,524 KB |
testcase_12 | AC | 1,377 ms
19,848 KB |
testcase_13 | AC | 455 ms
10,588 KB |
testcase_14 | AC | 422 ms
10,168 KB |
testcase_15 | AC | 399 ms
9,728 KB |
testcase_16 | AC | 600 ms
10,916 KB |
testcase_17 | AC | 670 ms
14,712 KB |
testcase_18 | AC | 1 ms
6,944 KB |
ソースコード
#[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); } }