結果

問題 No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント
ユーザー SSRSSSRS
提出日時 2021-06-11 22:25:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 4,500 ms
コード長 4,014 bytes
コンパイル時間 1,706 ms
コンパイル使用メモリ 177,992 KB
実行使用メモリ 31,156 KB
最終ジャッジ日時 2023-08-21 13:01:59
合計ジャッジ時間 10,170 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 150 ms
16,772 KB
testcase_03 AC 49 ms
10,108 KB
testcase_04 AC 145 ms
30,092 KB
testcase_05 AC 15 ms
4,880 KB
testcase_06 AC 109 ms
16,516 KB
testcase_07 AC 137 ms
30,576 KB
testcase_08 AC 153 ms
17,036 KB
testcase_09 AC 109 ms
10,216 KB
testcase_10 AC 107 ms
29,784 KB
testcase_11 AC 125 ms
30,096 KB
testcase_12 AC 74 ms
6,764 KB
testcase_13 AC 84 ms
30,132 KB
testcase_14 AC 113 ms
9,848 KB
testcase_15 AC 95 ms
30,580 KB
testcase_16 AC 126 ms
17,136 KB
testcase_17 AC 156 ms
29,784 KB
testcase_18 AC 138 ms
30,464 KB
testcase_19 AC 62 ms
10,120 KB
testcase_20 AC 103 ms
30,092 KB
testcase_21 AC 100 ms
16,552 KB
testcase_22 AC 203 ms
30,572 KB
testcase_23 AC 201 ms
30,920 KB
testcase_24 AC 204 ms
30,724 KB
testcase_25 AC 206 ms
30,628 KB
testcase_26 AC 206 ms
30,924 KB
testcase_27 AC 205 ms
30,572 KB
testcase_28 AC 205 ms
30,572 KB
testcase_29 AC 205 ms
30,580 KB
testcase_30 AC 205 ms
30,728 KB
testcase_31 AC 206 ms
30,676 KB
testcase_32 AC 194 ms
30,984 KB
testcase_33 AC 194 ms
30,672 KB
testcase_34 AC 193 ms
31,156 KB
testcase_35 AC 194 ms
30,988 KB
testcase_36 AC 196 ms
30,620 KB
testcase_37 AC 167 ms
30,984 KB
testcase_38 AC 194 ms
31,008 KB
testcase_39 AC 197 ms
30,620 KB
testcase_40 AC 196 ms
30,988 KB
testcase_41 AC 196 ms
30,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long modpow(long long a, long long b){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
long long modinv(long long a){
  return modpow(a, MOD - 2);
}
struct lazy_segment_tree{
  int N;
  vector<array<long long, 5>> ST;
  vector<long long> lazy;
  lazy_segment_tree(vector<long long> A){
    int N2 = A.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<array<long long, 5>>(N * 2 - 1);
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i][0] = 1;
      for (int j = 0; j < 4; j++){
        ST[N - 1 + i][j + 1] = ST[N - 1 + i][j] * A[i] % MOD;
      }
    }
    for (int i = N2; i < N; i++){
      for (int j = 0; j < 5; j++){
        ST[N - 1 + i][j] = 0;
      }
    }
    for (int i = N - 2; i >= 0; i--){
      for (int j = 0; j < 5; j++){
        ST[i][j] = (ST[i * 2 + 1][j] + ST[i * 2 + 2][j]) % MOD;
      }
    }
    lazy = vector<long long>(N * 2 - 1, -1);
  }
  void eval(int i){
    if (lazy[i] != -1){
      if (i < N - 1){
        lazy[i * 2 + 1] = lazy[i];
        lazy[i * 2 + 2] = lazy[i];
      }
      for (int j = 0; j < 4; j++){
        ST[i][j + 1] = ST[i][j] * lazy[i] % MOD;
      }
      lazy[i] = -1;
    }
  }
  void range_update(int L, int R, long long x, int i, int l, int r){
    eval(i);
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      lazy[i] = x;
      eval(i);
    } else {
      int m = (l + r) / 2;
      range_update(L, R, x, i * 2 + 1, l, m);
      range_update(L, R, x, i * 2 + 2, m, r);
      for (int j = 1; j < 5; j++){
        ST[i][j] = (ST[i * 2 + 1][j] + ST[i * 2 + 2][j]) % MOD;
      }
    }
  }
  void range_update(int L, int R, long long x){
    range_update(L, R, x, 0, 0, N);
  }
  array<long long, 5> range_sum(int L, int R, int i, int l, int r){
    eval(i);
    if (r <= L || R <= l){
      array<long long, 5> ans;
      for (int i = 0; i < 5; i++){
        ans[i] = 0;
      }
      return ans;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      array<long long, 5> A = range_sum(L, R, i * 2 + 1, l, m);
      array<long long, 5> B = range_sum(L, R, i * 2 + 2, m, r);
      array<long long, 5> ans;
      for (int j = 0; j < 5; j++){
        ans[j] = (A[j] + B[j]) % MOD;
      }
      return ans;
    }
  }
  array<long long, 5> range_sum(int L, int R){
    return range_sum(L, R, 0, 0, N);
  }
};
int main(){
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  lazy_segment_tree ST(A);
  vector<vector<long long>> binom(5, vector<long long>(5, 1));
  for (int i = 2; i < 5; i++){
    for (int j = 1; j < i; j++){
      binom[i][j] = binom[i - 1][j - 1] + binom[i - 1][j];
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 0){
      int U, V, W, B;
      cin >> U >> V >> W >> B;
      U--;
      V--;
      W--;
      if (U > V){
        swap(U, V);
      }
      if (U < W && W < V){
        ST.range_update(U, V + 1, B);
      } else {
        ST.range_update(0, U + 1, B);
        ST.range_update(V, N, B);
      }
    }
    if (t >= 1){
      int U, V, W;
      cin >> U >> V >> W;
      U--;
      V--;
      W--;
      if (U > V){
        swap(U, V);
      }
      array<long long, 5> S;
      if (U < W && W < V){
        S = ST.range_sum(U, V + 1);
      } else {
        array<long long, 5> L = ST.range_sum(0, U + 1);
        array<long long, 5> R = ST.range_sum(V, N);
        for (int j = 0; j < 5; j++){
          S[j] = (L[j] + R[j]) % MOD;
        }
      }
      long long M = S[1] * modinv(S[0]) % MOD;
      long long ans = 0;
      for (int j = 0; j <= t; j++){
        ans += S[j] * modpow(MOD - M, t - j)* binom[t][j] % MOD;
        ans %= MOD;
      }
      ans *= modinv(S[0]);
      ans %= MOD;
      cout << ans << endl;
    }
  }
}
0