結果

問題 No.2292 Interval Union Find
ユーザー koba-e964koba-e964
提出日時 2023-07-24 10:59:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 4,070 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 187,888 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-08 13:01:26
合計ジャッジ時間 14,933 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 202 ms
6,676 KB
testcase_05 AC 245 ms
6,676 KB
testcase_06 AC 200 ms
6,676 KB
testcase_07 AC 235 ms
6,676 KB
testcase_08 AC 249 ms
6,676 KB
testcase_09 AC 241 ms
6,676 KB
testcase_10 AC 230 ms
6,676 KB
testcase_11 AC 222 ms
6,676 KB
testcase_12 AC 245 ms
6,676 KB
testcase_13 AC 219 ms
6,676 KB
testcase_14 AC 223 ms
6,676 KB
testcase_15 AC 224 ms
6,676 KB
testcase_16 AC 222 ms
6,676 KB
testcase_17 AC 236 ms
6,676 KB
testcase_18 AC 136 ms
9,088 KB
testcase_19 AC 199 ms
7,552 KB
testcase_20 AC 212 ms
7,680 KB
testcase_21 AC 243 ms
6,676 KB
testcase_22 AC 249 ms
6,676 KB
testcase_23 AC 231 ms
6,676 KB
testcase_24 AC 234 ms
6,676 KB
testcase_25 AC 238 ms
6,676 KB
testcase_26 AC 238 ms
6,676 KB
testcase_27 AC 240 ms
6,676 KB
testcase_28 AC 237 ms
6,676 KB
testcase_29 AC 237 ms
6,676 KB
testcase_30 AC 237 ms
6,676 KB
testcase_31 AC 238 ms
6,676 KB
testcase_32 AC 240 ms
6,676 KB
testcase_33 AC 242 ms
6,676 KB
testcase_34 AC 231 ms
6,676 KB
testcase_35 AC 245 ms
6,676 KB
testcase_36 AC 243 ms
6,676 KB
testcase_37 AC 236 ms
6,676 KB
testcase_38 AC 241 ms
6,676 KB
testcase_39 AC 235 ms
6,676 KB
testcase_40 AC 236 ms
6,676 KB
testcase_41 AC 186 ms
6,676 KB
testcase_42 AC 187 ms
6,676 KB
testcase_43 AC 189 ms
6,676 KB
testcase_44 AC 198 ms
6,676 KB
testcase_45 AC 198 ms
6,676 KB
testcase_46 AC 199 ms
6,676 KB
testcase_47 AC 206 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// Port from https://satanic0258.github.io/snippets/data-structure/SegmentMap.html
// Verified by: https://yukicoder.me/submissions/701257
//              https://codeforces.com/contest/1556/submission/129318651
#[derive(Clone, Debug, Default)]
struct Segs {
    s: std::collections::BTreeMap<i64, i64>,
}

impl Segs {
    fn new() -> Self { Default::default() }
    // Returns a segment that contains x.
    #[allow(unused)]
    fn get(&self, x: i64) -> Option<(i64, i64)> {
        if let Some((&l, &r)) = self.s.range(..=x).rev().next() {
            if x < r {
                Some((l, r))
            } else {
                None
            }
        } else {
            None
        }
    }
    // adds [l, r).
    fn add(&mut self, rng: std::ops::Range<i64>) {
        let (mut l, mut r) = (rng.start, rng.end);
        assert!(l <= r);
        if l == r { return; }
        fn deref((&x, &y): (&i64, &i64)) -> (i64, i64) { (x, y) }
        let mut p = self.s.range(..l).rev().next().map(deref);
        if p.is_none() {
            p = self.s.iter().next().map(deref);
        }
        while let Some((a, b)) = p {
            if a > r { break; }
            if b >= l {
                l = std::cmp::min(l, a);
                r = std::cmp::max(r, b);
                self.s.remove(&a);
            }
            p = self.s.range(a + 1..).next().map(deref);
        }
        self.s.insert(l, r);
    }
    // removes [l, r).
    #[allow(unused)]
    fn remove(&mut self, rng: std::ops::Range<i64>) {
        let (l, r) = (rng.start, rng.end);
        assert!(l <= r);
        if l == r { return; }
        fn deref((&x, &y): (&i64, &i64)) -> (i64, i64) { (x, y) }
        let mut p = self.s.range(..l).rev().next().map(deref);
        if p.is_none() {
            p = self.s.iter().next().map(deref);
        }
        let mut tl = std::i64::MAX;
        let mut tr = std::i64::MIN;
        while let Some((a, b)) = p {
            if a > r { break; }
            if b >= l {
                tl = std::cmp::min(tl, a);
                tr = std::cmp::max(tr, b);
                self.s.remove(&a);
            }
            p = self.s.range(a + 1..).next().map(deref);
        }
        if tl < l { self.s.insert(tl, l); }
        if r < tr { self.s.insert(r, tr); }
    }
    #[allow(unused)]
    fn each<F: FnMut(i64, i64)>(&self, mut f: F) {
        for (&x, &y) in &self.s { f(x, y); }
    }
}

fn main() {
    let _n: i64 = get();
    let q: i32 = get();
    let mut segs = Segs::new();
    for _ in 0..q {
        let ty: i32 = get();
        let u: i64 = get();
        // eprintln!("{:?} {}", segs, ty);
        if ty == 4 {
            let (l, r) = segs.get(2 * u).unwrap_or((2 * u, 2 * u + 1));
            println!("{}", (r - l - 1) / 2 + 1);
            continue;
        }
        let v: i64 = get();
        if ty == 1 {
            segs.add(2 * u..2 * v + 1);
            continue;
        }
        if ty == 2 {
            segs.remove(2 * u + 1..2 * v);
            continue;
        }
        let (l, r) = segs.get(2 * u).unwrap_or((2 * u, 2 * u + 1));
        println!("{}", if l <= 2 * v && 2 * v < r {
            "1"
        } else {
            "0"
        });
    }
}
0