QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431130 | #8133. When Anton Saw This Task He Reacted With 😩 | ucup-team3215 | RE | 0ms | 0kb | C++23 | 4.7kb | 2024-06-05 02:58:44 | 2024-06-05 02:58:44 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 998244353;
constexpr int K = 3;
constexpr int N = 2e5 + 500;
using vec = array<int, K>;
using matrix = array<vec, K>;
constexpr matrix E = {1, 0, 0, 0, 1, 0, 0, 0, 1};
matrix get_matrix(const vec &v, bool mis) {
mis ^= 1;
matrix res{};
res[0][1] = v[2];
res[0][2] = -v[1];
res[1][2] = v[0];
res[1][0] = -v[2];
res[2][0] = v[1];
res[2][1] = -v[0];
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j) {
if (mis)res[i][j] *= -1;
if (res[i][j] < 0)res[i][j] += mod;
}
}
return res;
}
matrix mlp(const matrix &a, const matrix &b) {
matrix res{};
for (int i = 0; i < K; ++i)
for (int k = 0; k < K; ++k)
for (int j = 0; j < K; ++j) {
(res[i][j] += ((ll) a[i][k] * b[k][j]) % mod) %= mod;
}
return res;
}
vec mlp(const vec &a, const matrix &b) {
vec res{};
for (int j = 0; j < K; ++j) {
for (int i = 0; i < K; ++i) {
(res[i] += (ll) a[j] * b[j][i] % mod) %= mod;
}
}
return res;
}
struct s_tr {
int n;
vector<matrix> s;
s_tr(int n) : n(n) {
s.resize(4 * n, E);
}
void set(int v, int l, int r, int k, const matrix &matr) {
if (l + 1 == r) {
s[v] = matr;
return;
}
int m = (r + l) / 2;
if (k < m)set(v * 2 + 1, l, m, k, matr);
else set(v * 2 + 2, m, r, k, matr);
s[v] = mlp(s[v * 2 + 1], s[v * 2 + 2]);
}
matrix get(int v, int l, int r, int lq, int rq) {
if (lq >= rq)return E;
if (l >= rq || lq >= r) { return E; };
if (l >= lq && r <= rq) { return s[v]; }
int m = (r + l) / 2;
return mlp(get(v * 2 + 1, l, m, lq, rq), get(v * 2 + 2, m, r, lq, rq));
}
auto get() {
return get(0, 0, n, 1, n);
}
void set(int k, const matrix &matr) {
set(0, 0, n, k, matr);
}
};
int main() {
cin.tie(0)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<char> minus(n, 0);
vector<char> leaf(n, 1);
vector<array<int, 2>> g(n);
vector<vec> val(n);
vector<int> pred(n, -1);
for (int i = 0; i < n; ++i) {
char t;
cin >> t;
if (t == 'x') {
int l, r;
cin >> l >> r;
--l, --r;
minus[l] = 1;
g[i] = {l, r};
pred[l] = pred[r] = i;
leaf[i] = 0;
} else {
for (auto &x: val[i])cin >> x;
}
}
vector<int> pos(n), where(n), sz(n, 0);
vector<matrix> matr(n, E);
vector<vector<int>> all;
auto dfs = [&](auto &&dfs, int v) -> void {
sz[v] = 1;
if (leaf[v]) {
where[v] = all.size();
all.emplace_back();
all[where[v]].push_back(v);
return;
}
for (auto to: g[v]) {
dfs(dfs, to);
sz[v] += sz[to];
}
int it = -1;
for (auto to: g[v]) {
if (sz[to] > sz[v] / 2)it = to;
}
if (~it) {
where[v] = where[it];
if (sz[g[v][0]] > sz[v] / 2) {
matr[v] = get_matrix(val[g[v][1]], minus[g[v][1]]);
} else {
matr[v] = get_matrix(val[g[v][0]], minus[g[v][0]]);
}
} else {
where[v] = all.size();
all.emplace_back();
}
val[v] = mlp(val[g[v][0]], get_matrix(val[g[v][1]], minus[g[v][1]]));
all[where[v]].push_back(v);
};
dfs(dfs, 0);
vector<s_tr> trees;
for (auto &v: all) {
trees.emplace_back(v.size());
for (int i = 0; i < v.size(); ++i) {
trees.back().set(0, 0, v.size(), i, matr[v[i]]);
}
}
while (q--) {
ll v, x, y, z;
cin >> v >> x >> y >> z;
--v;
x %= mod;
if (x < 0)x += mod;
y %= mod;
if (y < 0)y += mod;
z %= mod;
if (z < 0)z += mod;
val[v] = vec{(int) x, (int) y, (int) z};
while (v) {
int last = all[where[v]].back();
int p = pred[last];
val[last] = mlp(val[all[where[v]].front()], trees[where[v]].get());
matr[p] = get_matrix(val[last], minus[last]);
trees[where[p]].set(pos[p], matr[p]);
val[p] = mlp(val[g[p][0]], get_matrix(val[g[p][1]], minus[g[p][1]]));
v = p;
}
auto re = val[0];
cout << re[0] << " " << re[1] << " " << re[2] << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 3 x 2 3 v 1 0 1 x 4 5 v 0 2 1 v 1 1 1 4 1 2 3 5 0 1 1 4 0 2 2