結果

問題 No.2294 Union Path Query (Easy)
ユーザー koba-e964koba-e964
提出日時 2023-07-03 11:02:42
言語 Rust
(1.77.0)
結果
AC  
実行時間 800 ms / 4,000 ms
コード長 5,515 bytes
コンパイル時間 1,408 ms
コンパイル使用メモリ 150,024 KB
実行使用メモリ 87,440 KB
最終ジャッジ日時 2023-09-24 08:05:02
合計ジャッジ時間 37,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 713 ms
86,272 KB
testcase_04 AC 666 ms
50,212 KB
testcase_05 AC 647 ms
44,304 KB
testcase_06 AC 705 ms
86,264 KB
testcase_07 AC 716 ms
86,540 KB
testcase_08 AC 649 ms
85,924 KB
testcase_09 AC 643 ms
86,012 KB
testcase_10 AC 789 ms
86,236 KB
testcase_11 AC 771 ms
86,292 KB
testcase_12 AC 775 ms
86,284 KB
testcase_13 AC 714 ms
85,968 KB
testcase_14 AC 621 ms
84,544 KB
testcase_15 AC 484 ms
84,768 KB
testcase_16 AC 800 ms
85,452 KB
testcase_17 AC 724 ms
87,440 KB
testcase_18 AC 750 ms
87,376 KB
testcase_19 AC 785 ms
87,108 KB
testcase_20 AC 779 ms
86,580 KB
testcase_21 AC 780 ms
86,780 KB
testcase_22 AC 721 ms
87,108 KB
testcase_23 AC 740 ms
87,280 KB
testcase_24 AC 743 ms
87,076 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 519 ms
6,228 KB
testcase_29 AC 553 ms
10,604 KB
testcase_30 AC 581 ms
14,684 KB
testcase_31 AC 588 ms
19,120 KB
testcase_32 AC 613 ms
23,108 KB
testcase_33 AC 627 ms
27,268 KB
testcase_34 AC 632 ms
31,768 KB
testcase_35 AC 647 ms
35,868 KB
testcase_36 AC 654 ms
39,992 KB
testcase_37 AC 661 ms
44,080 KB
testcase_38 AC 661 ms
48,268 KB
testcase_39 AC 676 ms
52,676 KB
testcase_40 AC 684 ms
56,732 KB
testcase_41 AC 693 ms
60,964 KB
testcase_42 AC 705 ms
65,144 KB
testcase_43 AC 729 ms
69,256 KB
testcase_44 AC 724 ms
73,732 KB
testcase_45 AC 715 ms
77,748 KB
testcase_46 AC 737 ms
81,964 KB
testcase_47 AC 746 ms
86,056 KB
testcase_48 AC 403 ms
84,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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() }

// Quick-Find data structure.
// Verified by: https://atcoder.jp/contests/cf17-tournament-round3-open/submissions/22928265
// Verified by: https://atcoder.jp/contests/ttpc2019/submissions/23384553 (polymorphic version)
// Verified by: https://yukicoder.me/submissions/727881 (polymorphic version)
#[derive(Clone)]
struct QuickFind<T = ()> {
    root: Vec<usize>, mem: Vec<Vec<usize>>,
    dat: Vec<T>, default: T,
}

impl QuickFind<()> {
    #[allow(unused)]
    fn new(n: usize) -> Self {
        Self::with_dat(n, ())
    }
    #[allow(unused)]
    fn unite(&mut self, x: usize, y: usize) {
        self.unite_with_hooks(x, y, |&(), _| (), |(), (), _| ());
    }
}
impl<T: Clone> QuickFind<T> {
    fn with_dat(n: usize, def: T) -> Self {
        let root = (0..n).collect();
        let mut mem = vec![vec![]; n];
        for i in 0..n {
            mem[i] = vec![i];
        }
        QuickFind { root: root, mem: mem, dat: vec![def.clone(); n], default: def }
    }
    fn root(&self, x: usize) -> usize {
        self.root[x]
    }
    #[allow(unused)]
    fn set(&mut self, idx: usize, val: T) {
        self.apply(idx, move |me| *me = val);
    }
    #[allow(unused)]
    fn get(&mut self, idx: usize) -> T {
        let mut ans = self.default.clone();
        self.apply(idx, |me| ans = me.clone());
        ans
    }
    fn apply<F: FnOnce(&mut T)>(&mut self, idx: usize, f: F) {
        let r = self.root[idx];
        f(&mut self.dat[r]);
    }
    // unite always merges y to x if |x| >= |y|.
    fn unite_with_hooks<F: FnMut(&T, i64), G: FnMut(T, T, &[usize]) -> T>(
        &mut self, x: usize, y: usize,
        mut hook: F, mut merge: G) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.mem[x].len() < self.mem[y].len() {
            std::mem::swap(&mut x, &mut y);
        }
        let memy = std::mem::replace(&mut self.mem[y], vec![]);
        for &v in &memy {
            self.root[v] = x;
        }
        self.mem[x].extend_from_slice(&memy);
        // hook
        hook(&self.dat[x], -1);
        hook(&self.dat[y], -1);
        self.dat[x] = merge(
            std::mem::replace(&mut self.dat[x], self.default.clone()),
            std::mem::replace(&mut self.dat[y], self.default.clone()),
            &memy,
        );
        hook(&self.dat[x], 1);
    }
    #[allow(unused)]
    fn is_same_set(&self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&self, x: usize) -> usize {
        let x = self.root(x);
        self.mem[x].len()
    }
}

// https://yukicoder.me/problems/no/2294 (3.5)
// 30 個の桁ごとに独立な問題 (重みが 0 か 1) を解くと思うことができる。
// 連結性とある頂点からの重みの和の偶奇は UnionFind か QuickFind でできる。
// -> TLE。QuickFind そのものは複数ある必要はないが、複数持ってしまったため。
fn main() {
    let n: usize = get();
    let mut x: usize = get();
    let q: usize = get();
    let mut qf = QuickFind::with_dat(n, [(1, 0); 30]);
    let mut wei = vec![vec![0; n]; 30];
    for _ in 0..q {
        let ty: i32 = get();
        if ty == 1 {
            let v: usize = get();
            let w: u32 = get();
            qf.unite_with_hooks(x, v, |_, _| (), |a, b, less| {
                let mut res = [(0, 0); 30];
                for i in 0..30 {
                    let w = (w >> i) & 1;
                    if w != wei[i][x] ^ wei[i][v] {
                        for &v in less {
                            wei[i][v] ^= 1;
                        }
                        res[i] = (a[i].0 + b[i].1, a[i].1 + b[i].0);
                    } else {
                        res[i] = (a[i].0 + b[i].0, a[i].1 + b[i].1);
                    }
                }
                res
            });
            continue;
        }
        if ty == 2 {
            let u: usize = get();
            let v: usize = get();
            if qf.root(u) != qf.root(v) {
                println!("-1");
                continue;
            }
            let mut ans = 0u32;
            for i in 0..30 {
                ans |= (wei[i][u] ^ wei[i][v]) << i;
            }
            println!("{}", ans);
            x = (x + ans as usize) % n;
            continue;
        }
        if ty == 3 {
            let v: usize = get();
            let mut ans = 0i64;
            const MOD: i64 = 998_244_353;
            let val = qf.get(v);
            for i in 0..30 {
                let (a, b) = val[i];
                ans += (a as i64 * b as i64) % MOD << i;
                ans %= MOD;
            }
            println!("{}", ans);
            continue;
        }
        let val: usize = get();
        x = (x + val) % n;
    }
}
0