#include using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for(int64 i = 0;i < (n);i++) #define FOR(i, a, b) for(int64 i = (a);i < (b);i++) #define all(x) x.begin(),x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair; using PLL = pair; const double eps = 1e-10; templateinline void chmin(A &a, B b){if(a > b) a = b;} templateinline void chmax(A &a, B b){if(a < b) a = b;} template vector make_v(size_t a){return vector(a);} template auto make_v(size_t a,Ts... ts){ return vector(ts...))>(a,make_v(ts...)); } template typename enable_if::value!=0>::type fill_v(U &u,const V... v){u=U(v...);} template typename enable_if::value==0>::type fill_v(U &u,const V... v){ for(auto &e:u) fill_v(e,v...); } template class Container=::std::vector> class SegTree{ public: using value_structure = ValueMonoid; using value_type = typename value_structure::value_type; using const_reference = const value_type &; using container_type = Container; using size_type = typename container_type::size_type; private: ::std::vector tree; size_type size_; static size_type getsize(const size_type x){ size_type ret = 1; while(ret < x) ret <<= 1; return ret; } inline value_type calc(const value_type a, const value_type b){ return value_structure::operation(a, b); } inline void calc_node(const size_type index){ if(tree.size() <= (index << 1 | 1)) return; tree[index] = value_structure::operation(tree[index<<1], tree[index<<1 | 1]); } public: SegTree() : size_(0), tree(){} SegTree(const size_type size) : size_(size), tree(size << 1, value_structure::identity()){} template SegTree(InputIterator first, InputIterator last) : size_(::std::distance(first, last)){ tree = container_type(size_, value_structure::identity()); tree.insert(tree.end(), first, last); for(size_type i = size_;i > 0;i--){ calc_node(i); } } size_type size() const { return size_; } const_reference operator[](const size_type k) const { assert(k < size_); return tree[k+size_]; } value_type query(size_type l, size_type r){ assert(l <= r); assert(0 <= l && l < size_); assert(0 <= r && r <= size_); value_type retl = value_structure::identity(), retr = value_structure::identity(); for(l += size_, r += size_; l < r ; l >>= 1, r >>= 1){ if(l&1) retl = calc(retl, tree[l++]); if(r&1) retr = calc(tree[--r], retr); } return calc(retl, retr); } template void update(size_type index, const F& f){ assert(0 <= index && index < size()); index += size_; tree[index] = f(::std::move(tree[index])); while(index >>= 1) calc_node(index); } /* template size_type search(const F& f) const { // [0, result) is True and [0, result-1) is not. if(f(value_structure::identity())) return 0; if(!f(tree[1])) return size_+1; value_type acc = value_structure::identity(); size_type i = 1; while(i < } */ }; template class Container=::std::vector> class LazySegTree{ public: using value_structure = ValueMonoid; using value_type = typename value_structure::value_type; using operator_structure = OperatorMonoid; using operator_type = typename operator_structure::value_type; using modifier = Modifier; using const_reference = const value_type &; using container_value_type = Container; using container_operator_type = Container; using size_type = typename container_value_type::size_type; private: container_value_type tree; container_operator_type lazy; size_type size_, height; static size_type getsize(const size_type x){ size_type ret = 1; while(ret < x) ret <<= 1; return ret; } static size_type getheight(const size_type x){ size_type ret = 0; while((static_cast(1) << ret) < x){ ret++; } return ret; } inline static value_type calc(const value_type a, const value_type b){ return value_structure::operation(a, b); } inline static void apply(operator_type &data, const operator_type a){ data = operator_structure::operation(data, a); } inline static value_type reflect(const value_type v, const operator_type o){ return modifier::operation(v, o); } void push(const size_type index){ tree[index] = reflect(tree[index], lazy[index]); apply(lazy[index << 1], lazy[index]); apply(lazy[index << 1 | 1], lazy[index]); lazy[index] = operator_structure::identity(); } void calc_node(const size_type index){ if(tree.size() <= (index << 1 | 1)) return; assert(0 < index); tree[index] = calc(reflect(tree[index << 1], lazy[index << 1]), reflect(tree[index << 1 | 1], lazy[index << 1 | 1])); } void build(size_type index){ while(index >>= 1){ calc_node(index); } } void propagate(const size_type index){ for(size_type shift = height; shift ; --shift){ push(index >> shift); } } void rebuild(){ for(size_type i = size_-1;i > 0;--i){ calc_node(i); } } public: LazySegTree() : size_(0), height(0), tree(), lazy(){} LazySegTree(const size_type size) : size_(size), height(getheight(size)), tree(size << 1, value_structure::initializer()), lazy(size << 1, operator_structure::identity()){ rebuild(); } template LazySegTree(InputIterator first, InputIterator last) : size_(::std::distance(first, last)){ height = getheight(size_); tree = container_value_type(size_, value_structure::identity()); lazy = container_operator_type(size_ << 1, operator_structure::identity()); tree.insert(tree.end(), first, last); rebuild(); } size_type size() const { return size_; } const_reference operator[](const size_type k){ assert(k < size_); propagate(k+size_); tree[k+size_] = reflect(tree[k+size_], lazy[k+size_]); lazy[k+size_] = operator_structure::identity(); return tree[k+size_]; } value_type query(size_type l, size_type r){ assert(l <= r); assert(0 <= l && l < size_); assert(0 <= r && r <= size_); value_type retl = value_structure::identity(), retr = value_structure::identity(); l += size_; r += size_; propagate(l); propagate(r-1); for(; l < r ; l >>= 1, r >>= 1){ if(l&1){ retl = calc(retl, reflect(tree[l], lazy[l])); l++; } if(r&1){ r--; retr = calc(reflect(tree[r], lazy[r]), retr); } } return calc(retl, retr); } void update(size_type l, size_type r, const operator_type& data){ assert(l <= r); assert(0 <= l && l < size_); assert(0 <= r && r <= size_); l += size_; r += size_; propagate(l); propagate(r - 1); for(size_type l_ = l, r_ = r; l_ < r_ ; l_ >>= 1, r_ >>= 1){ if(l_ & 1) apply(lazy[l_++], data); if(r_ & 1) apply(lazy[--r_], data); } build(l); build(r - 1); } template void update(size_type index, const F& f){ assert(0 <= index && index < size()); index += size_; propagate(index); tree[index] = f(::std::move(tree[index])); lazy[index] = operator_structure::identity(); build(index); } /* template size_type search(const F& f) const { // [0, result) is True and [0, result-1) is not. if(f(value_structure::identity())) return 0; if(!f(tree[1])) return size_+1; value_type acc = value_structure::identity(); size_type i = 1; while(i < } */ }; //class f_v { //public: // using value_type = int64; // static value_type identity() { return 0; } // static value_type initializer() { return identity(); } // static value_type operation(const value_type& a, const value_type& b) { // return a + b; // } //}; // //class f_o { //public: // using value_type = int64; // static value_type identity() { return 0; } // static value_type init //}; struct value { int64 sum; array m; value() {} value(int64 even, int64 odd, int64 sum_) : sum(sum_) { m[0] = even; m[1] = odd; } }; class v_monoid { public: using value_type = value; static value_type identity() { return value(0, 0, 0); } static value_type initializer() { return identity(); } static value_type operation(const value_type& a, const value_type& b) { return value(a.m[0]+b.m[0], a.m[1]+b.m[1], a.sum+b.sum); } }; class o_monoid { public: using value_type = vector; static value_type identity() { return vector(); } static value_type initializer() { return identity(); } static value_type operation(const value_type& a, const value_type& b) { if (a.size() == 0) return b; vector res = a; // fs -> 0 => (v+sc)%2, fs -> 1 => v+sc REP(i, b.size()) { if (res.back().fs == 0 && b[i].fs == 0) { res.back().sc += b[i].sc; } else if (res.back().fs == 0 && b[i].fs == 1) { res.push_back(b[i]); } else if (res.back().fs == 1 && b[i].fs == 0) { res.back() = PLL(0, b[i].sc+res.back().sc); } else if (res.back().fs == 1 && b[i].fs == 1) { res.back().sc += b[i].sc; } } return res; } }; class modifier { public: static value operation(const value& a, const vector& b) { value ret = a; REP(i, b.size()) { if (b[i].fs == 0) { if (b[i].sc % 2) { swap(ret.m[0], ret.m[1]); } ret.sum = ret.m[1]; } else { if (b[i].sc % 2) { swap(ret.m[0], ret.m[1]); } ret.sum += b[i].sc * (ret.m[0] + ret.m[1]); } } return ret; } }; int main(void) { int64 N, Q; cin >> N >> Q; vector a(N); REP(i, N) { cin >> a[i].sum; a[i].m[a[i].sum%2] = 1; } LazySegTree lsg(a.begin(), a.end()); REP(i, Q) { int64 tp, l, r; cin >> tp >> l >> r; l--; if (tp == 1) { lsg.update(l, r, vector{PLL(0, 0)}); } else if (tp == 2) { int64 x; cin >> x; lsg.update(l, r, vector{PLL(1, x)}); } else { cout << lsg.query(l, r).sum << endl; } } }