結果

問題 No.1625 三角形の質問
ユーザー akakimidoriakakimidori
提出日時 2021-07-23 22:02:18
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 787 ms / 6,000 ms
コード長 5,737 bytes
コンパイル時間 22,833 ms
コンパイル使用メモリ 390,984 KB
実行使用メモリ 74,368 KB
最終ジャッジ日時 2024-07-18 17:44:24
合計ジャッジ時間 24,350 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 28 ms
7,040 KB
testcase_02 AC 224 ms
27,600 KB
testcase_03 AC 280 ms
32,324 KB
testcase_04 AC 171 ms
22,912 KB
testcase_05 AC 443 ms
44,476 KB
testcase_06 AC 787 ms
64,556 KB
testcase_07 AC 785 ms
64,556 KB
testcase_08 AC 675 ms
64,044 KB
testcase_09 AC 713 ms
64,824 KB
testcase_10 AC 670 ms
64,112 KB
testcase_11 AC 703 ms
64,568 KB
testcase_12 AC 712 ms
64,800 KB
testcase_13 AC 713 ms
64,680 KB
testcase_14 AC 702 ms
64,680 KB
testcase_15 AC 772 ms
64,048 KB
testcase_16 AC 84 ms
29,952 KB
testcase_17 AC 217 ms
60,032 KB
testcase_18 AC 38 ms
19,456 KB
testcase_19 AC 261 ms
74,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// (-inf, x] * (-inf, y] の和を求める
// 加算する点は先読みして突っ込んでおく
// ---------- begin rectangle sum ----------
pub struct RectangleSum<Index, Weight> {
    point: Vec<(Index, Index)>,
    row: Vec<Index>,
    col: Vec<Vec<Index>>,
    bit: Vec<Vec<Weight>>,
    zero: Weight,
}

impl<Index, Weight> RectangleSum<Index, Weight>
where
    Index: Ord + Copy,
    Weight: std::ops::Add<Output = Weight> + Copy,
{
    pub fn new(zero: Weight) -> Self {
        RectangleSum {
            point: vec![],
            row: vec![],
            col: vec![],
            bit: vec![],
            zero: zero,
        }
    }
    pub fn add_point(&mut self, x: Index, y: Index) {
        self.point.push((x, y));
    }
    pub fn build(&mut self) {
        let mut row: Vec<_> = self.point.iter().map(|p| p.0).collect();
        row.sort();
        row.dedup();
        let mut col = vec![vec![]; row.len() + 1];
        for &(x, y) in self.point.iter() {
            let mut k = row.binary_search(&x).unwrap() + 1;
            while let Some(a) = col.get_mut(k) {
                a.push(y);
                k += k & (!k + 1);
            }
        }
        let mut bit = vec![vec![]; row.len() + 1];
        for (bit, col) in bit.iter_mut().zip(col.iter_mut()).skip(1) {
            col.sort();
            col.dedup();
            bit.resize(col.len() + 1, self.zero);
        }
        self.row = row;
        self.col = col;
        self.bit = bit;
    }
    pub fn add(&mut self, x: Index, y: Index, w: Weight) {
        let mut x = self.row.binary_search(&x).unwrap() + 1;
        while let Some(bit) = self.bit.get_mut(x) {
            let mut y = self.col[x].binary_search(&y).unwrap() + 1;
            while let Some(v) = bit.get_mut(y) {
                *v = *v + w;
                y += y & (!y + 1);
            }
            x += x & (!x + 1);
        }
    }
    // (-inf, x] * (-inf * y] の和
    pub fn find(&self, x: Index, y: Index) -> Weight {
        let upper_bound = |x: &[Index], v: &Index| -> usize {
            use std::cmp::Ordering::Less;
            x.binary_search_by(|p| p.cmp(v).then(Less)).unwrap_err()
        };
        let mut x = upper_bound(&self.row, &x);
        let mut ans = self.zero;
        while x > 0 {
            let mut y = upper_bound(&self.col[x], &y);
            let bit = &self.bit[x];
            while y > 0 {
                ans = ans + bit[y];
                y -= y & (!y + 1);
            }
            x -= x & (!x + 1);
        }
        ans
    }
}
// ---------- end rectangle sum ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

#[derive(Clone, Copy)]
struct Value(i64);

impl std::ops::Add for Value {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Value(self.0.max(rhs.0))
    }
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let n: usize = sc.next();
    let q: usize = sc.next();
    let mut a = vec![(0i64, 0i64, 0i64, 0i64, 0i64, 0i64); n];
    let mut ask = vec![(0u8, 0i64, 0i64, 0i64, 0i64, 0i64, 0i64); q];
    let mut rect = RectangleSum::new(Value(-1));
    for a in a.iter_mut() {
        a.0 = sc.next();
        a.1 = sc.next();
        a.2 = sc.next();
        a.3 = sc.next();
        a.4 = sc.next();
        a.5 = sc.next();
        let l = a.0.min(a.2).min(a.4);
        let r = a.0.max(a.2).max(a.4);
        rect.add_point(-l, r);
    }
    for a in ask.iter_mut() {
        a.0 = sc.next();
        if a.0 == 1 {
            a.1 = sc.next();
            a.2 = sc.next();
            a.3 = sc.next();
            a.4 = sc.next();
            a.5 = sc.next();
            a.6 = sc.next();
            let l = a.1.min(a.3).min(a.5);
            let r = a.1.max(a.3).max(a.5);
            rect.add_point(-l, r);
        } else {
            a.1 = sc.next();
            a.2 = sc.next();
        }
    }
    rect.build();
    for a in a {
        let p = (a.2 - a.0, a.3 - a.1);
        let q = (a.4 - a.0, a.5 - a.1);
        let s = (p.0 * q.1 - p.1 * q.0).abs();
        let l = a.0.min(a.2).min(a.4);
        let r = a.0.max(a.2).max(a.4);
        rect.add(-l, r, Value(s));
    }
    for a in ask {
        if a.0 == 1 {
            let p = (a.3 - a.1, a.4 - a.2);
            let q = (a.5 - a.1, a.6 - a.2);
            let s = (p.0 * q.1 - p.1 * q.0).abs();
            let l = a.1.min(a.3).min(a.5);
            let r = a.1.max(a.3).max(a.5);
            rect.add(-l, r, Value(s));
        } else {
            let ans = rect.find(-a.1, a.2).0;
            writeln!(out, "{}", ans).ok();
        }
    }
}
0