結果

問題 No.1625 三角形の質問
ユーザー akakimidoriakakimidori
提出日時 2021-07-23 22:02:18
言語 Rust
(1.72.1)
結果
AC  
実行時間 840 ms / 6,000 ms
コード長 5,737 bytes
コンパイル時間 1,642 ms
コンパイル使用メモリ 157,716 KB
実行使用メモリ 75,864 KB
最終ジャッジ日時 2023-09-25 22:03:06
合計ジャッジ時間 14,122 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 31 ms
7,128 KB
testcase_02 AC 234 ms
31,356 KB
testcase_03 AC 285 ms
35,664 KB
testcase_04 AC 188 ms
23,568 KB
testcase_05 AC 462 ms
46,060 KB
testcase_06 AC 772 ms
70,972 KB
testcase_07 AC 785 ms
71,224 KB
testcase_08 AC 790 ms
70,788 KB
testcase_09 AC 806 ms
71,500 KB
testcase_10 AC 811 ms
70,676 KB
testcase_11 AC 840 ms
71,032 KB
testcase_12 AC 786 ms
71,180 KB
testcase_13 AC 788 ms
71,044 KB
testcase_14 AC 792 ms
71,076 KB
testcase_15 AC 795 ms
70,864 KB
testcase_16 AC 98 ms
29,896 KB
testcase_17 AC 227 ms
62,608 KB
testcase_18 AC 40 ms
19,652 KB
testcase_19 AC 287 ms
75,864 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