cp_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub SSRS-cp/cp_library

:heavy_check_mark: test/library_checker/data_structure/range_affine_range_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
#include "../../../data_structure/sequence/lazy_segment_tree.hpp"
#include "../../../other/monoids/linear.hpp"
#include "../../../other/monoids/affine_sum.hpp"
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<affine_sum> a(N);
  for (int i = 0; i < N; i++){
    a[i].cnt = 1;
    cin >> a[i].sum;
  }
  lazy_segment_tree<affine_sum, linear> ST(a, op, mp, composite, affine_sum(), linear());
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 0){
      int l, r, b, c;
      cin >> l >> r >> b >> c;
      ST.range_apply(l, r, linear(b, c));
    }
    if (t == 1){
      int l, r;
      cin >> l >> r;
      cout << ST.range_fold(l, r).sum << endl;
    }
  }
}
#line 1 "test/library_checker/data_structure/range_affine_range_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
#line 2 "data_structure/sequence/lazy_segment_tree.hpp"
/**
 * @brief 遅延セグメント木
*/
template <typename T, typename F>
struct lazy_segment_tree{
  int N;
  vector<T> ST;
  vector<F> lazy;
  function<T(T, T)> op;
  function<T(F, T)> mp;
  function<F(F, F)> comp;
  T E;
  F id;
  lazy_segment_tree(int n, function<T(T, T)> op, function<T(F, T)> mp, function<F(F, F)> comp, T E, F id): op(op), mp(mp), comp(comp), E(E), id(id){
    N = 1;
    while (N < n){
      N *= 2;
    }
    ST = vector<T>(N * 2 - 1, E);
    for (int i = N - 2; i >= 0; i--){
      ST[i] = op(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
    lazy = vector<F>(N * 2 - 1, id);
  }
  lazy_segment_tree(vector<T> &A, function<T(T, T)> op, function<T(F, T)> mp, function<F(F, F)> comp, T E, F id): op(op), mp(mp), comp(comp), E(E), id(id){
    int n = A.size();
    N = 1;
    while (N < n){
      N *= 2;
    }
    ST = vector<T>(N * 2 - 1, E);
    for (int i = 0; i < n; i++){
      ST[N - 1 + i] = A[i];
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = op(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
    lazy = vector<F>(N * 2 - 1, id);
  }
  void push(int i){
    if (i < N - 1){
      lazy[i * 2 + 1] = comp(lazy[i * 2 + 1], lazy[i]);
      lazy[i * 2 + 2] = comp(lazy[i * 2 + 2], lazy[i]);
    }
    ST[i] = mp(lazy[i], ST[i]);
    lazy[i] = id;
  }
  void range_apply(int L, int R, F f, int i, int l, int r){
    push(i);
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      lazy[i] = f;
      push(i);
    } else {
      int m = (l + r) / 2;
      range_apply(L, R, f, i * 2 + 1, l, m);
      range_apply(L, R, f, i * 2 + 2, m, r);
      ST[i] = op(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  void range_apply(int L, int R, F f){
    range_apply(L, R, f, 0, 0, N);
  }
  T range_fold(int L, int R, int i, int l, int r){
    push(i);
    if (r <= L || R <= l){
      return E;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return op(range_fold(L, R, i * 2 + 1, l, m), range_fold(L, R, i * 2 + 2, m, r));
    }
  }
  T range_fold(int L, int R){
    return range_fold(L, R, 0, 0, N);
  }
  T all(){
    push(0);
    return ST[0];
  }
};
#line 2 "other/monoids/linear.hpp"
struct linear{
  long long a, b;
  linear(){
    a = 1;
    b = 0;
  }
  linear(int a, int b): a(a), b(b){
  }
};
linear composite(linear A, linear B){
  return linear(A.a * B.a % MOD, (A.b * B.a + B.b) % MOD);
}
int value(linear A, int x){
  return (A.a * x + A.b) % MOD;
}
#line 2 "other/monoids/affine_sum.hpp"
struct affine_sum{
  int cnt;
  long long sum;
  affine_sum(): cnt(0), sum(0){
  }
};
affine_sum op(affine_sum A, affine_sum B){
  A.cnt += B.cnt;
  A.sum += B.sum;
  A.sum %= MOD;
  return A;
}
affine_sum mp(linear f, affine_sum A){
  A.sum = (A.sum * f.a + A.cnt * f.b) % MOD;
  return A;
}
#line 8 "test/library_checker/data_structure/range_affine_range_sum.test.cpp"
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<affine_sum> a(N);
  for (int i = 0; i < N; i++){
    a[i].cnt = 1;
    cin >> a[i].sum;
  }
  lazy_segment_tree<affine_sum, linear> ST(a, op, mp, composite, affine_sum(), linear());
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 0){
      int l, r, b, c;
      cin >> l >> r >> b >> c;
      ST.range_apply(l, r, linear(b, c));
    }
    if (t == 1){
      int l, r;
      cin >> l >> r;
      cout << ST.range_fold(l, r).sum << endl;
    }
  }
}
Back to top page