結果
問題 | No.924 紲星 |
ユーザー | akakimidori |
提出日時 | 2020-02-08 02:56:14 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 361 ms / 4,000 ms |
コード長 | 4,606 bytes |
コンパイル時間 | 14,247 ms |
コンパイル使用メモリ | 395,204 KB |
実行使用メモリ | 40,412 KB |
最終ジャッジ日時 | 2024-09-25 08:35:19 |
合計ジャッジ時間 | 18,870 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,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 356 ms
35,948 KB |
testcase_09 | AC | 352 ms
35,252 KB |
testcase_10 | AC | 351 ms
34,996 KB |
testcase_11 | AC | 361 ms
40,412 KB |
testcase_12 | AC | 356 ms
35,172 KB |
testcase_13 | AC | 160 ms
20,200 KB |
testcase_14 | AC | 142 ms
21,868 KB |
testcase_15 | AC | 139 ms
19,984 KB |
testcase_16 | AC | 177 ms
18,248 KB |
testcase_17 | AC | 218 ms
28,800 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; 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 { for ask in ask { assert!(ask.2 == 0); res.push((b[0].1, 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; } for i in (0..m).rev() { cnt[i] += cnt[i + 1]; sum[i] += sum[i + 1]; } 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)); } } for i in 0..m { cnt[i] -= cnt[i + 1]; } for r in r.iter() { cnt[r.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 + 1]; 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; } let mut id_l = vec![0; m]; let mut id_r = vec![0; m]; for i in 1..m { id_l[i] = id_l[i - 1]; if cnt_left[i] > 0 { id_l[i] += 1; } assert!(cnt_left[i] <= cnt[i]); id_r[i] = id_r[i - 1]; if cnt[i] > cnt_left[i] { id_r[i] += 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(); }