結果
問題 | No.924 紲星 |
ユーザー | akakimidori |
提出日時 | 2020-04-07 23:13:45 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 187 ms / 4,000 ms |
コード長 | 4,260 bytes |
コンパイル時間 | 22,387 ms |
コンパイル使用メモリ | 382,996 KB |
実行使用メモリ | 34,784 KB |
最終ジャッジ日時 | 2024-07-08 08:22:23 |
合計ジャッジ時間 | 26,052 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 180 ms
34,776 KB |
testcase_09 | AC | 180 ms
34,640 KB |
testcase_10 | AC | 178 ms
34,780 KB |
testcase_11 | AC | 187 ms
34,500 KB |
testcase_12 | AC | 178 ms
34,784 KB |
testcase_13 | AC | 76 ms
17,540 KB |
testcase_14 | AC | 70 ms
16,688 KB |
testcase_15 | AC | 67 ms
16,024 KB |
testcase_16 | AC | 103 ms
19,796 KB |
testcase_17 | AC | 105 ms
23,676 KB |
testcase_18 | AC | 1 ms
5,376 KB |
ソースコード
//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; fn run() { input! { n: usize, q: usize, a: [i64; n], ask: [(usize1, usize); q], } let mut b: Vec<_> = a.iter().cloned().enumerate().map(|p| (p.1, p.0)).collect(); b.sort(); let mut query = Vec::with_capacity(q); for (i, &(l, r)) in ask.iter().enumerate() { query.push((l, r, (r - l) / 2, 0, i)); } let mut ans = vec![(0, 0); q]; let mut dfs = vec![(query.as_mut_slice(), b.as_mut_slice())]; let mut cnt_sum = Vec::with_capacity(n + 1); let mut elem_id = Vec::with_capacity(n + 1); while let Some((query, val)) = dfs.pop() { if val.len() == 1 { assert!(val[0].1 == 0); let mid = val[0].0; for &(l, r, k, a, x) in query.iter() { assert!(l == 0 && r == 1 && k == 0); ans[x] = (a, mid); } continue; } let len = val.len(); cnt_sum.clear(); cnt_sum.resize(len + 1, (0, 0)); let (front, back) = val.split_at_mut(len / 2); for &(x, k) in front.iter() { let p = &mut cnt_sum[k]; p.0 += 1; p.1 += x; } let mut prev = (0, 0); for p in cnt_sum.iter_mut().rev().skip(1) { p.0 += prev.0; p.1 += prev.1; prev = *p; } let mut f = 0; let mut b = query.len(); while f < b { let (l, r, k, a, x) = query[f]; let c = cnt_sum[l].0 - cnt_sum[r].0; let s = cnt_sum[l].1 - cnt_sum[r].1; if k >= c { let k = k - c; let a = a + s; query[f] = (l, r, k, a, x); b -= 1; query.swap(f, b); } else { f += 1; } } let (left, right) = query.split_at_mut(f); let compress = vec![(left, front), (right, back)]; for (query, val) in compress.into_iter() { elem_id.clear(); elem_id.resize(len + 1, (false, 0)); for v in val.iter() { elem_id[v.1].0 = true; } let mut pos = 0; for p in elem_id.iter_mut() { p.1 = pos; if p.0 { pos += 1; } } for q in query.iter_mut() { q.0 = elem_id[q.0].1; q.1 = elem_id[q.1].1; } for v in val.iter_mut() { v.1 = elem_id[v.1].1; } dfs.push((query, val)); } } let mut sum = a.clone(); sum.push(0); for i in (0..n).rev() { sum[i] += sum[i + 1]; } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for (i, (l, r)) in ask.into_iter().enumerate() { let ans = sum[l] - sum[r] - 2 * ans[i].0 - (r - l) as i64 % 2 * ans[i].1; writeln!(out, "{}", ans).ok(); } } fn main() { run(); }