結果
問題 | No.924 紲星 |
ユーザー | akakimidori |
提出日時 | 2019-11-09 02:06:37 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,330 bytes |
コンパイル時間 | 14,529 ms |
コンパイル使用メモリ | 378,648 KB |
実行使用メモリ | 40,452 KB |
最終ジャッジ日時 | 2024-09-15 03:52:38 |
合計ジャッジ時間 | 34,502 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | WA | - |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 2,460 ms
40,432 KB |
testcase_09 | AC | 2,437 ms
40,308 KB |
testcase_10 | AC | 2,452 ms
40,428 KB |
testcase_11 | AC | 2,460 ms
40,452 KB |
testcase_12 | AC | 2,471 ms
40,432 KB |
testcase_13 | AC | 1,023 ms
20,492 KB |
testcase_14 | AC | 940 ms
18,912 KB |
testcase_15 | AC | 882 ms
18,400 KB |
testcase_16 | AC | 1,078 ms
24,664 KB |
testcase_17 | AC | 1,543 ms
28,380 KB |
testcase_18 | RE | - |
ソースコード
//---------- begin SegmentTree Point update Range query ---------- mod segment_tree { pub struct PURQ<T: Clone, F: Fn(T, T) -> T> { n: usize, a: Vec<T>, id: T, op: F, } #[allow(dead_code)] impl<T: Clone, F: Fn(T, T) -> T> PURQ<T, F> { pub fn new(n: usize, id: T, op: F) -> PURQ<T, F> { let mut k = 1; while k < n { k *= 2; } PURQ { n: k, a: vec![id.clone(); 2 * k], id: id, op: op, } } pub fn update(&mut self, x: usize, v: T) { let mut k = self.n + x; let a = &mut self.a; a[k] = v; k >>= 1; while k > 0 { a[k] = (self.op)(a[2 * k].clone(), a[2 * k + 1].clone()); k >>= 1; } } pub fn update_tmp(&mut self, x: usize, v: T) { self.a[x + self.n] = v; } pub fn update_all(&mut self) { for k in (1..(self.n)).rev() { self.a[k] = (self.op)(self.a[2 * k].clone(), self.a[2 * k + 1].clone()); } } pub fn find(&self, mut l: usize, mut r: usize) -> T { let mut p = self.id.clone(); let mut q = self.id.clone(); l += self.n; r += self.n; while l < r { if (l & 1) == 1 { p = (self.op)(p, self.a[l].clone()); l += 1; } if (r & 1) == 1 { r -= 1; q = (self.op)(self.a[r].clone(), q); } l >>= 1; r >>= 1; } (self.op)(p, q) } } } //---------- end SegmentTree Point update Range query ---------- use std::io::Read; use std::io::Write; fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let q: usize = it.next().unwrap().parse().unwrap(); let a: Vec<i64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect(); let ask: Vec<(usize, usize)> = (0..q).map(|_| { let l: usize = it.next().unwrap().parse::<usize>().unwrap() - 1; let r: usize = it.next().unwrap().parse::<usize>().unwrap(); (l, r) }).collect(); let mut b = a.clone(); b.sort(); b.dedup(); let mut left = vec![0; q]; let mut right = vec![b.len(); q]; loop { let mut op = vec![]; let mut update = false; for i in 0..q { if left[i] + 1 < right[i] { let m = (left[i] + right[i]) / 2; op.push((b[m], 1, i)); update = true; } } if !update { break; } for i in 0..n { op.push((a[i], 0, i)); } op.sort(); let mut s = segment_tree::PURQ::new(n, 0, |a, b| a + b); for &(a, op, k) in &op { if op == 0 { s.update(k, 1); } else { let v = s.find(ask[k].0, ask[k].1); if 2 * v < ask[k].1 - ask[k].0 { left[k] = b.binary_search(&a).unwrap(); } else { right[k] = b.binary_search(&a).unwrap(); } } } } let mut sum = a.clone(); sum.push(0); for i in (0..n).rev() { sum[i] += sum[i + 1]; } let mut s = segment_tree::PURQ::new(n, (0i64, 0i64), |a, b| (a.0 + b.0, a.1 + b.1)); let mut op = vec![]; for i in 0..q { op.push((b[right[i]], 1, i)); } for i in 0..n { op.push((a[i], 0, i)); } op.sort(); let mut ans = vec![0; q]; for (v, op, k) in op { if op == 0 { s.update(k, (v, 1)); } else { let (l, r) = ask[k]; let u = s.find(l, r); ans[k] = (sum[l] - sum[r] - u.0 - ((r - l) as i64 - u.1) * v) + (u.1 * v - u.0); } } for v in ans { writeln!(out, "{}", v).ok(); } } fn main() { run(); }