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/aoj/dsl/dsl_2_i.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_I"
#include <bits/stdc++.h>
using namespace std;
const int e = 100000;
#include "../../../data_structure/sequence/lazy_segment_tree.hpp"
struct monoid{
  int cnt, sum;
  monoid(): cnt(0), sum(0){
  }
};
int main(){
  int n, q;
  cin >> n >> q;
  vector<monoid> A(n);
  for (int i = 0; i < n; i++){
    A[i].cnt = 1;
  }
  function<monoid(monoid, monoid)> op = [](monoid a, monoid b){
    a.cnt += b.cnt;
    a.sum += b.sum;
    return a;
  };
  function<monoid(int, monoid)> mp = [](int a, monoid b){
    if (a != e){
      b.sum = a * b.cnt;
    }
    return b;
  };
  function<int(int, int)> comp = [](int a, int b){
    if (b == e){
      return a;
    } else {
      return b;
    }
  };
  lazy_segment_tree<monoid, int> ST(A, op, mp, comp, monoid(), e);
  for (int i = 0; i < q; i++){
    int c;
    cin >> c;
    if (c == 0){
      int s, t, x;
      cin >> s >> t >> x;
      t++;
      ST.range_apply(s, t, x);
    }
    if (c == 1){
      int s, t;
      cin >> s >> t;
      t++;
      cout << ST.range_fold(s, t).sum << endl;
    }
  }
}
#line 1 "test/aoj/dsl/dsl_2_i.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_I"
#include <bits/stdc++.h>
using namespace std;
const int e = 100000;
#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 6 "test/aoj/dsl/dsl_2_i.test.cpp"
struct monoid{
  int cnt, sum;
  monoid(): cnt(0), sum(0){
  }
};
int main(){
  int n, q;
  cin >> n >> q;
  vector<monoid> A(n);
  for (int i = 0; i < n; i++){
    A[i].cnt = 1;
  }
  function<monoid(monoid, monoid)> op = [](monoid a, monoid b){
    a.cnt += b.cnt;
    a.sum += b.sum;
    return a;
  };
  function<monoid(int, monoid)> mp = [](int a, monoid b){
    if (a != e){
      b.sum = a * b.cnt;
    }
    return b;
  };
  function<int(int, int)> comp = [](int a, int b){
    if (b == e){
      return a;
    } else {
      return b;
    }
  };
  lazy_segment_tree<monoid, int> ST(A, op, mp, comp, monoid(), e);
  for (int i = 0; i < q; i++){
    int c;
    cin >> c;
    if (c == 0){
      int s, t, x;
      cin >> s >> t >> x;
      t++;
      ST.range_apply(s, t, x);
    }
    if (c == 1){
      int s, t;
      cin >> s >> t;
      t++;
      cout << ST.range_fold(s, t).sum << endl;
    }
  }
}
Back to top page