QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431134 | #8133. When Anton Saw This Task He Reacted With 😩 | ucup-team3215 | WA | 550ms | 64372kb | C++23 | 4.7kb | 2024-06-05 03:05:25 | 2024-06-05 03:05:25 |
Judging History
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 > 0) {
int last = all[where[v]].back();
int p = pred[last];
val[last] = mlp(val[all[where[v]].front()], trees[where[v]].get());
if (~p) {
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: 100
Accepted
time: 0ms
memory: 3572kb
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
output:
998244351 0 2 1 998244351 998244352 0 0 0
result:
ok 9 numbers
Test #2:
score: -100
Wrong Answer
time: 550ms
memory: 64372kb
input:
199999 100000 x 137025 65661 v 572518668 158967010 74946561 x 129836 192657 x 141948 187810 v 574918069 328924434 141474729 x 143312 111002 x 52772 148497 v 922857701 690080961 651915759 v 656198340 28002884 129579416 v 639893144 265359784 646791226 v 796871409 411409966 598676495 v 882562617 224394...
output:
419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 419921572 229228662 540482847 ...
result:
wrong answer 1st numbers differ by non-multiple of MOD, - expected: '393120558', found: '419921572'