結果
| 問題 |
No.2295 Union Path Query (Medium)
|
| コンテスト | |
| ユーザー |
SSRS
|
| 提出日時 | 2023-05-05 22:39:59 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,693 bytes |
| コンパイル時間 | 1,834 ms |
| コンパイル使用メモリ | 170,376 KB |
| 実行使用メモリ | 17,152 KB |
| 最終ジャッジ日時 | 2024-11-23 10:05:07 |
| 合計ジャッジ時間 | 212,413 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 TLE * 39 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long MOD = 998244353;
struct unionfind{
vector<int> p, t;
unionfind(int N): p(N, -1), t(N, -1){
}
int root(int x, int q = INF){
if (p[x] < 0 || t[x] > q){
return x;
} else {
return root(p[x], q);
}
}
bool same(int x, int y, int q = INF){
return root(x, q) == root(y, q);
}
int size(int v){
return -p[root(v)];
}
void unite(int x, int y, int w){
x = root(x);
y = root(y);
if (p[x] < p[y]){
swap(p[x], p[y]);
}
p[y] += p[x];
p[x] = y;
t[x] = w;
}
};
int main(){
int N, X, Q;
cin >> N >> X >> Q;
unionfind UF(N);
vector<long long> ans(N, 0);
for (int i = 0; i < Q; i++){
int t;
cin >> t;
if (t == 1){
int v, w;
cin >> v >> w;
int u = X;
u = UF.root(u);
v = UF.root(v);
if (u != v){
long long res = ans[u] + ans[v] + (long long) UF.size(u) * UF.size(v) % MOD * w;
res %= MOD;
UF.unite(u, v, w);
ans[UF.root(u)] = res;
}
}
if (t == 2){
int u, v;
cin >> u >> v;
if (!UF.same(u, v)){
cout << -1 << endl;
} else {
int tv = INF, fv = -1;
while (tv - fv > 1){
int mid = (tv + fv) / 2;
if (UF.same(u, v, mid)){
tv = mid;
} else {
fv = mid;
}
}
cout << tv << endl;
X += tv;
X %= N;
}
}
if (t == 3){
int v;
cin >> v;
cout << ans[UF.root(v)] << endl;
}
if (t == 4){
int value;
cin >> value;
X += value;
X %= N;
}
}
}
SSRS