結果
問題 | No.482 あなたの名は |
ユーザー |
|
提出日時 | 2017-02-10 22:40:45 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 3,897 bytes |
コンパイル時間 | 17,637 ms |
コンパイル使用メモリ | 389,424 KB |
実行使用メモリ | 8,384 KB |
最終ジャッジ日時 | 2024-12-29 20:15:44 |
合計ジャッジ時間 | 19,825 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
コンパイルメッセージ
warning: method `is_same_set` is never used --> src/main.rs:108:8 | 87 | impl UnionFind { | -------------- method in this implementation ... 108 | fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool { | ^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
ソースコード
#[allow(unused_imports)]use std::cmp::*;#[allow(unused_imports)]use std::collections::*;use std::io::*;#[allow(dead_code)]fn getline() -> String {let mut ret = String::new();std::io::stdin().read_line(&mut ret).ok();return ret;}fn get_word() -> String {let mut stdin = std::io::stdin();let mut u8b: [u8; 1] = [0];loop {let mut buf: Vec<u8> = Vec::with_capacity(16);loop {let res = stdin.read(&mut u8b);if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {break;} else {buf.push(u8b[0]);}}if buf.len() >= 1 {let ret = std::string::String::from_utf8(buf).unwrap();return ret;}}}fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }#[allow(dead_code)]fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }/*** Segment Tree. This data structure is useful for fast folding on intervals of an array* whose elements are elements of monoid M. Note that constructing this tree requires the identity* element of M and the operation of M.* Verified by: yukicoder No. 259 (http://yukicoder.me/submissions/100581)*/struct SegTree<I, BiOp> {n: usize,dat: Vec<I>,op: BiOp,e: I,}impl<I, BiOp> SegTree<I, BiOp>where BiOp: Fn(I, I) -> I,I: Copy {pub fn new(n_: usize, op: BiOp, e: I) -> Self {let mut n = 1;while n < n_ { n *= 2; } // n is a power of 2SegTree {n: n, dat: vec![e; 2 * n - 1], op: op, e: e}}/* ary[k] <- v */pub fn update(&mut self, idx: usize, v: I) {let mut k = idx + self.n - 1;self.dat[k] = v;while k > 0 {k = (k - 1) / 2;self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]);}}/* l,r are for simplicity */fn query_sub(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> I {// [a,b) and [l,r) intersects?if r <= a || b <= l { return self.e; }if a <= l && r <= b { return self.dat[k]; }let vl = self.query_sub(a, b, 2 * k + 1, l, (l + r) / 2);let vr = self.query_sub(a, b, 2 * k + 2, (l + r) / 2, r);(self.op)(vl, vr)}/* [a, b] (note: inclusive) */pub fn query(&self, a: usize, b: usize) -> I {self.query_sub(a, b + 1, 0, 0, self.n)}}/*** Union-Find tree.* Verified by yukicoder No.94 (http://yukicoder.me/submissions/82111)*/struct UnionFind { disj: Vec<usize> }impl UnionFind {fn new(n: usize) -> Self {let mut disj = vec![0; n];for i in 0 .. n {disj[i] = i;}UnionFind { disj: disj }}fn root(self: &mut Self, x: usize) -> usize {if x != self.disj[x] {let par = self.disj[x];let r = self.root(par);self.disj[x] = r;}return self.disj[x];}fn unite(self: &mut Self, x: usize, y: usize) {let xr = self.root(x);let yr = self.root(y);self.disj[xr] = yr;}fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool {return self.root(x) == self.root(y);}}fn main() {let n = get();let k: i64 = get();let d: Vec<usize> = (0 .. n).map(|_| get::<usize>()).collect();let mut st = SegTree::new(n + 1, |x, y| x + y, 0i64);let mut uf = UnionFind::new(n);let mut inv: i64 = 0;let mut derange = 0;for i in 0 .. n {inv += st.query(d[i] + 1, n);let tmp = st.query(d[i], d[i]);st.update(d[i], tmp + 1);uf.unite(i, d[i] - 1);}inv %= 2;for i in 0 .. n {if uf.root(i) != i {derange += 1;}}println!("{}", if (k + inv) % 2 == 0 && derange as i64 <= k { "YES" } else { "NO" });}