結果

問題 No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント
ユーザー 👑 NachiaNachia
提出日時 2021-06-11 21:44:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 4,500 ms
コード長 3,409 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 92,640 KB
実行使用メモリ 23,324 KB
最終ジャッジ日時 2023-08-21 12:00:41
合計ジャッジ時間 6,243 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 78 ms
12,844 KB
testcase_03 AC 25 ms
7,984 KB
testcase_04 AC 73 ms
21,580 KB
testcase_05 AC 8 ms
4,376 KB
testcase_06 AC 58 ms
11,904 KB
testcase_07 AC 64 ms
22,856 KB
testcase_08 AC 81 ms
13,160 KB
testcase_09 AC 58 ms
8,556 KB
testcase_10 AC 52 ms
21,072 KB
testcase_11 AC 61 ms
21,388 KB
testcase_12 AC 40 ms
5,572 KB
testcase_13 AC 38 ms
21,356 KB
testcase_14 AC 60 ms
7,712 KB
testcase_15 AC 40 ms
22,848 KB
testcase_16 AC 65 ms
13,656 KB
testcase_17 AC 82 ms
20,904 KB
testcase_18 AC 68 ms
22,516 KB
testcase_19 AC 31 ms
7,712 KB
testcase_20 AC 48 ms
21,920 KB
testcase_21 AC 53 ms
12,104 KB
testcase_22 AC 104 ms
23,072 KB
testcase_23 AC 104 ms
23,104 KB
testcase_24 AC 104 ms
23,060 KB
testcase_25 AC 105 ms
23,136 KB
testcase_26 AC 106 ms
23,152 KB
testcase_27 AC 105 ms
23,324 KB
testcase_28 AC 105 ms
23,060 KB
testcase_29 AC 106 ms
23,108 KB
testcase_30 AC 105 ms
23,208 KB
testcase_31 AC 106 ms
23,144 KB
testcase_32 AC 81 ms
23,108 KB
testcase_33 AC 82 ms
23,136 KB
testcase_34 AC 80 ms
23,064 KB
testcase_35 AC 81 ms
23,168 KB
testcase_36 AC 79 ms
23,136 KB
testcase_37 AC 119 ms
23,308 KB
testcase_38 AC 77 ms
23,132 KB
testcase_39 AC 79 ms
23,116 KB
testcase_40 AC 78 ms
23,064 KB
testcase_41 AC 78 ms
23,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
0