結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー koba-e964koba-e964
提出日時 2021-11-17 11:58:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 4,074 bytes
コンパイル時間 7,604 ms
コンパイル使用メモリ 159,772 KB
実行使用メモリ 15,560 KB
最終ジャッジ日時 2023-08-24 22:22:34
合計ジャッジ時間 18,754 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 13 ms
4,380 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 11 ms
4,384 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 42 ms
8,256 KB
testcase_24 AC 74 ms
10,764 KB
testcase_25 AC 122 ms
12,456 KB
testcase_26 AC 90 ms
10,876 KB
testcase_27 AC 87 ms
7,912 KB
testcase_28 AC 41 ms
4,856 KB
testcase_29 AC 37 ms
6,088 KB
testcase_30 AC 147 ms
14,212 KB
testcase_31 AC 145 ms
15,560 KB
testcase_32 AC 95 ms
12,748 KB
testcase_33 AC 110 ms
10,360 KB
testcase_34 AC 89 ms
9,248 KB
testcase_35 AC 70 ms
10,864 KB
testcase_36 AC 125 ms
14,720 KB
testcase_37 AC 27 ms
5,376 KB
testcase_38 AC 51 ms
10,320 KB
testcase_39 AC 133 ms
12,556 KB
testcase_40 AC 130 ms
12,440 KB
testcase_41 AC 115 ms
13,304 KB
testcase_42 AC 91 ms
7,724 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 74 ms
14,352 KB
testcase_45 AC 41 ms
4,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{Write, BufWriter};
// 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),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Quick-Find data structure.
// Verified by: https://atcoder.jp/contests/cf17-tournament-round3-open/submissions/22928265
// Verified by: https://atcoder.jp/contests/ttpc2019/submissions/23384553 (polymorphic version)
struct QuickFind<T = ()> {
    root: Vec<usize>, mem: Vec<Vec<usize>>,
    dat: Vec<T>, default: T,
}

impl QuickFind<()> {
    #[allow(unused)]
    fn new(n: usize) -> Self {
        Self::with_dat(n, ())
    }
    #[allow(unused)]
    fn unite(&mut self, x: usize, y: usize) {
        self.unite_with_hooks(x, y, |&(), _| (), |(), ()| ());
    }
}
impl<T: Clone> QuickFind<T> {
    fn with_dat(n: usize, def: T) -> Self {
        let root = (0..n).collect();
        let mut mem = vec![vec![]; n];
        for i in 0..n {
            mem[i] = vec![i];
        }
        QuickFind { root: root, mem: mem, dat: vec![def.clone(); n], default: def }
    }
    fn root(&self, x: usize) -> usize {
        self.root[x]
    }
    #[allow(unused)]
    fn set(&mut self, idx: usize, val: T) {
        let r = self.root[idx];
        self.dat[r] = val;
    }
    // unite always merges y to x if |x| >= |y|.
    fn unite_with_hooks<F: FnMut(&T, i64), G: FnMut(T, T) -> T>(
        &mut self, x: usize, y: usize,
        mut hook: F, mut merge: G) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.mem[x].len() < self.mem[y].len() {
            std::mem::swap(&mut x, &mut y);
        }
        let memy = std::mem::replace(&mut self.mem[y], vec![]);
        for &v in &memy {
            self.root[v] = x;
        }
        self.mem[x].extend_from_slice(&memy);
        // hook
        hook(&self.dat[x], -1);
        hook(&self.dat[y], -1);
        self.dat[x] = merge(
            std::mem::replace(&mut self.dat[x], self.default.clone()),
            std::mem::replace(&mut self.dat[y], self.default.clone()),
        );
        hook(&self.dat[x], 1);
    }
    #[allow(unused)]
    fn is_same_set(&self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&self, x: usize) -> usize {
        let x = self.root(x);
        self.mem[x].len()
    }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize,
        a: [f64; n],
        b: [f64; n],
        p: [f64; n],
        q: usize,
        xy:[(usize1, usize1); q],
    }
    let mut qf = QuickFind::with_dat(n, (0.0, 0.0, 0.0));
    let mut tot = 0.0;
    for i in 0..n {
        qf.set(i, (a[i], b[i], 1.0 / p[i]));
        tot += (a[i] - b[i]) * (a[i] - b[i]) * p[i];
    }
    for &(x, y) in &xy {
        qf.unite_with_hooks(x, y, |(a, b, pinv), dir| {
            tot += (a - b) * (a - b) / pinv * dir as f64;
        }, |(a1, b1, pinv1), (a2, b2, pinv2)| (a1 + a2, b1 + b2, pinv1 + pinv2));
        puts!("{}\n", tot);
    }
}
0