QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#473203 | #8133. When Anton Saw This Task He Reacted With 😩 | ucup-team1198# | AC ✓ | 742ms | 86472kb | C++20 | 6.7kb | 2024-07-11 23:03:23 | 2024-07-11 23:03:24 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int MOD = 998244353;
const int MAXN = 200'200;
vector<int> G[MAXN];
int sz[MAXN];
int par[MAXN];
int path_id[MAXN];
int path_ind[MAXN];
vector<int> paths[MAXN];
int tin[MAXN];
int tout[MAXN];
vector<int> order;
const int K = 3;
long long values[MAXN][K];
void dfs_sz(int v) {
sz[v] = 1;
for (int u : G[v]) {
dfs_sz(u);
sz[v] += sz[u];
}
}
void dfs_paths(int v) {
tin[v] = order.size();
order.emplace_back(v);
if (G[v].empty()) {
path_id[v] = v;
} else {
int L = G[v][0], R = G[v][1];
if (sz[L] >= sz[R]) {
dfs_paths(L);
dfs_paths(R);
path_id[v] = path_id[L];
} else {
dfs_paths(R);
dfs_paths(L);
path_id[v] = path_id[R];
}
values[v][0] = (values[L][1] * values[R][2] - values[L][2] * values[R][1]) % MOD;
values[v][1] = (values[L][2] * values[R][0] - values[L][0] * values[R][2]) % MOD;
values[v][2] = (values[L][0] * values[R][1] - values[L][1] * values[R][0]) % MOD;
}
path_ind[v] = paths[path_id[v]].size();
paths[path_id[v]].emplace_back(v);
tout[v] = order.size();
}
void dumbfs(int v) {
if (G[v].empty()) {
} else {
int L = G[v][0], R = G[v][1];
dumbfs(L);
dumbfs(R);
values[v][0] = (values[L][1] * values[R][2] - values[L][2] * values[R][1]) % MOD;
values[v][1] = (values[L][2] * values[R][0] - values[L][0] * values[R][2]) % MOD;
values[v][2] = (values[L][0] * values[R][1] - values[L][1] * values[R][0]) % MOD;
}
}
struct Matrix {
long long vals[K][K];
Matrix() {
clear();
}
void clear() {
for (int i = 0; i < K; ++i)
fill(vals[i], vals[i] + K, 0);
}
};
void multiply(const Matrix& left, const Matrix& right, Matrix& ans) {
ans.clear();
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j) {
for (int l = 0; l < K; ++l)
ans.vals[i][l] += left.vals[i][j] * right.vals[j][l];
}
}
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j)
ans.vals[i][j] %= MOD;
}
}
Matrix tree[(1 << 19) + 228];
void build(int v, int left, int right, vector<Matrix>& A) {
if (left + 1 == right) {
tree[v] = A[left];
return;
}
int mid = (left + right) / 2;
build(2 * v + 1, left, mid, A);
build(2 * v + 2, mid, right, A);
multiply(tree[2 * v + 1], tree[2 * v + 2], tree[v]);
}
void upd(int v, int left, int right, int i, const Matrix& val) {
if (left + 1 == right) {
tree[v] = val;
return;
}
int mid = (left + right) / 2;
if (i < mid)
upd(2 * v + 1, left, mid, i, val);
else
upd(2 * v + 2, mid, right, i, val);
multiply(tree[2 * v + 1], tree[2 * v + 2], tree[v]);
}
long long tmp_vals[K];
long long extra_tmp_vals[K];
void mul_ans(int v, int left, int right, int x, int y) {
if (y <= left || right <= x)
return;
if (x <= left && right <= y) {
for (int i = 0; i < K; ++i) {
extra_tmp_vals[i] = 0;
for (int j = 0; j < K; ++j)
extra_tmp_vals[i] += tree[v].vals[i][j] * tmp_vals[j];
extra_tmp_vals[i] %= MOD;
}
for (int i = 0; i < K; ++i)
tmp_vals[i] = extra_tmp_vals[i];
return;
}
int mid = (left + right) / 2;
mul_ans(2 * v + 2, mid, right, x, y);
mul_ans(2 * v + 1, left, mid, x, y);
}
Matrix tmp_mat;
void make_left(long long v[K]) {
tmp_mat.clear();
tmp_mat.vals[0][1] = -v[2];
tmp_mat.vals[0][2] = v[1];
tmp_mat.vals[1][0] = v[2];
tmp_mat.vals[1][2] = -v[0];
tmp_mat.vals[2][0] = -v[1];
tmp_mat.vals[2][1] = v[0];
}
void make_right(long long v[K]) {
tmp_mat.clear();
tmp_mat.vals[0][1] = v[2];
tmp_mat.vals[0][2] = -v[1];
tmp_mat.vals[1][0] = -v[2];
tmp_mat.vals[1][2] = v[0];
tmp_mat.vals[2][0] = v[1];
tmp_mat.vals[2][1] = -v[0];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
par[0] = -1;
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
if (c == 'x') {
int l, r;
cin >> l >> r;
--l;
--r;
G[i].emplace_back(l);
G[i].emplace_back(r);
par[l] = i;
par[r] = i;
} else {
cin >> values[i][0] >> values[i][1] >> values[i][2];
}
}
dfs_sz(0);
dfs_paths(0);
vector<Matrix> start_vals(n);
Matrix E;
for (int i = 0; i < K; ++i)
E.vals[i][i] = 1;
for (int i = 0; i < n; ++i) {
int v = order[i];
if (G[v].empty()) {
start_vals[i] = E;
} else {
int L = G[v][0], R = G[v][1];
if (sz[L] >= sz[R]) {
make_right(values[R]);
} else {
make_left(values[L]);
}
start_vals[i] = tmp_mat;
}
}
build(0, 0, n, start_vals);
vector<int> leaves;
for (int i = 0; i < n; ++i) {
if (G[i].empty())
leaves.emplace_back(i);
}
while (q--) {
int v;
cin >> v;
--v;
cin >> values[v][0] >> values[v][1] >> values[v][2];
/*v = leaves[rand() % leaves.size()];
values[v][0] = rand() % 10;
values[v][1] = rand() % 10;
values[v][2] = rand() % 10;
cerr << v + 1 << ' ' << values[v][0] << ' ' << values[v][1] << ' ' << values[v][2] << '\n';*/
while (v != -1) {
int id = path_id[v];
int u = paths[id].back();
// start of the path
int fin = paths[id].front();
for (int i = 0; i < K; ++i)
tmp_vals[i] = values[fin][i];
mul_ans(0, 0, n, tin[u], tin[fin]);
for (int i = 0; i < K; ++i)
values[u][i] = tmp_vals[i];
int p = par[u];
if (p != -1) {
int L = G[p][0], R = G[p][1];
if (u == L) {
make_left(values[u]);
} else {
make_right(values[u]);
}
upd(0, 0, n, tin[p], tmp_mat);
}
v = p;
}
/*int cur_values[K];
for (int i = 0; i < K; ++i)
cur_values[i] = values[0][i];
dumbfs(0);
for (int i = 0; i < K; ++i) {
if ((cur_values[i] - values[0][i]) % MOD != 0) {
cerr << "BUG!!\n";
cerr << "got: ";
for (int j = 0; j < K; ++j)
cerr << cur_values[j] << ' ';
cerr << '\n';
cerr << "need ";
for (int j = 0; j < K; ++j)
cerr << values[0][j] << ' ';
cerr << '\n';
}
assert(cur_values[i] == values[0][i]);
}*/
for (int i = 0; i < K; ++i)
cout << values[0][i] << ' ';
cout << '\n';
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 45992kb
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:
-2 0 2 1 -2 -1 0 0 0
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 742ms
memory: 80940kb
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:
393120558 -224477738 -610947005 759959566 981774500 128012497 -703632542 -18232745 533642029 -593864779 745296852 -944750793 -593678852 828869760 -920223197 592494858 647751304 -116816620 190018467 515243135 518180555 628180500 -488259799 257430135 -984507108 352087791 917410487 932051309 3...
result:
ok 300000 numbers
Test #3:
score: 0
Accepted
time: 633ms
memory: 81224kb
input:
199999 100000 x 154525 80092 v 450407262 725458410 590777520 x 24720 135901 v 719242117 114943104 186796011 v 382530926 89156744 943939337 x 183376 26042 x 109984 157873 x 151637 150600 x 4115 27454 x 163135 92648 x 16764 33212 v 849210403 945083972 689295397 v 471196117 68587428 225597765 x 24643 5...
output:
-321176892 -1730057 -549077502 810403092 -740047511 -144784620 410756156 -744895835 -85579882 -671109463 -478998570 922528759 317367558 536888537 506214109 -513490823 879045782 -225839405 422920052 152084658 517340457 -997036451 348787162 -677423276 776293878 699474926 -287129823 -126385880...
result:
ok 300000 numbers
Test #4:
score: 0
Accepted
time: 487ms
memory: 81048kb
input:
199999 100000 x 72889 193806 x 35339 33069 v 314802407 406980523 492377265 x 108307 60027 x 144922 140917 v 328481079 117663280 644171354 v 482028404 951751561 166221217 v 936461869 436114879 421819757 x 152919 99965 x 61168 150607 v 56403640 743462679 134896557 v 24809217 462947115 966139991 v 7828...
output:
23709876 -617795986 -369084686 760678420 539369190 611778104 -883317666 653692915 -58366939 674199470 304745735 -454120550 953800112 186017361 -949043816 327282782 871001677 -704263640 588783157 -496113704 190564297 102680906 -4355337 963478755 510012481 -892827456 281770975 811082806 36713...
result:
ok 300000 numbers
Test #5:
score: 0
Accepted
time: 464ms
memory: 81108kb
input:
199999 100000 x 134204 79203 v 152855933 152545887 271660214 v 393332175 182708769 115884220 v 731792305 965028257 676889584 x 89631 14495 x 142016 85686 v 600051847 172947969 906920949 v 846126215 214556203 657929902 x 125721 133526 x 93179 35713 v 330716449 450417250 611411649 v 114397688 58644961...
output:
139597616 375474977 -983624560 -108915893 79727434 -634540722 397351102 -120282751 429046190 588368345 -178818454 -496095614 -477665442 186408072 484373545 997888597 816534316 338411279 -664078084 288211584 608252640 509280845 362972392 286415695 -634847393 878881251 3658455 -286973481 9481...
result:
ok 300000 numbers
Test #6:
score: 0
Accepted
time: 396ms
memory: 81644kb
input:
199999 100000 x 29842 60343 x 22382 27649 v 148997533 411153785 529195552 v 831453378 856711025 439562917 x 183061 152107 v 208562249 845550020 248974502 x 8708 152913 v 901332053 480035531 424365358 v 120556946 620074725 884675784 v 493886564 455460926 851190410 x 63346 64739 x 35246 36255 v 762936...
output:
-761447031 -808025939 70559261 -336478455 -731887881 -516614332 410967670 613729303 804008156 92638320 37926778 82924205 357869883 -765477642 579608532 -306542271 124868602 -810877141 237610689 -389754505 -417139943 848616732 -90371214 -138436462 614624615 -543569509 673629667 485784731 743...
result:
ok 300000 numbers
Test #7:
score: 0
Accepted
time: 368ms
memory: 81776kb
input:
199999 100000 x 179471 175117 x 189060 178495 x 20142 58065 x 22916 150184 v 704047412 186112247 660817663 v 761554808 199099716 794321264 v 362925435 508140595 251556541 v 65674025 717152823 157775106 v 325965317 977108704 50644678 v 566946741 833186394 771714200 v 996708965 76780827 625429369 v 85...
output:
-632986028 -892414579 -385846523 -266735298 -421343908 663777200 -444725676 415454275 7683807 -530113104 -421018422 513594285 -782654117 -137098079 812820392 669985796 -768757519 564691763 929231866 520228049 -223634605 29950289 -428877962 -277172238 644573107 -484529715 554458153 728007201...
result:
ok 300000 numbers
Test #8:
score: 0
Accepted
time: 385ms
memory: 82276kb
input:
199999 100000 x 73506 171332 x 135330 187765 v 308206068 679940956 278078069 v 442448744 196158033 738117032 x 194709 115786 v 208526122 936976225 340056181 x 42663 43401 x 55484 199464 v 865443128 131903961 74265613 x 44659 44773 x 32199 18455 v 986118756 284329619 244212114 v 793747964 649179736 4...
output:
429717039 -129935757 -823225834 -31998235 -465792513 -225112347 457086098 631788280 989689243 550574851 6706768 -581628454 -713103269 505326489 916518702 457465389 653530244 951605771 -384032521 767828057 44273794 698196640 494937773 99337798 718503234 422078037 151379051 20520347 707143833...
result:
ok 300000 numbers
Test #9:
score: 0
Accepted
time: 391ms
memory: 82632kb
input:
199999 100000 x 109220 170287 v 563361501 367416904 98790213 x 31431 96958 x 99594 159052 x 95382 129615 v 61965807 547448247 405891792 v 443530416 578856323 588763197 v 761021716 795533831 212530056 v 370964907 391812631 255457982 x 49713 153066 x 141543 111694 v 144135957 614962153 284136518 x 416...
output:
433293962 -661330106 -250875550 -6126833 9180464 -838628109 -514418394 -501508520 -33736634 912495370 737285784 595438897 467123496 44306423 562070292 903488238 -955272710 -936828694 269853145 336741491 -433105475 926999098 -863372670 277614816 644727031 -521919528 69621281 984868613 -88565...
result:
ok 300000 numbers
Test #10:
score: 0
Accepted
time: 3ms
memory: 44740kb
input:
3 1 x 2 3 v 998244352 998244352 998244352 v 0 0 0 3 1 2 0
output:
-998244351 998244352 998244352
result:
ok 3 number(s): "2 998244352 998244352"
Test #11:
score: 0
Accepted
time: 392ms
memory: 83344kb
input:
199999 100000 x 199465 1690 x 70268 106693 v 194793703 729830314 457557419 x 64673 6910 v 755452906 141328541 558160677 v 725017524 158685295 201414156 x 161801 27226 x 181414 47025 v 387724146 819109666 514628998 v 252532326 97757436 828777580 v 200868967 692540096 706977766 v 300419109 2053530 824...
output:
627210517 640945891 -597759713 305641486 -105185528 -898351186 735729088 -192648820 283037791 -621173639 357962902 336785549 835938680 -363549622 -975855419 -504547421 -385691560 -481299119 -34353998 -480713478 -950021127 -782926273 -255660608 -618453331 135074745 -27793541 -76420073 865723...
result:
ok 300000 numbers
Test #12:
score: 0
Accepted
time: 327ms
memory: 86472kb
input:
199999 100000 x 37758 141919 v 148792593 369372129 595139892 x 59335 149367 v 452667329 904801829 628919068 v 160106559 532238331 179544300 v 850489754 705167899 265598880 x 120963 167491 x 92157 46815 v 444945978 987276260 843838004 x 189040 28027 v 889755401 760730228 3237333 x 168907 82672 v 2329...
output:
897185498 177437016 -344597551 -949384144 883514812 -233545577 -493156041 -412281905 546090395 -83998326 -457300186 -315254628 965835151 -194537930 -695946246 452996535 714783487 -36392156 -115526544 425959754 -111853311 203667304 -543580851 78105722 -486048218 727218227 -580039826 -7233095...
result:
ok 300000 numbers
Extra Test:
score: 0
Extra Test Passed