QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#324397 | #8133. When Anton Saw This Task He Reacted With 😩 | ucup-team055 | WA | 1486ms | 57300kb | C++20 | 5.4kb | 2024-02-10 18:15:06 | 2024-02-10 18:15:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define all(p) p.begin(),p.end()
using ll =long long;
// バグっとる
constexpr ll mod = 998244353;
struct mm {
ll x;
mm(ll x_ = 0): x(x_ % mod) {
if(x < 0) x += mod;
}
friend mm operator+(mm a, mm b) { return a.x + b.x; }
friend mm operator-(mm a, mm b) { return a.x - b.x; }
friend mm operator*(mm a, mm b) { return a.x * b.x; }
friend mm operator/(mm a, mm b) { return a.x * b.inv(); }
friend void operator+=(mm& a, mm b) { a = a.x + b.x; }
friend void operator-=(mm& a, mm b) { a = a.x - b.x; }
friend void operator*=(mm& a, mm b) { a = a.x * b.x; }
friend void operator/=(mm& a, mm b) { a = a.x * b.inv(); }
mm inv() const { return pow(*this, mod - 2); }
friend mm pow(mm a, ll b) {
mm c = 1;
while(b) {
if(b & 1) c *= a;
a *= a;
b >>= 1;
}
return c;
}
};
template<class T,T (*op)(T,T),T (*e)()>
struct segtree{
int size=1,n_;
vector<T> d;
segtree(int n){
n_=n;
while(size<n) size*=2;
d.resize(size*2,e());
}
void set(int ind,T v){
assert(0<=ind&&ind<n_);
ind+=size;
d[ind]=v;
while(ind!=1){
ind/=2;
d[ind]=op(d[ind*2],d[ind*2+1]);
}
}
void get(int ind){
return d[ind+size];
}
T prod(int l,int r){
assert(0<=l&&l<=r&&r<n_);
l+=size,r+=size;
T lres=e(),rres=e();
while(r-l>0){
if(l&1){
lres=op(lres,d[l--]);
}
if(r&1){
rres=op(d[--r],rres);
}
l/=2;r/=2;
}
return op(lres,rres);
}
};
using mint = mm;
using F1=array<mint,3>;
using F2=array<array<mint,3>,3>;
F1 cross(F1 l,F1 r){
F1 res;
res[0]=l[1]*r[2]-l[2]*r[1];
res[1]=l[2]*r[0]-l[0]*r[2];
res[2]=l[0]*r[1]-l[1]*r[0];
return res;
}
F2 op(F2 l,F2 r){
F2 res;
rep(i,0,3) rep(j,0,3) res[i][j]=0;
rep(i,0,3) rep(j,0,3) rep(k,0,3) res[i][k]+=l[i][j]*r[j][k];
return res;
}
F2 e(){
F2 base;
rep(i,0,3) rep(j,0,3) base[i][j]=(i==j?1:0);
return base;
}
F2 to_mat(F1 l){
F2 res;
res[0]={0,l[2]*-1,l[1]};
res[1]={l[2],0,l[0]*-1};
res[2]={l[1]*-1,l[0],0};
return res;
}
F1 mul21(F2 l,F1 r){
F1 res;
rep(i,0,3) res[i]=0;
rep(i,0,3) rep(j,0,3) res[i]+=l[i][j]*r[j];
return res;
}
void solve(){
int N,Q;
cin>>N>>Q;
vector<vector<int>> G(N);
vector<F1> A(N);
vector<int> pare(N,-1),to(N,-1);
rep(i,0,N){
char c;
cin>>c;
if(c=='v'){
rep(j,0,3){
ll a;
cin>>a;
A[i][j]=a;
}
}
else{
int a,b;
cin>>a>>b;
a--,b--;
G[i]={a,b};
pare[a]=i;pare[b]=i;
}
}
mint sign=1;
vector<int> order,inv(N),wei(N),bot(N);
auto dfs1=[&](auto self,int var) -> void {
if(G[var].empty()) wei[var]++;
else{
self(self,G[var][0]);
self(self,G[var][1]);
wei[var]=1+wei[G[var][0]]+wei[G[var][1]];
if(wei[G[var][0]]>wei[G[var][1]]){
sign*=-1;
swap(G[var][0],G[var][1]);
}
}
};
dfs1(dfs1,0);
auto dfs2=[&](auto self,int var) -> void {
if(to[var]==-1) to[var]=var;
inv[var]=order.size();
order.push_back(var);
if(!G[var].empty()){
to[G[var][1]]=to[var];
self(self,G[var][1]);
self(self,G[var][0]);
A[var]=cross(A[G[var][0]],A[G[var][1]]);
bot[var]=bot[G[var][1]];
}
else{
bot[var]=var;
}
};
auto out=[&](F1 a){
cout<<a[0].x<<" "<<a[1].x<<" "<<a[2].x<<"\n";
};
dfs2(dfs2,0);
segtree<F2,op,e> seg(N);
rep(i,0,N){
if(!G[i].empty()){
seg.set(inv[i],to_mat(A[G[i][0]]));
}
}
auto calc=[&](int var) -> F1 {
if(G[var].empty()) return A[var];
F1 tmp;
tmp=mul21(seg.prod(inv[var]+1,inv[bot[var]]),A[bot[var]]);
return cross(A[G[var][0]],tmp);
};
auto upd=[&](int var,F1 a) -> F1 {
A[var]=a;
while(var>=0){
if(to[var]!=var){
a=mul21(seg.prod(inv[to[var]],inv[var]),a);
}
if(var==0){
return a;
}
else{
//out(a);
var=to[var];
A[var]=a;
if(var==0) return a;
seg.set(inv[pare[var]],to_mat(a));
var=pare[var];
a=calc(var);
}
}
return a;
};
while(Q--){
int ind;
cin>>ind;
ind--;
F1 a;
rep(i,0,3){
ll b;
cin>>b;
a[i]=b;
}
auto ans=upd(ind,a);
//ans=calc(0);
rep(i,0,3) ans[i]*=sign;
out(ans);
}
}
int main(){
int T=1;
//cin>>T;
while(T--) solve();
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3528kb
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: 1486ms
memory: 57300kb
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:
96904538 715849384 885556407 432723789 106144399 154859103 352281538 514505606 158416483 768261382 911346146 802181970 150820938 21437821 528041415 374665152 395459337 677111684 226933421 147297192 867780674 831250824 795942907 84410569 167602028 264074274 895851913 579500829 946109087 350349665 150...
result:
wrong answer 1st numbers differ by non-multiple of MOD, - expected: '393120558', found: '96904538'