結果

問題 No.2294 Union Path Query (Easy)
ユーザー koba-e964koba-e964
提出日時 2023-07-03 11:04:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 832 ms / 4,000 ms
コード長 5,507 bytes
コンパイル時間 3,409 ms
コンパイル使用メモリ 138,324 KB
実行使用メモリ 87,524 KB
最終ジャッジ日時 2023-09-24 08:07:20
合計ジャッジ時間 38,326 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 741 ms
86,284 KB
testcase_04 AC 641 ms
50,220 KB
testcase_05 AC 630 ms
44,268 KB
testcase_06 AC 747 ms
86,260 KB
testcase_07 AC 785 ms
86,448 KB
testcase_08 AC 689 ms
86,020 KB
testcase_09 AC 691 ms
86,016 KB
testcase_10 AC 820 ms
86,276 KB
testcase_11 AC 818 ms
86,272 KB
testcase_12 AC 809 ms
86,228 KB
testcase_13 AC 751 ms
86,024 KB
testcase_14 AC 622 ms
84,544 KB
testcase_15 AC 496 ms
84,852 KB
testcase_16 AC 819 ms
85,380 KB
testcase_17 AC 757 ms
87,524 KB
testcase_18 AC 777 ms
87,424 KB
testcase_19 AC 790 ms
87,012 KB
testcase_20 AC 832 ms
86,620 KB
testcase_21 AC 801 ms
86,824 KB
testcase_22 AC 735 ms
87,024 KB
testcase_23 AC 776 ms
87,240 KB
testcase_24 AC 796 ms
87,024 KB
testcase_25 AC 1 ms
4,508 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 493 ms
6,272 KB
testcase_29 AC 511 ms
10,596 KB
testcase_30 AC 536 ms
14,576 KB
testcase_31 AC 584 ms
19,136 KB
testcase_32 AC 599 ms
23,108 KB
testcase_33 AC 613 ms
27,260 KB
testcase_34 AC 613 ms
31,740 KB
testcase_35 AC 636 ms
35,836 KB
testcase_36 AC 650 ms
39,988 KB
testcase_37 AC 671 ms
44,012 KB
testcase_38 AC 674 ms
48,204 KB
testcase_39 AC 695 ms
52,680 KB
testcase_40 AC 701 ms
56,736 KB
testcase_41 AC 716 ms
60,972 KB
testcase_42 AC 720 ms
65,064 KB
testcase_43 AC 759 ms
69,212 KB
testcase_44 AC 760 ms
73,736 KB
testcase_45 AC 785 ms
77,732 KB
testcase_46 AC 752 ms
81,868 KB
testcase_47 AC 773 ms
86,060 KB
testcase_48 AC 424 ms
84,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 そのものは複数ある必要はないが、複数持ってしまったため。
// QuickFind を 1 つにしてデータの方を 30 並列にすることで AC。
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