結果

問題 No.2628 Shrinkage
ユーザー nautnaut
提出日時 2024-02-16 22:11:42
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 6,306 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 182,424 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-16 22:11:45
合計ジャッジ時間 1,833 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
6,676 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
6,676 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*, BufWriter, StdinLock, StdoutLock};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let t = input!(usize);

    for _ in 0..t {
        solve(&mut scan, &mut out);
    }
}

fn solve(scan: &mut Scanner<StdinLock>, out: &mut BufWriter<StdoutLock>) {
    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let (x1, y1, x2, y2, X1, Y1, X2, Y2) = (
        input!(i128),
        input!(i128),
        input!(i128),
        input!(i128),
        input!(i128),
        input!(i128),
        input!(i128),
        input!(i128),
    );

    // (x1, y1), (X1, Y1) を通る直線の方程式と (x2, y2), (X2, Y2) を通る直線の方程式を求める
    // (x1 < X1 < cx) or (cx < x1 < X1) みたいな位置関係でないなら一致できない?

    let is_parallel = {
        let a1 = Y1 - y1;
        let b1 = x1 - X1;
        let a2 = Y2 - y2;
        let b2 = x2 - X2;

        a1 * b2 - a2 * b1 == 0
    };

    if is_parallel {
        if (x1 < X1 && X1 < X2 && X2 < x2) || (x2 < X2 && X2 < X1 && X1 < x1) {
            if (y1 < Y1 && Y1 < Y2 && Y2 < y2) || (y2 < Y2 && Y2 < Y1 && Y1 < y1) {
                let a1 = Y1 - y1;
                let b1 = x1 - X1;
                let a2 = Y2 - y2;
                let b2 = x2 - X2;
                let c1 = X1 * y1 - x1 * Y1;
                let c2 = X2 * y2 - x2 * Y2;

                if a1 * b2 == a2 * b1 && a1 * c2 == a2 * c1 {
                    writeln!(out, "Yes");
                    return;
                }
            }
        }

        writeln!(out, "No");
        return;
    } else {
        let a1 = Y1 - y1;
        let b1 = x1 - X1;
        let a2 = Y2 - y2;
        let b2 = x2 - X2;
        let c1 = X1 * y1 - x1 * Y1;
        let c2 = X2 * y2 - x2 * Y2;

        let d = a1 * b2 - a2 * b1;

        let cx = (b1 * c2 - b2 * c1) as f64 / d as f64;
        let cy = (a2 * c1 - a1 * c2) as f64 / d as f64;

        // cx == X1 とか cy == Y1 とかだと一致できない
        if (b1 * c2 - b2 * c1) == X1 * d || (b1 * c2 - b2 * c1) == X2 * d {
            writeln!(out, "No");
            return;
        }

        if (a2 * c1 - a1 * c2) == Y1 * d || (a2 * c1 - a1 * c2) == Y2 * d {
            writeln!(out, "No");
            return;
        }

        let e1 = b1 * c2 - b2 * c1;
        let e2 = a2 * c1 - a1 * c2;

        if d > 0 {
            if (e1 < X1 * d && X1 < x1) || (x1 < X1 && X1 * d < e1) {
                if (e1 < X2 * d && X2 < x2) || (x2 < X2 && X2 * d < e1) {
                    if (e2 < Y1 * d && Y1 < y1) || (y1 < Y1 && Y1 * d < e2) {
                        if (e2 < Y2 * d && Y2 < y2) || (y2 < Y2 && Y2 * d < e2) {
                            let dx11 = cx - x1 as f64;
                            let dy11 = cy - y1 as f64;

                            let dx12 = cx - X1 as f64;
                            let dy12 = cy - Y1 as f64;

                            let dx21 = cx - x2 as f64;
                            let dy21 = cy - y2 as f64;

                            let dx22 = cx - X2 as f64;
                            let dy22 = cy - Y2 as f64;

                            if ((dx11 * dx11 + dy11 * dy11) * (dx22 * dx22 + dy22 * dy22)
                                - (dx12 * dx12 + dy12 * dy12) * (dx21 * dx21 + dy21 * dy21))
                                .abs()
                                < 1e-6
                            {
                                writeln!(out, "Yes");
                                return;
                            }
                        }
                    }
                }
            }
        } else {
            if (e1 > X1 * d && X1 < x1) || (x1 < X1 && X1 * d > e1) {
                if (e1 > X2 * d && X2 < x2) || (x2 < X2 && X2 * d > e1) {
                    if (e2 > Y1 * d && Y1 < y1) || (y1 < Y1 && Y1 * d > e2) {
                        if (e2 > Y2 * d && Y2 < y2) || (y2 < Y2 && Y2 * d > e2) {
                            let dx11 = cx - x1 as f64;
                            let dy11 = cy - y1 as f64;

                            let dx12 = cx - X1 as f64;
                            let dy12 = cy - Y1 as f64;

                            let dx21 = cx - x2 as f64;
                            let dy21 = cy - y2 as f64;

                            let dx22 = cx - X2 as f64;
                            let dy22 = cy - Y2 as f64;

                            if ((dx11 * dx11 + dy11 * dy11) * (dx22 * dx22 + dy22 * dy22)
                                - (dx12 * dx12 + dy12 * dy12) * (dx21 * dx21 + dy21 * dy21))
                                .abs()
                                < 1e-6
                            {
                                writeln!(out, "Yes");
                                return;
                            }
                        }
                    }
                }
            }
        }

        writeln!(out, "No");
    }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0