Submission #76911798
Source Code Expand
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using std::cerr, std::cin, std::cout;
#define vec vector
#if __has_include(<atcoder/all>)
#include <atcoder/all>
// using mint = atcoder::modint998244353;
using mint = atcoder::modint;
std::istream &operator>>(std::istream &is, mint &a) {
long long t;
is >> t;
a = t;
return is;
}
std::ostream &operator<<(std::ostream &os, mint a) { return os << a.val(); }
// vec<mint> operator*(const vec<mint> &a, const vec<mint> &b) { return a.empty() || b.empty() ? vec<mint>() : atcoder::convolution(a, b); }
// vec<mint> &operator*=(vec<mint> &a, const vec<mint> &b) { return a = a * b; }
#endif
typedef long double ld;
#if LONG_MAX == 2147483647L
#undef long
#define long long long
#endif
#define uint unsigned int
#define ulong unsigned long
#define overload3(a, b, c, name, ...) name
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(__i, n)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define per3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define per2(i, n) per3(i, 0, n)
#define per1(n) per2(__i, n)
#define per(...) overload3(__VA_ARGS__, per3, per2, per1)(__VA_ARGS__)
#define all(a) a.begin(), a.end()
#define UNIQUE(a) sort(all(a)), a.erase(unique(all(a)), a.end()), a.shrink_to_fit()
#define sz(a) static_cast<int>(a.size())
#ifndef DEBUG
#define cerr \
if (false) cerr
// #undef assert
// #define assert(...) void(0)
#undef endl
#define endl '\n'
#endif
ostream &operator<<(ostream &os, __int128_t value) {
ostream::sentry s(os);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) --d, *d = '-';
const int len = std::end(buffer) - d;
if (os.rdbuf()->sputn(d, len) != len) os.setstate(std::ios_base::badbit);
}
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
return os << a.first << ' ' << a.second;
};
template <typename T>
ostream &operator<<(ostream &os, vector<T> a) {
const int n = a.size();
rep(i, n) {
os << a[i];
if (i + 1 != n) os << " ";
}
return os;
}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &a) {
return is >> a.first >> a.second;
}
template <typename T, size_t n>
ostream &operator<<(ostream &os, array<T, n> a) {
rep(i, n) {
os << a[i];
if (i + 1 != n) os << " ";
}
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &a) {
for (T &i : a) is >> i;
return is;
}
template <typename T, typename S>
bool chmin(T &x, S y) {
if ((T)y < x) {
x = (T)y;
return true;
}
return false;
}
template <typename T, typename S>
bool chmax(T &x, S y) {
if (x < (T)y) {
x = (T)y;
return true;
}
return false;
}
template <typename T>
void operator++(vector<T> &a) {
for (T &i : a) ++i;
}
template <typename T>
void operator--(vector<T> &a) {
for (T &i : a) --i;
}
template <typename T>
void operator++(vector<T> &a, int) {
for (T &i : a) i++;
}
template <typename T>
void operator--(vector<T> &a, int) {
for (T &i : a) i--;
}
void init(), solve();
int main() {
// srand((unsigned)time(NULL));
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
init();
int t = 1;
// cin >> t;
while (t--) solve();
}
void init() {
}
template <class Abel>
struct UndoUnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
Abel SUM_UNITY;
struct History {
bool merged;
int x, y;
int rank_x;
int par_y;
Abel diff_y;
};
vector<History> hist;
UndoUnionFind(int n = 1, Abel SUM_UNITY = 0) {
init(n, SUM_UNITY);
}
void init(int n = 1, Abel SUM_UNITY_ = 0) {
SUM_UNITY = SUM_UNITY_;
par.resize(n);
rank.resize(n);
diff_weight.resize(n);
hist.clear();
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
diff_weight[i] = SUM_UNITY;
}
}
int root(int x) const {
while (par[x] != x) x = par[x];
return x;
}
Abel weight(int x) const {
Abel res = SUM_UNITY;
while (par[x] != x) {
res += diff_weight[x];
x = par[x];
}
return res;
}
bool issame(int x, int y) const {
return root(x) == root(y);
}
bool merge(int x, int y, Abel w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y) {
hist.push_back({false, -1, -1, -1, -1, SUM_UNITY});
return false;
}
if (rank[x] < rank[y]) {
swap(x, y);
w = -w;
}
hist.push_back({true, x, y, rank[x], par[y], diff_weight[y]});
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) const {
return weight(y) - weight(x);
}
void undo() {
History h = hist.back();
hist.pop_back();
if (!h.merged) return;
rank[h.x] = h.rank_x;
par[h.y] = h.par_y;
diff_weight[h.y] = h.diff_y;
}
};
struct S {
int ty;
int l, r;
mint x;
};
void solve() {
int n, q;
long k;
cin >> n >> k >> q;
mint::set_mod(k);
vec<S> a(q + 1);
a[0] = {-1, -1, -1, -1};
vec<string> ans(q);
vec<vec<int>> g(q + 1);
rep(i, 1, q + 1) {
int ty, b, l, r;
cin >> ty >> b >> l >> r;
l--;
g[b].push_back(i);
mint x = 0;
if (ty == 0) cin >> x;
a[i] = {ty, l, r, x};
}
UndoUnionFind<mint> uf(n + 1);
auto f = [&](auto f, int idx) -> void {
const auto [ty, l, r, x] = a[idx];
bool undo = false;
if (ty == 0) {
if (uf.issame(l, r) && uf.diff(l, r) != x) ans[idx - 1] = "NO";
else {
ans[idx - 1] = "YES";
uf.merge(l, r, x);
undo = true;
}
}
if (ty == 1) {
if (uf.issame(l, r)) ans[idx - 1] = to_string(uf.diff(l, r).val());
else ans[idx - 1] = "UNKNOWN";
}
for (int idxx : g[idx]) f(f, idxx);
if (undo) uf.undo();
};
f(f, 0);
for (string s : ans) cout << s << endl;
}
Submission Info
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
500 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt |
| All |
sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt, in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in46.txt, in47.txt, in48.txt, in49.txt, in50.txt, in51.txt, in52.txt, in53.txt, in54.txt, in55.txt, in56.txt, in57.txt, in58.txt, in59.txt, in60.txt, in61.txt, in62.txt, in63.txt, in64.txt, in65.txt, in66.txt, in67.txt, in68.txt, in69.txt, in70.txt, in71.txt, in72.txt, in73.txt, in74.txt |
| Case Name |
Status |
Exec Time |
Memory |
| in01.txt |
AC |
4 ms |
6276 KiB |
| in02.txt |
AC |
3 ms |
6340 KiB |
| in03.txt |
AC |
2 ms |
6204 KiB |
| in04.txt |
AC |
2 ms |
6284 KiB |
| in05.txt |
AC |
2 ms |
6416 KiB |
| in06.txt |
AC |
2 ms |
6340 KiB |
| in07.txt |
AC |
2 ms |
6416 KiB |
| in08.txt |
AC |
2 ms |
6284 KiB |
| in09.txt |
AC |
3 ms |
6204 KiB |
| in10.txt |
AC |
3 ms |
6252 KiB |
| in11.txt |
AC |
41 ms |
17052 KiB |
| in12.txt |
AC |
39 ms |
17300 KiB |
| in13.txt |
AC |
36 ms |
17052 KiB |
| in14.txt |
AC |
34 ms |
17164 KiB |
| in15.txt |
AC |
30 ms |
16084 KiB |
| in16.txt |
AC |
21 ms |
16044 KiB |
| in17.txt |
AC |
29 ms |
14300 KiB |
| in18.txt |
AC |
30 ms |
16160 KiB |
| in19.txt |
AC |
23 ms |
16092 KiB |
| in20.txt |
AC |
34 ms |
33536 KiB |
| in21.txt |
AC |
25 ms |
15588 KiB |
| in22.txt |
AC |
3 ms |
6380 KiB |
| in23.txt |
AC |
2 ms |
6292 KiB |
| in24.txt |
AC |
3 ms |
6384 KiB |
| in25.txt |
AC |
3 ms |
6284 KiB |
| in26.txt |
AC |
3 ms |
6284 KiB |
| in27.txt |
AC |
3 ms |
6540 KiB |
| in28.txt |
AC |
3 ms |
6340 KiB |
| in29.txt |
AC |
3 ms |
6212 KiB |
| in30.txt |
AC |
2 ms |
6248 KiB |
| in31.txt |
AC |
2 ms |
6392 KiB |
| in32.txt |
AC |
2 ms |
6380 KiB |
| in33.txt |
AC |
2 ms |
6292 KiB |
| in34.txt |
AC |
3 ms |
6256 KiB |
| in35.txt |
AC |
4 ms |
8408 KiB |
| in36.txt |
AC |
3 ms |
6392 KiB |
| in37.txt |
AC |
3 ms |
6540 KiB |
| in38.txt |
AC |
2 ms |
6204 KiB |
| in39.txt |
AC |
3 ms |
6252 KiB |
| in40.txt |
AC |
43 ms |
17112 KiB |
| in41.txt |
AC |
38 ms |
33464 KiB |
| in42.txt |
AC |
36 ms |
16884 KiB |
| in43.txt |
AC |
34 ms |
16744 KiB |
| in44.txt |
AC |
35 ms |
26768 KiB |
| in45.txt |
AC |
37 ms |
25352 KiB |
| in46.txt |
AC |
29 ms |
16100 KiB |
| in47.txt |
AC |
30 ms |
16164 KiB |
| in48.txt |
AC |
28 ms |
20116 KiB |
| in49.txt |
AC |
35 ms |
33456 KiB |
| in50.txt |
AC |
36 ms |
33564 KiB |
| in51.txt |
AC |
36 ms |
33616 KiB |
| in52.txt |
AC |
24 ms |
16048 KiB |
| in53.txt |
AC |
26 ms |
17300 KiB |
| in54.txt |
AC |
3 ms |
6300 KiB |
| in55.txt |
AC |
3 ms |
6540 KiB |
| in56.txt |
AC |
3 ms |
6384 KiB |
| in57.txt |
AC |
3 ms |
6416 KiB |
| in58.txt |
AC |
3 ms |
6320 KiB |
| in59.txt |
AC |
3 ms |
6248 KiB |
| in60.txt |
AC |
3 ms |
6416 KiB |
| in61.txt |
AC |
2 ms |
6340 KiB |
| in62.txt |
AC |
2 ms |
6248 KiB |
| in63.txt |
AC |
3 ms |
6340 KiB |
| in64.txt |
AC |
3 ms |
6384 KiB |
| in65.txt |
AC |
3 ms |
6248 KiB |
| in66.txt |
AC |
3 ms |
6416 KiB |
| in67.txt |
AC |
3 ms |
6204 KiB |
| in68.txt |
AC |
3 ms |
6392 KiB |
| in69.txt |
AC |
3 ms |
6280 KiB |
| in70.txt |
AC |
3 ms |
6252 KiB |
| in71.txt |
AC |
3 ms |
6204 KiB |
| in72.txt |
AC |
3 ms |
6204 KiB |
| in73.txt |
AC |
5 ms |
6392 KiB |
| in74.txt |
AC |
30 ms |
18988 KiB |
| sample01.txt |
AC |
3 ms |
6292 KiB |
| sample02.txt |
AC |
3 ms |
6232 KiB |
| sample03.txt |
AC |
3 ms |
6420 KiB |
| sample04.txt |
AC |
3 ms |
6384 KiB |
| sample05.txt |
AC |
3 ms |
6420 KiB |