QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#324385 | #8133. When Anton Saw This Task He Reacted With 😩 | ucup-team055 | WA | 0ms | 3516kb | C++20 | 5.4kb | 2024-02-10 18:09:13 | 2024-02-10 18:09:13 |
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) -> void {
A[var]=a;
while(var>=0){
if(to[var]!=var){
a=mul21(seg.prod(inv[to[var]],inv[var]),a);
}
if(var==0){
return;
}
else{
var=to[var];
A[var]=a;
if(var==0) return;
seg.set(inv[pare[var]],to_mat(a));
var=pare[var];
a=calc(var);
}
}
return;
};
while(Q--){
int ind;
cin>>ind;
ind--;
F1 a;
rep(i,0,3){
ll b;
cin>>b;
a[i]=b;
}
upd(ind,a);
auto 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
*/
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3516kb
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:
8 0 998244345 998244349 8 4 0 0 0
result:
wrong answer 1st numbers differ by non-multiple of MOD, - expected: '998244351', found: '8'