結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー | hatoo |
提出日時 | 2018-06-08 23:49:23 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 9,904 bytes |
コンパイル時間 | 13,163 ms |
コンパイル使用メモリ | 398,112 KB |
実行使用メモリ | 29,780 KB |
最終ジャッジ日時 | 2024-06-30 11:16:59 |
合計ジャッジ時間 | 21,612 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 1 ms
6,940 KB |
ソースコード
/** * _ _ __ _ _ _ _ _ _ _ * | | | | / / | | (_) | (_) | | (_) | | * | |__ __ _| |_ ___ ___ / /__ ___ _ __ ___ _ __ ___| |_ _| |_ ___ _____ ______ _ __ _ _ ___| |_ ______ ___ _ __ _ _ __ _ __ ___| |_ ___ * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __| * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) | __/ |_| | |_| |\ V / __/ | | | |_| \__ \ |_ \__ \ | | | | |_) | |_) | __/ |_\__ \ * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___| |_| \__,_|___/\__| |___/_| |_|_| .__/| .__/ \___|\__|___/ * | | | | | | * |_| |_| |_| * * https://github.com/hatoo/competitive-rust-snippets */ #[allow(unused_imports)] use std::cmp::{max, min, Ordering}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::io::{stdin, stdout, BufWriter, Write}; #[allow(unused_imports)] use std::iter::FromIterator; mod util { use std::fmt::Debug; use std::io::{stdin, stdout, BufWriter, StdoutLock}; use std::str::FromStr; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn chars() -> Vec<char> { line().chars().collect() } #[allow(dead_code)] pub fn gets<T: FromStr>() -> Vec<T> where <T as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) { let out = stdout(); let writer = BufWriter::new(out.lock()); f(writer) } } #[allow(unused_macros)] macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; } #[allow(unused_macros)] macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } } const BIG_STACK_SIZE: bool = true; #[allow(dead_code)] fn main() { use std::thread; if BIG_STACK_SIZE { thread::Builder::new() .stack_size(32 * 1024 * 1024) .name("solve".into()) .spawn(solve) .unwrap() .join() .unwrap(); } else { solve(); } } #[allow(dead_code)] /// Lazy Segment Tree pub struct SEG<T: SEGimpl> { n: usize, buf: Vec<T::Elem>, zero: T::Elem, phantom: std::marker::PhantomData<T>, } impl<T: SEGimpl> SEG<T> { #[allow(dead_code)] pub fn new(n: usize, zero: T::Elem) -> SEG<T> { let n = (1..).map(|i| 1 << i).find(|&x| x >= n).unwrap(); SEG { n: n, buf: vec![zero.clone(); 2 * n], zero: zero, phantom: std::marker::PhantomData, } } #[allow(dead_code)] fn eval(&mut self, k: usize, l: usize, r: usize) { if r - l > 1 { let (l, r) = self.buf.split_at_mut(2 * k + 1); let (c1, c2) = r.split_at_mut(1); T::eval(&mut l[k], Some((&mut c1[0], &mut c2[0]))); } else { T::eval(&mut self.buf[k], None); } } #[allow(dead_code)] pub fn update(&mut self, i: usize, x: T::Elem) { let mut k = i + self.n - 1; self.buf[k] = x; self.eval(k, i, i + 1); while k > 0 { k = (k - 1) / 2; let (l, r) = self.buf.split_at_mut(2 * k + 1); let (c1, c2) = r.split_at_mut(1); T::reduce(&mut l[k], &c1[0], &c2[0]); } } #[allow(dead_code)] pub fn get(&mut self, i: usize) -> Option<T::R> { self.query(i, i + 1) } #[allow(dead_code)] fn r(&mut self, x: &T::A, a: usize, b: usize, k: usize, l: usize, r: usize) { self.eval(k, l, r); if r <= a || b <= l { return; } if a <= l && r <= b { T::range(x, &mut self.buf[k], l, r); self.eval(k, l, r); return; } self.r(x, a, b, 2 * k + 1, l, (l + r) / 2); self.r(x, a, b, 2 * k + 2, (l + r) / 2, r); let (l, r) = self.buf.split_at_mut(2 * k + 1); let (c1, c2) = r.split_at_mut(1); T::reduce(&mut l[k], &c1[0], &c2[0]); } #[allow(dead_code)] pub fn range_add(&mut self, x: &T::A, a: usize, b: usize) { let n = self.n; self.r(x, a, b, 0, 0, n); } #[allow(dead_code)] pub fn add(&mut self, x: &T::A, i: usize) { self.range_add(x, i, i + 1); } #[allow(dead_code)] fn q(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> Option<T::Elem> { self.eval(k, l, r); if r <= a || b <= l { return None; } if a <= l && r <= b { Some(self.buf[k].clone()) } else { let vl = self.q(a, b, k * 2 + 1, l, (l + r) / 2); let vr = self.q(a, b, k * 2 + 2, (l + r) / 2, r); match (vl, vr) { (Some(l), Some(r)) => { let mut res = self.zero.clone(); T::reduce(&mut res, &l, &r); Some(res) } (Some(l), None) => Some(l), (None, Some(r)) => Some(r), _ => None, } } } #[allow(dead_code)] pub fn query(&mut self, a: usize, b: usize) -> Option<T::R> { let n = self.n; self.q(a, b, 0, 0, n).map(T::to_result) } } pub trait SEGimpl { type Elem: Clone; type A; type R; fn eval(parent: &mut Self::Elem, children: Option<(&mut Self::Elem, &mut Self::Elem)>); fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize); fn reduce(parent: &mut Self::Elem, c1: &Self::Elem, c2: &Self::Elem); fn to_result(elem: Self::Elem) -> Self::R; } #[allow(dead_code)] struct RangeAddSum( ); impl SEGimpl for RangeAddSum { type Elem = (i64, i64); type A = i64; type R = i64; fn eval(parent: &mut Self::Elem, children: Option<(&mut Self::Elem, &mut Self::Elem)>) { let x = parent.1; parent.0 += x; parent.1 = 0; if let Some((c1, c2)) = children { c1.1 += x / 2; c2.1 += x / 2; } } fn range(x: &Self::A, elem: &mut Self::Elem, l: usize, r: usize) { elem.1 += (r - l) as i64 * x; } fn reduce(parent: &mut Self::Elem, c1: &Self::Elem, c2: &Self::Elem) { parent.0 = c1.0 + c2.0; } fn to_result(elem: Self::Elem) -> Self::R { elem.0 } } #[allow(dead_code)] /// Binary Indexed Tree of usize pub struct BIT { buf: Vec<usize>, } #[allow(dead_code)] impl BIT { pub fn new(n: usize) -> BIT { BIT { buf: vec![0; n + 1], } } pub fn sum(&self, i: usize) -> usize { let mut i = i; let mut s = 0; while i > 0 { s += self.buf[i]; i &= i - 1; } s } pub fn add(&mut self, i: usize, x: usize) { let mut i = i as i64; while i < self.buf.len() as i64 { self.buf[i as usize] += x; i += i & -i; } } } fn solve() { let n = get!(usize); let xs = get!(u64; n); let mut cc = xs.clone(); cc.sort(); cc.dedup(); let xs = xs.into_iter() .map(|x| cc.binary_search(&x).unwrap()) .collect::<Vec<_>>(); let mut seg: SEG<RangeAddSum> = SEG::new(n, (0, 0)); let mut bit = BIT::new(n); for (i, &x) in xs.iter().enumerate() { // debug!(x, bit.sum(x + 1)); seg.update(x, ((i - bit.sum(x + 1)) as i64, 0)); bit.add(x + 1, 1); } util::with_bufwriter(|mut out| { for i in 0..n { /* debug!(i, xs[i]); for j in 0..n { debug!(j, seg.get(j).unwrap()); } */ writeln!(out, "{}", seg.query(0, n).unwrap()).unwrap(); seg.range_add(&(-1), 0, xs[i]); let y = seg.get(xs[i]).unwrap(); // seg.update(xs[i], (n as i64 - (xs[i] + 1) as i64, 0)); seg.add(&(cc.len() as i64 - (xs[i] + 1) as i64 - y), xs[i]); } }); }