AtCoder Beginner Contest 406 D

https://atcoder.jp/contests/abc406/tasks/abc406_d

同じxのyと同じyのxを集めるだけですが、時間的けっこう厳しいです。

// Garbage Removal
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// Table ////////////////////

use std::collections::HashSet;

struct Table {
    vec_x: Vec<HashSet<usize>>,     // [{y}]
    vec_y: Vec<HashSet<usize>>,
}

impl Table {
    fn insert(&mut self, (x, y): Point) {
        self.vec_x[x].insert(y);
        self.vec_y[y].insert(x);
    }
    
    fn remove_x(&mut self, x: usize) -> usize{
        let n = self.vec_x[x].len();
        for &y in self.vec_x[x].iter() {
            self.vec_y[y].remove(&x);
        }
        self.vec_x[x].clear();
        n
    }
    
    fn remove_y(&mut self, y: usize) -> usize {
        let n = self.vec_y[y].len();
        for &x in self.vec_y[y].iter() {
            self.vec_x[x].remove(&y);
        }
        self.vec_y[y].clear();
        n
    }
    
    fn create(H: usize, W: usize, garbage: Vec<Point>) -> Table {
        let vec_x: Vec<HashSet<usize>> = vec![HashSet::new(); H];
        let vec_y: Vec<HashSet<usize>> = vec![HashSet::new(); W];
        let mut table = Table { vec_x, vec_y };
        for g in garbage {
            table.insert(g)
        }
        table
    }
}


//////////////////// Query ////////////////////

enum Query {
    RemoveX(usize),
    RemoveY(usize),
}

impl Query {
    fn read() -> Query {
        let v: Vec<usize> = read_vec();
        if v[0] == 1 {
            Query::RemoveX(v[1]-1)
        }
        else {
            Query::RemoveY(v[1]-1)
        }
    }
    
    fn query(q: Query, table: &mut Table) -> usize {
        match q {
            Query::RemoveX(x) => table.remove_x(x),
            Query::RemoveY(y) => table.remove_y(y)
        }
    }
}


//////////////////// process ////////////////////

type Point = (usize, usize);

fn read_point() -> Point {
    let v: Vec<usize> = read_vec();
    (v[0] - 1, v[1] - 1)
}

fn read_input() -> (usize, usize, Vec<Point>, usize) {
    let v: Vec<usize> = read_vec();
    let (H, W, N) = (v[0], v[1], v[2]);
    let garbase: Vec<Point> = (0..N).map(|_| read_point()).collect();
    let Q: usize = read();
    (H, W, garbase, Q)
}

fn F(H: usize, W: usize, garbage: Vec<Point>, Q: usize) {
    let mut table = Table::create(H, W, garbage);
    for _ in 0..Q {
        let q = Query::read();
        println!("{}", Query::query(q, &mut table))
    }
}

fn main() {
    let (H, W, garbage, Q) = read_input();
    F(H, W, garbage, Q)
}