QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#549893 | #3034. Antennas | crimson231 | AC ✓ | 470ms | 6240kb | Rust | 4.2kb | 2024-09-06 23:10:20 | 2024-09-06 23:10:20 |
Judging History
answer
use std::io::{self, BufRead, Write};
#[derive(Debug, Clone, Copy)]
struct Pos {
x: i64,
y: i64,
}
fn cross(p1: &Pos, p2: &Pos, p3: &Pos) -> i64 {
(p2.x - p1.x) * (p3.y - p2.y) - (p2.y - p1.y) * (p3.x - p2.x)
}
fn ccw(p1: &Pos, p2: &Pos, p3: &Pos) -> i32 {
let c = cross(p1, p2, p3);
if c == 0 { 0 }
else if c > 0 { 1 }
else { -1 }
}
fn conquer(l: usize, m: usize, r: usize, arr: &mut Vec<Pos>, tmp: &mut Vec<Pos>, pvt: &Pos) -> i64 {
let mut i = l;
let mut j = m + 1;
let mut t = l;
let mut count = 0;
while i <= m && j <= r {
if cross(pvt, &arr[i], &arr[j]) > 0 {
tmp[t] = arr[i];
t += 1; i += 1;
}
else {
tmp[t] = arr[j];
t += 1; j += 1;
count += m - i + 1;
}
}
while i <= m { tmp[t] = arr[i]; t += 1; i += 1; }
while j <= r { tmp[t] = arr[j]; t += 1; j += 1; }
i = l;
while i <= r { arr[i] = tmp[i]; i += 1; }
count as i64
}
fn divide(l: usize, r: usize, arr: &mut Vec<Pos>, tmp: &mut Vec<Pos>, pvt: &Pos) -> i64 {
let mut ret = 0;
if l < r {
let m = l + r >> 1;
ret += divide(l, m, arr, tmp, pvt);
ret += divide(m + 1, r, arr, tmp, pvt);
ret += conquer(l, m, r, arr, tmp, pvt);
}
ret
}
fn merge_sort(arr: &mut Vec<Pos>, tmp: &mut Vec<Pos>, pvt: &Pos) -> i64 {
divide(0, arr.len() - 1, arr, tmp, pvt)
}
fn query(p1: &Pos, p2: &Pos, p3: &Pos, p4: &Pos, ant: &mut Vec<Pos>) -> i64 {
let mut tmp: Vec<Pos> = vec![Pos { x: 0, y: 0 }; ant.len()];
merge_sort(ant, &mut tmp, p2);
let x = merge_sort(ant, &mut tmp, p3);
let mut fuck: Vec<Pos> = vec![];
for &mut p in &mut *ant {
if ccw(p2, p4, &p) < 0 {
fuck.push(p);
}
}
let y = if fuck.len() > 0 {
merge_sort(&mut fuck, &mut tmp, p2);
merge_sort(&mut fuck, &mut tmp, p4)
} else { 0 };
let mut suck: Vec<Pos> = vec![];
for &mut p in &mut *ant {
if ccw(p1, p3, &p) < 0 {
suck.push(p);
}
}
let z = if suck.len() > 0 {
merge_sort(&mut suck, &mut tmp, p1);
merge_sort(&mut suck, &mut tmp, p3)
} else { 0 };
let cmb1 = ant.len() as i64 * (ant.len() as i64 - 1) / 2;
let cmb2 = fuck.len() as i64 * (fuck.len() as i64 - 1) / 2;
let cmb3 = suck.len() as i64 * (suck.len() as i64 - 1) / 2;
cmb1- x + y - cmb2 + z - cmb3
}
fn main() {
let mut iter = io::stdin().lock().lines();
let mut writer = io::BufWriter::new(io::stdout());
let z: usize = iter.next().unwrap().unwrap().trim().parse().unwrap();
for _ in 0..z {
iter.next(); // empty line
let n: usize = iter.next().unwrap().unwrap().trim().parse().unwrap();
let mut hull: Vec<Pos> = vec![];
for _ in 0..n {
let v: Vec<i64> = iter.next().unwrap().unwrap()
.split_whitespace().map(|x| x.parse().unwrap()).collect();
hull.push(Pos { x: v[0], y: v[1]});
}
let m: usize = iter.next().unwrap().unwrap().trim().parse().unwrap();
let mut ant: Vec<Pos> = vec![];
for _ in 0..m {
let v: Vec<i64> = iter.next().unwrap().unwrap()
.split_whitespace().map(|x| x.parse().unwrap()).collect();
ant.push(Pos { x: v[0], y: v[1]});
}
let q: usize = iter.next().unwrap().unwrap().trim().parse().unwrap();
for _ in 0..q {
let v: Vec<usize> = iter.next().unwrap().unwrap()
.split_whitespace().map(|x| x.parse().unwrap()).collect();
let mut x: Vec<Pos> = vec![];
let p1 = &hull[v[0]];
let p2 = &hull[(v[0] + 1) % n];
let p3 = &hull[v[1]];
let p4 = &hull[(v[1] + 1) % n];
for p in &ant {
if ccw(p1, p4, p) >= 0 && ccw(p2, p3, p) <= 0 {
x.push(*p);
}
}
_ = write!(writer, "{} ", if x.len() > 0 {
query(p1, p2, p3, p4, &mut x)
} else { 0 });
}
_ = writeln!(writer);
}
writer.flush().unwrap();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 275ms
memory: 2144kb
input:
116799 4 0 2 2 3 2 1 0 0 2 1 1 1 2 6 1 3 1 2 0 2 0 3 0 1 2 3 4 0 2 2 3 2 1 0 0 2 1 1 1 2 6 1 2 2 3 0 2 0 1 1 3 0 3 4 0 2 1 3 3 1 0 0 2 1 2 2 1 6 0 2 0 3 1 2 0 1 1 3 2 3 5 0 2 1 3 2 3 3 1 0 0 2 1 2 2 1 10 2 3 0 1 1 2 1 4 1 3 0 4 2 4 0 3 0 2 3 4 5 0 2 1 3 2 3 3 1 0 0 2 1 2 2 1 10 2 4 2 3 3 4 1 2 ...
output:
0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 ...
result:
ok 116799 lines
Test #2:
score: 0
Accepted
time: 446ms
memory: 6240kb
input:
6 3 -1000000000 -1000000000 -1000000000 1000000000 1000000000 -1000000000 50000 -157866428 -481268129 -943828535 -404551206 -702156200 -918976587 -930672851 -662946583 -701356764 -690150251 -125890933 -15199649 -454987413 -601182432 -796857455 -452853385 -364662709 -332922528 -446377184 -59908020 -...
output:
422781691 405698933 421494376 90964408 112140449 58183510 0 86959753 401170255 0 83964813 77431438 36622679 0 0 0 201707235 298189155 0 20661817 1108613238 83335 103419782 17196828 0 109534039 102221733 35500923 0 107684911 381431817 0 100815183 107832883 20782102 28845692 14952512 28864912 378...
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 470ms
memory: 2132kb
input:
150000 10 128337801 981646274 680824665 718733451 973259647 181288879 893942524 -425401883 473169741 -869603585 -128337801 -981646274 -680824665 -718733451 -973259647 -181288879 -893942524 425401883 -473169741 869603585 2 0 900000000 0 -900000000 10 4 8 1 2 6 7 5 6 0 8 6 8 1 8 3 4 5 7 0 6 10 12833...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 150000 lines