結果

問題 No.2294 Union Path Query (Easy)
ユーザー akakimidoriakakimidori
提出日時 2023-03-14 23:27:14
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 152 ms / 4,000 ms
コード長 5,067 bytes
コンパイル時間 20,242 ms
コンパイル使用メモリ 376,312 KB
実行使用メモリ 57,856 KB
最終ジャッジ日時 2024-11-22 13:26:29
合計ジャッジ時間 23,843 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
  --> src/main.rs:96:6
   |
96 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
  --> src/main.rs:97:6
   |
97 | type Set<T> = BTreeSet<T>;
   |      ^^^

warning: type alias `Deque` is never used
  --> src/main.rs:98:6
   |
98 | type Deque<T> = VecDeque<T>;
   |      ^^^^^

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        assert!(n < std::i32::MAX as usize);
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            x = self.p[x] as usize;
        }
        x
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
    pub fn parent(&self, x: usize) -> Option<usize> {
        assert!(x < self.p.len());
        let p = self.p[x];
        if p >= 0 {
            Some(p as usize)
        } else {
            None
        }
    }
    pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
    where
        F: FnMut(usize),
    {
        while let Some(p) = self.parent(x) {
            f(x);
            x = p;
        }
        x
    }
    pub fn size(&self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let n: usize = sc.next();
    let mut x: usize = sc.next();
    let q: usize = sc.next();
    const MOD: usize = 998_244_353;
    const K: usize = 30;
    let mut dsu = DSU::new(n);
    let mut sum = vec![0; n];// 連結成分内の距離の総和
    let mut cnt = vec![[0; K]; n];// 連結成分内の根からの距離のibit目が1なものの個数
    let mut weight = vec![0; n];// 親への辺の重み
    for _ in 0..q {
        let op: u8 = sc.next();
        if op == 1 {
            let v: usize = sc.next();
            let mut w: usize = sc.next();
            let a = dsu.sum(x, |v| w ^= weight[v]);
            let sa = dsu.size(a);
            let b = dsu.sum(v, |v| w ^= weight[v]);
            let sb = dsu.size(b);
            let (p, c) = dsu.unite(v, x).unwrap();
            let (sp, sc) = if a == p {(sa, sb)} else {(sb, sa)};
            sum[p] = (sum[p] + sum[c]) % MOD;
            weight[c] = w;
            let sum = &mut sum[p];
            let c_cnt = std::mem::take(&mut cnt[c]);
            for (i, (p, c)) in cnt[p].iter_mut().zip(c_cnt.iter()).enumerate() {
                let a = [sp - *p, *p];
                let b = [sc - *c, *c];
                let w = (w >> i & 1) as usize;
                let cnt = (a[0] * b[w ^ 1] + a[1] * b[w]) % MOD;
                *sum = (*sum + cnt * (1 << i)) % MOD;
                *p += b[w ^ 1];
            }
        } else if op == 2 {
            let u: usize = sc.next();
            let v: usize = sc.next();
            if dsu.same(u, v) {
                let mut w = 0;
                dsu.sum(u, |v| w ^= weight[v]);
                dsu.sum(v, |v| w ^= weight[v]);
                x += w;
                writeln!(out, "{}", w).ok();
            } else {
                writeln!(out, "-1").ok();
            }
        } else if op == 3 {
            let v: usize = sc.next();
            writeln!(out, "{}", sum[dsu.root(v)]).ok();
        } else {
            let value: usize = sc.next();
            x += value;
        }
        x %= n;
    }
}
0