結果
問題 | No.1079 まお |
ユーザー | koba-e964 |
提出日時 | 2021-11-19 01:45:07 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 277 ms / 2,000 ms |
コード長 | 3,864 bytes |
コンパイル時間 | 14,182 ms |
コンパイル使用メモリ | 380,272 KB |
実行使用メモリ | 13,368 KB |
最終ジャッジ日時 | 2024-06-08 16:49:57 |
合計ジャッジ時間 | 20,779 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 5 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 5 ms
5,376 KB |
testcase_10 | AC | 4 ms
5,376 KB |
testcase_11 | AC | 4 ms
5,376 KB |
testcase_12 | AC | 119 ms
7,652 KB |
testcase_13 | AC | 166 ms
11,472 KB |
testcase_14 | AC | 182 ms
11,868 KB |
testcase_15 | AC | 211 ms
12,332 KB |
testcase_16 | AC | 237 ms
12,844 KB |
testcase_17 | AC | 264 ms
12,836 KB |
testcase_18 | AC | 246 ms
12,696 KB |
testcase_19 | AC | 251 ms
12,676 KB |
testcase_20 | AC | 253 ms
12,764 KB |
testcase_21 | AC | 251 ms
12,824 KB |
testcase_22 | AC | 269 ms
13,304 KB |
testcase_23 | AC | 264 ms
13,368 KB |
testcase_24 | AC | 277 ms
13,352 KB |
testcase_25 | AC | 273 ms
13,272 KB |
testcase_26 | AC | 262 ms
13,356 KB |
testcase_27 | AC | 102 ms
6,356 KB |
testcase_28 | AC | 184 ms
9,964 KB |
testcase_29 | AC | 268 ms
13,332 KB |
testcase_30 | AC | 265 ms
13,312 KB |
testcase_31 | AC | 112 ms
6,100 KB |
ソースコード
#[allow(unused_imports)] use std::cmp::*; use std::collections::*; // 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 ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } // #{(i, j) | i in v1, j in v2, min in unique in [i, j]} fn calc(v1: &[usize], v2: &[usize], acc: &[(i64, i32)]) -> i64 { let a = v1.len(); let b = v2.len(); let mut uniq_cnt = vec![0; b + 1]; let mut uniq_sum = vec![0; b + 1]; for i in (0..b).rev() { uniq_cnt[i] = uniq_cnt[i + 1] + if acc[v2[i]].1 == 1 { 1 } else { 0 }; uniq_sum[i] = uniq_sum[i + 1] + if acc[v2[i]].1 == 1 { v2[i] as i64 } else { 0 }; } let mut idx_sum = vec![0; b + 1]; for i in 0..b { idx_sum[i + 1] = idx_sum[i] + v2[i] as i64; } let mut ans = 0; let mut posgt = 0; let mut posge = 0; for i in (0..a).rev() { let (val, uniq) = acc[v1[i]]; while posgt < b && acc[v2[posgt]].0 > val { posgt += 1; } while posge < b && acc[v2[posge]].0 >= val { posge += 1; } if uniq == 1 { ans += idx_sum[posgt] - (v1[i] as i64 - 1) * posgt as i64; } ans += uniq_sum[posge] - uniq_cnt[posge] * (v1[i] as i64 - 1); } ans } fn rec(l: usize, r: usize, a: &[i64], k: i64) -> i64 { if l >= r { return 0; } if l + 1 == r { return if 2 * a[l] == k { 1 } else { 0 }; } let mid = (l + r) / 2; let mut tot = 0; tot += rec(l, mid, a, k); tot += rec(mid, r, a, k); let mut left = HashMap::new(); let mut right = HashMap::new(); let mut acc = vec![(0, 0); r - l]; acc[mid - l - 1] = (a[mid - 1], 1); acc[mid - l] = (a[mid], 1); for i in (l..mid - 1).rev() { if acc[i - l + 1].0 <= a[i] { acc[i - l] = acc[i - l + 1]; if acc[i - l].0 == a[i] { acc[i - l].1 += 1; } } else { acc[i - l] = (a[i], 1); } } for i in mid + 1..r { if acc[i - l - 1].0 <= a[i] { acc[i - l] = acc[i - l - 1]; if acc[i - l].0 == a[i] { acc[i - l].1 += 1; } } else { acc[i - l] = (a[i], 1); } } for i in l..r { if i < mid { left.entry(a[i]).or_insert(vec![]).push(i - l); } else { right.entry(a[i]).or_insert(vec![]).push(i - l); } } for (k1, v1) in left { let k2 = k - k1; if let Some(v2) = right.get(&k2) { tot += calc(&v1, v2, &acc); } } 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() { input! { n: usize, k: i64, a: [i64; n], } println!("{}", rec(0, n, &a, k)); }