Submission #1689917


Source Code Expand

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

trait Monoid {
    fn mul(&self, Self) -> Self;

    fn one() -> Self;
}

struct SegmentTree<T> {
    n: usize,
    data: Vec<T>
}

impl<T: Monoid + Clone> SegmentTree<T> {
    fn new(n: usize, initial: T) -> Self {
        let vs = (n-1).next_power_of_two() << 1;
        SegmentTree { n: n, data: vec![initial; vs] }
    }

    fn new_with(v: &Vec<T>) -> Self {
        let vs = max(4, (v.len()-1).next_power_of_two() << 1);
        let n = v.len();
        let mut data: Vec<T> = vec![T::one(); vs];

        let bottom = vs/2-1;
        for i in 0..n {
            data[bottom+i] = v[i].clone();
        }
        for i in (0..bottom).rev() {
            data[i] = data[i*2+1].mul(data[i*2+2].clone());
        }
        SegmentTree { n: v.len(), data: data }
    }

    fn change(&mut self, idx: usize, new_value: T) {
        let mut pos = self.data.len() / 2 - 1 + idx;
        self.data[pos] = new_value;
        while pos >= 1 {
            let to = (pos - 1) / 2;
            self.data[to] = self.data[to*2+1].mul(self.data[to*2+2].clone());
            pos = to;
        }
    }

    fn range(&self, l: usize, r: usize) -> T {
        self.range2(l, r, 0, 0, self.data.len() / 2)
    }

    fn range2(&self, l: usize, r: usize, idx: usize, segl: usize, segr: usize) -> T {
        if r <= segl || segr <= l {
            return T::one()
        }
        if l <= segl && segr <= r {
            return self.data[idx].clone()
        }
        let med = (segl + segr) / 2;
        self.range2(l, r, idx*2+1, segl, med).mul(self.range2(l, r, idx*2+2, med, segr))
    }
}


// ===

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

impl Monoid for I64 {
    fn mul(&self, other: Self) -> Self {
        if self.0 < other.0 { other } else { *self }
    }

    fn one() -> Self { I64(0) }
}

// ===

fn solve0(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and bmax

    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 0..n {
        if bmax <= xy[i].1 {
            bmax = xy[i].1;
            bi = i;
        }
    }
    rmin = min(rmin, xy[bi].0);
    rmax = max(rmax, xy[bi].0);

    for i in 0..n {
        if i == 0 || i == bi {
            continue
        }
        bmin = min(bmin, xy[i].1);
        rmax = max(rmax, xy[i].0);
    }

    (rmax - rmin) * (bmax - bmin)
}

fn solve1(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and rmax
    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 1..n {
        if rmax <= xy[i].1 {
            rmax = xy[i].1;
            bi = i;
        }
    }
    bmin = min(bmin, xy[bi].0);
    bmax = max(bmax, xy[bi].0);

    if n == 2 {
        return (rmax - rmin) * (bmax - bmin)
    }

    let mut pairs = vec![];
    for i in 0..n {
        if i != 0 && i != bi {
            pairs.push(xy[i]);
        }
    }

    let pn = pairs.len();
    let mut r = SegmentTree::new(n+10, I64::one());
    for i in 0..pn {
        r.change(i, I64(pairs[i].1));
    }

    let mut best_blue = (0, 1e11 as i64);
    for i in 0..pn {
        let bmin = min(bmin, pairs[i].0);
        let bmax = max(bmax, max(r.range(0, i).0, r.range(i+1, pn).0));

        if best_blue.1 - best_blue.0 >= bmax - bmin {
            best_blue = (bmin, bmax);
        }
    }

    (rmax - rmin) * (best_blue.1 - best_blue.0)
}

fn main() {
    let n: usize = read();
    let mut xy: Vec<(i64, i64)> = readn(n);

    for i in 0..n {
        if xy[i].1 < xy[i].0 {
            xy[i] = (xy[i].1, xy[i].0);
        }
    }
    xy.sort();

    // println!("{:?}", xy);
    println!("{}", min(solve0(&xy), solve1(&xy)));
}

Submission Info

Submission Time
Task E - Ball Coloring
User hamadu
Language Rust (1.15.1)
Score 0
Code Size 5317 Byte
Status WA
Exec Time 183 ms
Memory 17724 KB

Compile Error

warning: variable does not need to be mutable, #[warn(unused_mut)] on by default
   --> ./Main.rs:177:9
    |
177 |     let mut rmin = xy[0].0;
    |         ^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 9
WA × 26
Set Name Test Cases
Sample example0, example1, example2
All div20, div21, div22, div23, div24, example0, example1, example2, maxrand0, maxrand1, maxrand2, maxrand20, maxrand21, maxrand210, maxrand211, maxrand22, maxrand23, maxrand24, maxrand25, maxrand26, maxrand27, maxrand28, maxrand29, maxrand3, maxrand4, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, sparse0, sparse1, sparse2, sparse3, sparse4
Case Name Status Exec Time Memory
div20 WA 182 ms 17688 KB
div21 WA 183 ms 17684 KB
div22 WA 180 ms 17700 KB
div23 WA 180 ms 17724 KB
div24 WA 181 ms 17684 KB
example0 AC 2 ms 4352 KB
example1 AC 2 ms 4352 KB
example2 AC 2 ms 4352 KB
maxrand0 WA 183 ms 17720 KB
maxrand1 WA 181 ms 17720 KB
maxrand2 WA 182 ms 17720 KB
maxrand20 WA 181 ms 17720 KB
maxrand21 WA 181 ms 17720 KB
maxrand210 WA 181 ms 17700 KB
maxrand211 WA 181 ms 17592 KB
maxrand22 WA 182 ms 17720 KB
maxrand23 WA 183 ms 17720 KB
maxrand24 WA 182 ms 17700 KB
maxrand25 WA 183 ms 17720 KB
maxrand26 WA 183 ms 17720 KB
maxrand27 WA 183 ms 17720 KB
maxrand28 WA 182 ms 17684 KB
maxrand29 WA 181 ms 17684 KB
maxrand3 WA 181 ms 17524 KB
maxrand4 WA 181 ms 17700 KB
smallrand0 AC 2 ms 4352 KB
smallrand1 WA 2 ms 4352 KB
smallrand2 WA 2 ms 4352 KB
smallrand3 WA 2 ms 4352 KB
smallrand4 WA 2 ms 4352 KB
sparse0 AC 180 ms 17524 KB
sparse1 AC 181 ms 17720 KB
sparse2 AC 180 ms 17684 KB
sparse3 AC 180 ms 17700 KB
sparse4 AC 180 ms 17720 KB