結果

問題 No.2294 Union Path Query (Easy)
ユーザー milanis48663220
提出日時 2023-05-05 23:18:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,829 bytes
コンパイル時間 4,151 ms
コンパイル使用メモリ 124,336 KB
最終ジャッジ日時 2025-02-12 19:53:40
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 9 RE * 18 TLE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
using mint = atcoder::modint998244353;
ostream& operator<<(ostream& os, const mint& m){
os << m.val();
return os;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n, x, q; cin >> n >> x >> q;
UnionFind uf(n);
vector<int> xo(n);
auto cnt = vec2d(n, 30, 0);
vector<vector<int>> components(n);
for(int i = 0; i < n; i++) components[i].push_back(i);
while(q--){
int t; cin >> t;
// debug_value(x)
// print_vector(xo);
if(t == 1){
int u, w; cin >> u >> w;
int us = uf.size(u);
int ux = uf.size(x);
if(us > ux) swap(us, ux);
int cur_xor = xo[u]^xo[x];
int diff = cur_xor^w;
int rx = ux > us ? uf.root(x) : uf.root(u);
int ru = ux > us ? uf.root(u) : uf.root(x);
// cout << u << "->" << x << endl;
for(int v: components[ru]){
xo[v] ^= diff;
for(int j = 0; j < 30; j++){
if(xo[v]&(1<<j)){
cnt[rx][j]++;
}
}
components[rx].push_back(v);
}
uf.unionSet(u, x);
int r = uf.root(u);
cnt[r] = cnt[rx];
components[r] = components[rx];
components[us].clear();
}else if(t == 2){
int u, v; cin >> u >> v;
if(uf.findSet(u, v)){
int dist = xo[u]^xo[v];
cout << dist << endl;
x = (x+dist)%n;
}else{
cout << -1 << endl;
}
}else if(t == 3){
int v; cin >> v;
int r = uf.root(v);
mint ans = 0;
int m = uf.size(r);
for(int j = 0; j < 30; j++){
int c1 = cnt[r][j];
int c0 = m-c1;
ans += mint(1<<j)*mint(c0)*mint(c1);
}
cout << ans << endl;
}else{
int v; cin >> v;
x = (x+v)%n;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0