結果
| 問題 |
No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-06-11 21:44:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 4,500 ms |
| コード長 | 3,409 bytes |
| コンパイル時間 | 1,136 ms |
| コンパイル使用メモリ | 106,196 KB |
| 最終ジャッジ日時 | 2025-01-22 05:40:38 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 42 |
ソースコード
#include <iostream>
#include <vector>
#include <atcoder/modint>
#include <atcoder/segtree>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
using mll = atcoder::static_modint<998244353>;
template<
class S,
S(*op)(S a, S b),
S(*e)(),
class F,
S(*mapping)(F a, S b),
F(*composition)(F a, F b),
F(*id)()
>
struct lazy_segtree{
private:
struct Node{ S v; F r; };
int N,logN;
vector<Node> V;
void mapF(int i,F f){
V[i].v = mapping(f,V[i].v);
V[i].r = composition(f,V[i].r);
}
void merge(int i){
V[i].v = mapping(V[i].r,op(V[i*2].v,V[i*2+1].v));
}
void spread(int i){
mapF(i*2,V[i].r); mapF(i*2+1,V[i].r);
V[i].r=id();
}
public:
lazy_segtree(int n){
N=1; logN=0; while(N<n){ N*=2; logN++; }
V.assign(N*2,{ e(),id() });
}
lazy_segtree(vector<S> A) : lazy_segtree(A.size()){
rep(i,A.size()) V[N+i].v=A[i];
for(int i=N-1; i>=1; i--) merge(i);
}
void set(int p,S v){
p+=N;
for(int d=logN; d>=1; d--) spread(p>>d);
V[p].v=v;
for(int d=1; d<=logN; d++) merge(p>>d);
}
S get(int p){
p+=N;
for(int d=logN; d>=1; d--) spread(p>>d);
return V[p].v;
}
S prod(int l,int r){
l+=N; r+=N;
for(int d=logN; d>=1; d--){ spread(l>>d); spread((r-1)>>d); }
S ql=e(), qr=e();
while(l<r){
if(l&1) ql=op(ql,V[l++].v);
if(r&1) qr=op(V[--r].v,qr);
l/=2; r/=2;
}
return op(ql,qr);
}
void apply(int p,F f){
p+=N;
for(int d=logN; d>=1; d--) spread(p>>d);
mapF(p,f);
for(int d=1; d<=logN; d++) merge(p>>d);
}
void apply(int l,int r,F f){
l+=N; r+=N;
for(int d=logN; d>=1; d--){ spread(l>>d); spread((r-1)>>d); }
int lp=l, rp=r;
while(l<r){
if(l&1) mapF(l++,f);
if(r&1) mapF(--r,f);
l/=2; r/=2;
}
for(int d=1; d<=logN; d++){ merge(lp>>d); merge((rp-1)>>d); }
}
};
struct S {
mll G[5];
};
S op(S l, S r) {
S res;
rep(i,5) res.G[i] = l.G[i] + r.G[i];
return res;
}
S e() {
return { {0,0,0,0,0} };
}
struct F { int upd; };
S mapping(F f, S a) {
if(f.upd != -1){
rep(t,4) a.G[t+1] = a.G[t] * f.upd;
}
return a;
}
F composition(F f, F g) { return (f.upd == -1) ? g : f; }
F id() { return { -1 }; }
using RQ = lazy_segtree<S, op, e, F, mapping, composition, id>;
int N;
vector<S> A;
RQ G(0);
int Q;
int main(){
cin >> N;
A.resize(N);
rep(i,N){
int a; cin >> a;
A[i].G[0] = 1;
rep(t,4) A[i].G[t+1] = A[i].G[t] * a;
}
G = RQ(A);
cin >> Q;
rep(q,Q){
int t,u,v,w; cin >> t >> u >> v >> w;
u--; v--; w--;
if(u > v) swap(u,v);
int l1, r1, l2, r2;
if(u <= w && w <= v){ l1=u; r1=v+1; l2=0; r2=0; }
else{ l1=v; r1=N; l2=0; r2=u+1; }
if(t == 0){
int b; cin >> b;
G.apply(l1,r1,{b}); G.apply(l2,r2,{b});
}
else{
S x = op( G.prod(l1,r1) , G.prod(l2,r2) );
vector<mll> coe;
mll M = x.G[1] * x.G[0].inv();
if(t == 1) coe = {-M,1,0,0,0};
if(t == 2) coe = {M*M,-M*2,1,0,0};
if(t == 3) coe = {-M*M*M,M*M*3,-M*3,1,0};
if(t == 4) coe = {M*M*M*M,-M*M*M*4,M*M*6,-M*4,1};
mll ans = 0;
rep(t,5) ans += x.G[t] * coe[t];
ans /= x.G[0];
cout << ans.val() << "\n";
}
}
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
} ios_do_not_sync_inst;
Nachia